John Doe
John Doe

Reputation: 2060

AJAX connecting to db with php and javascript

I have never used ajax before and am trying to fully understand this example which is similar to what I want to do. Here are my questions in regard to the sample code.

  1. Do I need to include some sort of ajax header script to use it???

  2. What does this part do in the html file:

    • xmlhttp.open("GET","getuser.php?q="+str,true);
    • xmlhttp.send();
  3. This is where I have the biggest issue. What is q? Is it the vaule ex:1, 2,3 etc.??

    • $q=$_GET["q"];

Here is the html file....

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

Here is the php file called getuser.php...

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Upvotes: 0

Views: 3571

Answers (2)

AlanPHP
AlanPHP

Reputation: 188

xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();

This opens the file, and then sends the url to getuser.php. Basics :P

$q=$_GET["q"];

This is is what the ajax file sent to your getuser.php file. This shouldn't be a "security" variable because get is easy to hack. If it needs to be, try using post.

Hope I helped :)

Upvotes: 0

esqew
esqew

Reputation: 44707

  1. No. It's a standard spec in EMCAScript.
  2. That particular line of code opens a GET request to the getuser.php file, passing the GET parameter q as the value of str in your JS.
  3. In that particular instance, $q is being set to the value of the GET value of q.

In a nutshell, your JS calls a PHP script to execute itself while passing a variable to (ostensibly) modify the results of the script.

If you don't understand GET and POST variables, I reccomend reading at least the PHP manual's page on the $_GET array, or a more general overview of GET and POST variables.

Upvotes: 1

Related Questions