Click here to Skip to main content
12,248,339 members (45,867 online)
Rate this:
 
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"); 
?>
Posted 10-Jan-13 12:43pm
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2016
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