/    Sign up×
Community /Pin to ProfileBookmark

PHP AJAX problem

Hi. I am trying a simple example to pull some text from a database via AJAX. I am following this example [url]http://www.w3schools.com/PHP/php_ajax_database.asp[/url]

The javascript and form are here:

<!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>
<script type=”text/javascript”>
var xmlHttpfunction showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert (“Browser does not support HTTP Request”)
return
}
var url=”getuser.php”
url=url+”?q=”+str
url=url+”&sid=”+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open(“GET”,url,true)
xmlHttp.send(null)
}function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState==”complete”)
{
document.getElementById(“txtHint”).innerHTML=xmlHttp.responseText
}
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}

</script>
</head>
<body>
<form>
Select a User:
<select name=”users” onchange=”showUser(this.value)”>
<option value=”1″>1</option>
<option value=”2″>2</option>
<option value=”3″>Glenn Quagmire</option>
<option value=”4″>Joseph Swanson</option>
</select>
</form><p>
<div id=”txtHint”><b>User info will be listed here.</b></div>
</p></body>
</html>

</body>
</html>

and the php code is here getuser.php

<?php

define(‘MY_CODE’,TRUE);

require(‘globals/functions.gbl.php’);
require(‘classes/templates/templates.class.php’);
require(‘config/config.cfg.php’);
require(‘globals/db.gbl.php’);
require(‘globals/common.gbl.php’);
require(‘globals/logindata.gbl.php’);
require(‘globals/stats.gbl.php’);

$q=$_GET[“q”];

$sql=”SELECT * FROM mi_users WHERE user_id = ‘”.$q.”‘”;

$result = mysql_query($sql);

echo “<table border=’1′>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>”;

while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘user_name’] . “</td>”;
echo “<td>” . $row[‘user_email’] . “</td>”;
echo “</tr>”;
}
echo “</table>”;
?>

The php code is fine as i ahve checked it. For some reason when i change the drop down menu, no results are displayed.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@SheldonAug 11.2008 — Are you getting any Javascript Errors?

Try adding some 'alert' dubbuging in to your JS,

Also you should look in to Indenting your code! and using [ php ] <?php /* code */ ?> [ /php] tags around your ode inserts !!
Copy linkTweet thisAlerts:
@MrCoderAug 11.2008 — Use FireFox and the FireBug addon to help debug your Javascript.

<i>
</i>console.log();

..Is your friend in firebug.
Copy linkTweet thisAlerts:
@Kostas_ZotosAug 13.2008 — Hi,

For some reason when i change the drop down menu, no results are displayed.[/QUOTE]

Because the function "showUser" cannot be found.. (If the following is a fact)..

Is this a typo?

[CODE]<script type="text/javascript">
var [COLOR="Red"]xmlHttpfunction[/COLOR] showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{[/CODE]


I assume the right code should be like this:
[CODE]<script type="text/javascript">
var [COLOR="Green"]xmlHttp [/COLOR] // A global variable

[COLOR="Green"]function [/COLOR]showUser(str) // A function...
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{[/CODE]


Of course the problem can be elsewhere..

Regards!

Kostas
×

Success!

Help @birdy247 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...