Jump to content

Ajax Menu To Retrieve mySQL Records


hobbiton73

Recommended Posts

I wonder whether someone can help me please.

 

I've found http://www.plus2net.com/php_tutorial/ajax-listbox.php tutorial to create a drop down menu using mySQL table data, which, in turn returns a list of results on the page.

 

Following this tutorial I've put together the tables in my database and the required scripts as shown in the tutorial with the one exception, the "z_db.php" file, which I've assumed to be:

 

<?php mysql_connect("host", "user", "password")or die(mysql_error()); mysql_select_db("database"); ?> 

 

The problem I have, is that when I try and run this, I receive the following error:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/2/d333603417/htdocs/development/catsearch.php on line 91 which is this line in the search form: echo "</head><body onload="ajaxFunction()";>";.

 

I must admit I've guessed as to the structure of the 'z_db.php' file should look like because this is not shown so perhaps this is the problem.

 

I just wondered wether someone could perhaps take a look at this please and let me know where I've gone wrong.

 

 

Many thanks and kind regards

Link to comment
Share on other sites

Hi, many thanks for this, I really appreciate any help that I can get.

 

Unfortunately after making the changes I'm still getting the same error.

 

I've added my code below. I just wondered if you could perhaps have a look at this please to see if there is anything else that I'm doing wrong.

 

Kind regards

 

ajaxfunction.php

<html>
<head>

<script type="text/javascript">
function ajaxFunction()
{

//document.writeln(val)
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

function stateChanged(){
if(httpxml.readyState==4){

var myObject = eval('(' + httpxml.responseText + ')');


//var myObject = httpxml.responseText;
//document.getElementById("display").innerHTML=myObject;





var msg=myObject.value[0].message;
if(msg.length > 0){document.getElementById("msg").innerHTML=msg;}
else{document.getElementById("msg").style.display='none';}

var str="<table width='50%' bgcolor='#ffffff' align=center><tr><th>ID</th><th>Name</th></tr>";
var color="#f1f1f1";
for(i=0;i<myObject.data.length;i++)
{ 
if((i%2)==0){color="#ffffff";}
else{color="#f1f1f1";}
str = str + "<tr bgcolor="+color+"><td>" + myObject.data[i].subcat_id + " </td><td>"+ myObject.data[i].subcat_name + "</a></td></tr>"

}



str = str + "</table>" ;


document.getElementById("display").innerHTML=str;
}
}

var url="subcat2.php";
var cat_id=document.myForm.cat_id.value;
url=url+"?cat_id="+cat_id;

url=url+"&kid="+Math.random();
//alert(url)
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
// document.getElementById("display").innerHTML="Please Wait....";
document.getElementById("msg").style.background='#f1f1f1';
document.getElementById("msg").innerHTML="Please Wait ... ";
document.getElementById("msg").style.display='inline';
}

</script>

<?php
require "z_db.php";
echo '</head><body onload="ajaxFunction()";>';
echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit="ajaxFunction(this.form); return false">


<td align='center' colspan=2><font face='verdana, arial, helvetica' size='2' ><b> Select a Category</b> </font></td></tr>";
echo "<tr>";
echo "<td align='center'>";
$query="SELECT * FROM plus2_cat order by cat_name";
$result=mysql_query($query);
echo mysql_error();
echo "<select name=cat_id onChange="ajaxFunction()"><option value=0>Show All</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[cat_id]>$nt[cat_name]</option>";
}

echo "</select>";
echo "</font></td>";
echo "</tr></form>";
echo "</table>";


?>
<div id=msg style="position:absolute; z-index:1; left: 1100px; top: 0px;" >This is message area</div>

<div id="display"><b>Records will be displayed here</b></div>

</body>
</html> 

Link to comment
Share on other sites

echo '</head><body onload="ajaxFunction()";>';

 

Or! Alternatively and what I think blacknight was getting as use backslashed to escape the speech marks when within speech marks. Otherwise you will generate that error. Below:

 

<?php

echo "</head><body onload=\"ajaxFunction()\";>";

?>

 

That said, whenever you use speech marks within an echo try and surround the entire thing in apostrophes ('), makes it a LOT easier.

Link to comment
Share on other sites

Hi, many thanks for the clarification.

 

Unfortunately though, again after making the changes I'm still confronted by the same error. Perhaps this a beginners view on things but is this line correct?

 

echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit="ajaxFunction(this.form); return false">

 

I was thinking that the form should come before the PHP.

 

Kind regards

Link to comment
Share on other sites

Same as the line before.

 

echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit="ajaxFunction(this.form); return false">... ";

 

echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form name=myForm method='post' onSubmit=\"ajaxFunction(this.form); return false\">...";

Link to comment
Share on other sites

All,

 

Many thanks for your help. I wasn't able to get this particular example working. However, a few days ago I'd saved details of a simpler script. At the time I couldn't get it to work, but through all of your help I've now been able to get this working.

 

Kind regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.