Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP Ajax MySQL
I am trying to have a button on a webpage. When you click on it, the Dropdown box will load. The code I have below works fine, but I would rather start with an empty dropdown box and have it load when the button is clicked. Here is my working code....
 
<?php 
$dbc = mysql_connect('','','') 
     or die('Error connecting to MySQL server.'); 
 
mysql_select_db('MyDB'); 
 
$result = mysql_query("select * from tblRestaurants order by RestName ASC"); 
?> 
<!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" xml:lang="en" lang="en"> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
<title>SEARCH</title> 
 

</head> 
 
<body> 
<form method="post" action="1004mcout.php">
 

 
<p><center>SEARCH</CENTER></P> 
<select name="RestName"> 
<?php 
while ($nt= mysql_fetch_assoc($result))  
{ 
     echo '<option value="' . $nt['RestID'] . '">' . $nt['RestName'] . '</option>'; 
}  
?> 
</select>
 
<p> SPACE</p> 
 

<p></p> 
 

 

<p>Click "SUBMIT" to display the calculation results</p> 
 
<input type="submit" name="Submit" value="Submit" /> 
 
<br /> 
 

</form> 
 
</body> 
 
</html> 
Posted 10-Jan-13 11:43am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This works
 
 
while ($nt= mysql_fetch_assoc($result))
    $arrData[] = $nt;
 
if(isset($_GET["ajax"]))
{
    echo json_encode($arrData);
    die();
}
?>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

 
while ($nt= mysql_fetch_assoc($result))
    $arrData[] = $nt;
 
//If you want to test without DB, uncomment this, and comment previous
/*$arrData = array(
        array('RestID' => "1", 'RestName' => "Mike"),
        array('RestID' => "2", 'RestName' => "Sebastian"),
        array('RestID' => "3", 'RestName' => "Shitter")
        );*/
 
if(isset($_GET["ajax"]))
{
    echo json_encode($arrData);
    die();
}
?>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 510
1 CPallini 330
2 OriginalGriff 225
3 George Jonsson 120
4 ChauhanAjay 104
0 OriginalGriff 6,474
1 Sergey Alexandrovich Kryukov 6,190
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100