user3487006 user3487006 - 17 days ago 6
PHP Question

php form that requests data based on user select

I have edited this based on help already received. I seem to be having issues with how the information is being called from the database. I keep getting errors of no results found so to speak. I think I got a pretty good idea what might be the cause, but I am not sure to rectify this.

Originally this search function was set up differently. I had an html page that had a dropdown list each option was assigned a value, this being text i.e.

<option Value="North East">North East</option>


However because I have separate tables in the database that control the population of the drop downs, the value is no longer being text but a number.

Any ideas on how to combat this?

This is currently what I have from head to toe.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Results</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
}
});

});
});
</script>
<style>
body {
color: #FFFFFF;
}
#theclub{
border:1px solid white;
padding: 10px;
}
label
{
font-weight:bold;
padding:10px;
}
</style>
<link rel="stylesheet" type="text/css" href="Style.css">
<script type='text/javascript' src='style2.js'></script>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<label>Country :</label> <select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
} ?>
</select>
<label>City :</label> <select name="city" class="city">
<option selected="selected">--Select City--</option>
</select>
<input type="submit" value="Search" name="submit">
</form>
<?php
if(isset($_POST['submit']) && $_POST['submit'] != ""){ } //--here is the condition that is true if search button is pressed
$selectedOption = $_POST["city"];

$result = mysqli_query($con,
sprintf("SELECT * FROM `SouthYorkshire` WHERE `EstProv` = '%s'",
preg_replace("/[^A-Za-z ]/", '', $selectedOption)));

echo "<div id=\"Results\">";

while($row = mysqli_fetch_array($result))
{
echo "<div id=\"theclub\">";
echo "<div class=\"ClubName\">";
echo $row['EstName'];
echo "</div><br>";
echo "<div class=\"Location\">";
echo $row['EstAddress2'];
echo "</div>";
echo "<br>";
echo "<div id=\"website\"><a href=\"#\"><img src=\"photos/more-info.png\" width=\"75\" height=\"25\"/></a> <a href=\"" . $row['EstWebsite'] ."\" target=\"_blank\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div></a></div>";
echo "<br>";
}
echo "</div>";

mysqli_close($con);
?>
<br>
</body>

Answer

I'm shooting in the dark a bit because i'm not sure what you mean by first and second code...

if you want only one part of the code to run you need some sort of condition to check if to run it or not

if you dont want the second part of the code run right away you want to check if the post variable city is set and not empty and only run the code if the value isn't empty

if(isset($_POST['city']) && $_POST['city'] != ""){
  // your code here

}

Comments