DS928
asked on
PHP Switch Problems
I have a webpage with 5 buttons and one dropdown list. Depending on which button you click on, the listbox will be populated by that MySQL statement. The switch doesn't seem to work though. The listbox is not being populated. The code.
<?php
//$list = $_GET['case'];
//$case = 'cuisine';
$dbc = mysql_connect('','','')
or die('Error connecting to MySQL server.');
mysql_select_db('MyDB');
$list['Place'] = mysql_query("select * from tblRestaurants order by RestName ASC");
$list['Cuisine'] = mysql_query("select * from tblCuisines order by CuisineName ASC");
foreach($list as $key=>$value){
while ($nt = mysql_fetch_assoc($list[$key]))
$list_array[$key] = $nt;
}
if(isset($_GET["ajax"]))
{
switch($_GET['$case']){
case 'Place':
echo json_encode($list_array['Place']);
break;
case 'Cuisine':
echo json_encode($list_array['Cuisine']);
break;
}
die();
}
?>
<!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>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function displayPlace()
{
$.getJSON("Saturday.php?ajax=true", function(data) {
$.each(data, function(index, objRecord) {
var option=document.createElement("option");
option.value=objRecord.RestID;
option.text=objRecord.RestName;
$("#Doggie").append('<option value="' + objRecord.RestID + '">' + objRecord.RestName + '</option>');
});
});
}
function displayCuisine()
{
$.getJSON("Saturday.php?ajax=true", function(data) {
$.each(data, function(index, objRecord) {
var option=document.createElement("option");
option.value=objRecord.CuisineID;
option.text=objRecord.CuisineName;
$("#Doggie").append('<option value="' + objRecord.CuisineID + '">' + objRecord.CuisineName + '</option>');
});
});
}
</script>
<title>SEARCH</title>
</head>
<body>
<form>
<button type="button" onclick="javascript:displayPlace();">Place</button>
<button type="button" onclick="javascript:displayCuisine();">Cuisine</button>
<button type="button" onclick="javascript:displayCity();" >City</button>
<button type="button" onclick="javascript:displayState();">State</button>
<button type="button" onclick="javascript:displayZipCode();">Area</button>
<br />
<select name="Doggie" id="Doggie"></select>
<br />
</form>
</body>
</html>
It looks like your file is named "Saturday.php" and you are passing "ajax=true" as a GET. Your 'switch' statement is however based on $_GET['$case'] and you are Not passing a name/value pair for 'case'. In addition, $_GET['$case'] should not have the '$' because that says it's a variable instead of an index name. Should be $_GET['case'].
It would be normal to split this into two separate files, one HTML and a separate PHP file because you are not using the PHP info directly in the HTML part but only thru the AJAX call.
It would be normal to split this into two separate files, one HTML and a separate PHP file because you are not using the PHP info directly in the HTML part but only thru the AJAX call.
ASKER
That is a good question! I imagine that 'case' or whatever it's called need to be attached to the buttons. But how. I am new to this, I've been banging thru this with a hammer! Help is appreciated. Thank you.
ASKER
Thank you Dave. So how would I pass the value to 'case' from the buttons? I think I do need two pages. The PHP and the javascript on one and the html on another, otherwise the page will get reloaded? Correct? But still the lingering question, how to pass the value?
Your main page :
Test it script putting the following URL in your browser :
http://localhost/path/to/your/app lication/getOnlyJSONOptions.php?ca se=Place
or this one :
http://localhost/path/to/your/app lication/getOnlyJSONOptions.php?ca se=Cuisine
<!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=utf-8" />
<title>Q_27993331.html</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
// WAIT COMPLETE LOADING OF THE PAGE
$(document).ready(function() {
// ATTACH CLICK EVENT TO ALL BUTTON OF THE CURRENT DOCUMENT
$("button").click(function() {
// EACH TIME WE CLICK ON A BUTTON, CALL display FUNCTION
// HERE this represent THE CLICKED BUTTON
display( $(this).val() );
})
})
function display(what)
{
// CALL getOnlyJSONOptions.php, NOT THE SAME PAGE, THIS IS A SEPARATE PHP SCRIPT
$.getJSON("getOnlyJSONOptions.php?ajax=true", { "case" : what }, function(data) {
// REMOVE ANY PREVIOUS OPTIONS FIRST
$("#Doggie").empty();
// LOOP OVER THE OPTIONS DATA, OUR JSON OBJECT
$.each(data, function(index, objRecord) {
$("#Doggie").append('<option value="' + objRecord.ID + '">' + objRecord.Name + '</option>');
});
});
}
</script>
</head>
<body>
<form>
<button type="button" value="Place">Place</button>
<button type="button" value="Cuisine">Cuisine</button>
<button type="button" value="State">State</button>
<button type="button" value="ZipCode">Area</button>
<br />
<select name="Doggie" id="Doggie"></select>
<br />
</form>
</body>
</html>
getOnlyJSONOptions.php script :Test it script putting the following URL in your browser :
http://localhost/path/to/your/app
or this one :
http://localhost/path/to/your/app
<?php
$list_array = array();
if(isset($_GET['case'])) {
$case = $_GET['case'];
$dbc = mysql_connect('','','')
or die('Error connecting to MySQL server.');
mysql_select_db('MyDB');
switch($case){
case 'Place':
$list = mysql_query("select * from tblRestaurants order by RestName ASC");
break;
case 'Cuisine':
$list = mysql_query("select * from tblCuisines order by CuisineName ASC");
break;
}
foreach($list as $key=>$value) while($nt = mysql_fetch_assoc($list[$key])) $list_array[$key] = $nt;
}
header('Content-type: application/json');
json_encode($list_array);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How do you eat an elephant? One byte at a time! This will be a lot easier if you deconstruct the issues into separate parts. You might set the javaScript part aside and just concentrate on getting the PHP/MySQL part to work. Then you can add the jQuery layer back in later.
When you're debugging PHP scripts, var_dump() is your friend. It can show you what any variable contains, including arrays and objects.
When you're debugging PHP scripts, var_dump() is your friend. It can show you what any variable contains, including arrays and objects.
ASKER
Thank you, I tried the updated script. Still not populating the list.
switch($case){
case 'Place':
$result = mysql_query("select RestID as ID, RestName as Name from tblRestaurants order by RestName ASC");
break;
case 'Cuisine':
$result = mysql_query("select CuisineID as ID, CuisineName as Name from tblCuisines order by CuisineName ASC");
break;
}
if($result)
while($row = mysql_fetch_assoc($result)) {
$list_array[] = array("ID"=>$row["ID"], "Name"=>$row["Name"]);
}
}
header('Content-type: application/json');
echo json_encode($list_array);
ASKER
Thank you, Ray. I am very new to this. So debbuging although a great suggestion, is beyond my skill scope at this point.
try your query in a sql editor :
select RestID as ID, RestName as Name from tblRestaurants order by RestName ASC
and
select CuisineID as ID, CuisineName as Name from tblCuisines order by CuisineName ASC
again, again, USE two PHP distinct scripts.
One for your page
One to send back JSON, that will remove a possible issue and let you understand how ajax work. Please confirm you understand that.
If your query work, put the following in your web browser address bar :
http://localhost/path/to/your/application/getOnlyJSONOptions.php?case=Place
or this one :
http://localhost/path/to/your/application/getOnlyJSONOptions.php?case=Cuisine
you MUST see the JSON object in your browser
select RestID as ID, RestName as Name from tblRestaurants order by RestName ASC
and
select CuisineID as ID, CuisineName as Name from tblCuisines order by CuisineName ASC
again, again, USE two PHP distinct scripts.
One for your page
One to send back JSON, that will remove a possible issue and let you understand how ajax work. Please confirm you understand that.
If your query work, put the following in your web browser address bar :
http://localhost/path/to/your/application/getOnlyJSONOptions.php?case=Place
or this one :
http://localhost/path/to/your/application/getOnlyJSONOptions.php?case=Cuisine
you MUST see the JSON object in your browser
If you find that debugging is out of your reach, consider hiring a professional programmer. You will save a lot of time and money that way, and you may be able to get the results you need in very quick order. If you want to learn to do this stuff yourself, get this book for starters. It has excellent examples and is very readable. Most importantly it builds the learning experience in a structured way so you will never find yourself in "over your head."
http://www.sitepoint.com/books/phpmysql5/
http://www.sitepoint.com/books/phpmysql5/
ASKER
I ran the queries and the both work. Cannot see in my Browser. I have two files, the main one XSAT.php and the getOnlyJSONOptions.PHP...n ot sure if I understand the two distint scripts.
ASKER
Thank you Ray. Nice suggestion. I will order the books. In the meantime, I have almost the whole website down except for this problem. I had it working on one button only, but when I had to add the four buttons that chaged the whole ballgame. You see what we have so far. What do you suggest needs to be done?
I just want to be sure XSAT.php don't have the code retrieving the options data...
So if getOnlyJSONOptions.php don't show any data, you've an error in...
put the in the beggining of it (in bold) :
<?php
ini_set('display_errors',1 );
error_reporting(E_ALL);
an report any error you get.
So if getOnlyJSONOptions.php don't show any data, you've an error in...
put the in the beggining of it (in bold) :
<?php
ini_set('display_errors',1
error_reporting(E_ALL);
an report any error you get.
ASKER
No Errors reported, But just to be sure that all is square at this point here are my two pages. I renamed getOnlyJSONOptions.php to mydoggie.php, just easier.
XSAT.php
and mydoggie.php
*** Private data removed - Modalot, EE Community Support Moderator ***
XSAT.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//$dbc = mysql_connect('','','')
// or die('Error connecting to MySQL server.');
//mysql_select_db('MyDB');
?>
<!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=utf-8" />
<title>Doggie.html</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
// WAIT COMPLETE LOADING OF THE PAGE
$(document).ready(function() {
// ATTACH CLICK EVENT TO ALL BUTTON OF THE CURRENT DOCUMENT
$("button").click(function() {
// EACH TIME WE CLICK ON A BUTTON, CALL display FUNCTION
// HERE this represent THE CLICKED BUTTON
display( $(this).val() );
})
})
function display(what)
{
// CALL mydoggie.php, NOT THE SAME PAGE, THIS IS A SEPARATE PHP SCRIPT
$.getJSON("mydoggie.php?ajax=true", { "case" : what }, function(data) {
// REMOVE ANY PREVIOUS OPTIONS FIRST
$("#Doggie").empty();
// LOOP OVER THE OPTIONS DATA, OUR JSON OBJECT
$.each(data, function(index, objRecord) {
$("#Doggie").append('<option value="' + objRecord.ID + '">' + objRecord.Name + '</option>');
});
});
}
</script>
</head>
<body>
<form>
<button type="button" value="place">Place</button>
<button type="button" value="Cuisine">Cuisine</button>
<button type="button" value="City">City</button>
<button type="button" value="State">State</button>
<button type="button" value="ZipCode">Area</button>
<br />
<select name="Doggie" id="Doggie"></select>
<br />
</form>
</body>
</html>
and mydoggie.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$list_array = array();
if(isset($_GET['case'])) {
$dbc = mysql_connect('', '', '') or die('Error connecting to MySQL server.');
$case = mysql_real_escape_string($_GET['case']);
mysql_select_db('MyHead');
switch($case){
case 'Place':
$result = mysql_query("select RestID as ID, RestName as Name from tblRestaurants order by RestName ASC");
break;
case 'Cuisine':
$result = mysql_query("select CuisineID as ID, CuisineName as Name from tblCuisines order by CuisineName ASC");
break;
}
if($result)
while($row = mysql_fetch_assoc($result)) {
$list_array[] = array("ID"=>$row["ID"], "Name"=>$row["Name"]);
}
}
header('Content-type: application/json');
echo json_encode($list_array);
?>
*** Private data removed - Modalot, EE Community Support Moderator ***
ask a moderator to edit your php code, currently we can see login and password
your switch have << Place >>
and not << place >>
and you've
<button type="button" value="place">Place</button>
instead :
<button type="button" value="Place">Place</button>
else your script work fine
your switch have << Place >>
and not << place >>
and you've
<button type="button" value="place">Place</button>
instead :
<button type="button" value="Place">Place</button>
else your script work fine
ASKER
Since my password is up have you tried it? I did changes and its still not working. By the way how do I get in touch with the moderator? Today is my first day.
what do you see when you put : http://localhost/path/to/your/application/getOnlyJSONOptions.php?case=Cuisine
in the address bar of your browser?
in the address bar of your browser?
ASKER
HTTP Error 404.0 - Not Found
Maybe I am doing it wrong.
The path where the file is located is....
D:\MenuHead\Website\mydogg ie.php?cas e=Cuisine
so I am doing
http://localhost/D:\MenuHead\Website\mydoggie.php?case=Cuisine
Maybe I am doing it wrong.
The path where the file is located is....
D:\MenuHead\Website\mydogg
so I am doing
http://localhost/D:\MenuHead\Website\mydoggie.php?case=Cuisine
what is the name of the website folder inside your htdocs?
start your full path from here not starting by your drive...
what do you type to see the default page of your site?
start your full path from here not starting by your drive...
what do you type to see the default page of your site?
put both files inside htdocs folder and use :
http://localhost/getOnlyJSONOptions.php?case=Cuisine
and let me know what you're seeing
http://localhost/getOnlyJSONOptions.php?case=Cuisine
and let me know what you're seeing
ASKER
I dont have an htdocs folder. I have two drives. A c: drive for programs and a d; drive for my files, and nowhere did I find htdocs. Where should I make this folder?
ok, you need a webserver to use Ajax
you can install one for development purpose : http://www.wampserver.com/en/
BUT you should be able to test your mydoggie.php script putting the following in your webbrowser :
D:\MenuHead\Website\mydogg ie.php?cas e=Cuisine
let me know what you're seeing
If you can see the data go to install the wampserver... or perhaps you're ready to install both scrips on your hosted server? hostedresource.com
you can install one for development purpose : http://www.wampserver.com/en/
BUT you should be able to test your mydoggie.php script putting the following in your webbrowser :
D:\MenuHead\Website\mydogg
let me know what you're seeing
If you can see the data go to install the wampserver... or perhaps you're ready to install both scrips on your hosted server? hostedresource.com
ASKER
I'm using ISS on my computer. Do I still need to install wamp? I also made an htdocs folder on my website server and placed the two files into the folder. The folder is a sub-folder of html. Also I am on GoDaddy. They host my website currently. Whenever I type D:\MenuHead\Website\mydogg ie.php?=Cu isine I get an error that it's not found. If I type in D:\MenuHead\Website\mydogg ie.php it opens in Dreamweaver showing the code. The pages are both on the server. Did you try to run XSAT.php page. Give it a try. You have the password info. Let me know if it works for you.
you need a web server able to interpret php script so put the files on your web service
I confirm the scripts run fine with a correct web server
I confirm the scripts run fine with a correct web server
ASKER
The files are up. Just view the XSAT.php in your browser and let me know if you get any returned results.
what is the address?
if needed : http://goo.gl/
if needed : http://goo.gl/
ASKER
Mercy! It's working! Kinda. The first problem was a typo in the connect file, my fault! So it loaded on the place, so I added the other button querys. Here is what is strange.
Place gives places
Cuisine gives cities
City gives Cities
State gives Areas
Area gives Areas
It's like every other one is working correctly? The address id www.menuhead.net, still in play around stage yet. Also it's very slow. I thought Ajax was faaasssst. There aren't that many records in there as you can see,
Here's the code.
Place gives places
Cuisine gives cities
City gives Cities
State gives Areas
Area gives Areas
It's like every other one is working correctly? The address id www.menuhead.net, still in play around stage yet. Also it's very slow. I thought Ajax was faaasssst. There aren't that many records in there as you can see,
Here's the code.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$list_array = array();
if(isset($_GET['case'])) {
$dbc = mysql_connect('','','') or die('Error connecting to MySQL server.');
$case = mysql_real_escape_string($_GET['case']);
mysql_select_db('MyDB');
switch($case){
case 'Place':
$result = mysql_query("select RestID as ID, RestName as Name from tblRestaurants order by RestName ASC");
break;
case 'Cuisine':
$result = mysql_query("select CuisineID as ID, CuisineName as Name from tblCuisines order by CuisineName ASC");
case 'City':
$result = mysql_query("select CityID as ID, CityName as Name from tblCities order by CityName ASC");
break;
case 'State':
$result = mysql_query("select StateID as ID, StateName as Name from tblStates order by StateName ASC");
case 'Area':
$result = mysql_query("select AreaID as ID, AreaName as Name from tblAreas order by AreaName ASC");
break;
}
if($result)
while($row = mysql_fetch_assoc($result)) {
$list_array[] = array("ID"=>$row["ID"], "Name"=>$row["Name"]);
}
}
header('Content-type: application/json');
echo json_encode($list_array);
?>
great!
there's no problem inside the XSAT.php page
in mydoggie.php you forget to place a << break; >> after line 20 and after line 25
there's no problem inside the XSAT.php page
in mydoggie.php you forget to place a << break; >> after line 20 and after line 25
ASKER
That did it! This ended a week of torture! Thank you so very much! I appreciate your patience and bedside manor! Have a good day.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for DS928's comment #a38770872
for the following reason:
Knowledgable and patient. Took the time to do it right along with informative examples, Did not bash or belittle me for not knowing. A saint!
Accepted answer: 0 points for DS928's comment #a38770872
for the following reason:
Knowledgable and patient. Took the time to do it right along with informative examples, Did not bash or belittle me for not knowing. A saint!
points not awarded correctly
you need to choose one of my post to close the question to award points not one of your
ASKER
Like i said before, the best! Patient, knowledgable and a great bedside manor! Really an expert!
thanks for your kind words!
have a nice sunday!
have a nice sunday!
what is case?