user3487006 user3487006 - 1 year ago 67
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" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src=""></script>
<script type="text/javascript">
var id=$(this).val();
var dataString = 'id='+ id;

type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)

body {
color: #FFFFFF;
border:1px solid white;
padding: 10px;
<link rel="stylesheet" type="text/css" href="Style.css">
<script type='text/javascript' src='style2.js'></script>
<form method="POST" enctype="multipart/form-data">
<label>Country :</label> <select name="country" class="country">
<option selected="selected">--Select Country--</option>
$sql=mysql_query("select id,data from data where weight='1'");
echo '<option value="'.$id.'">'.$data.'</option>';
} ?>
<label>City :</label> <select name="city" class="city">
<option selected="selected">--Select City--</option>
<input type="submit" value="Search" name="submit">
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>";


Answer Source

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download