Jump to content

simple dynamic query, no search results


stijn0713

Recommended Posts

Can somebody tell me what's wrong with the code? i just followed this example: http://www.tutorialspoint.com/ajax/ajax_database.htm

 

I don't get any search results back

 

<!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>zoekfunctie</title>


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

var postcode = document.getElementById('postcode').value;
var provincie = document.getElementById('provincie').value;
var aland = document.getElementById('aland').value;
var gland = document.getElementById('gland').value;
var minAge = document.getElementById('minleeftijd').value;
var maxAge = document.getElementById('maxleeftijd').value;
var sex = document.getElementById('sex').value;	


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;
    }
  }

var queryString = "?postcode=" + postcode + "&provincie=" + provincie + "&aland=" + aland + "&gland=" +gland +"&minAge=" + minleeftijd +"&maxAge=" + maxleeftijd + "&sex=" + sex;
  
xmlhttp.open("GET","query.php" + queryString,true);
xmlhttp.send();
}
</script>

</head>

<body>

<form id="form1" name="form1" method="post" action="">
  <table width="287" height="270" border="1">
    <tr>
      <td width="93">Naam:</td>
      <td width="129">
        <input type="text" name="naam" id="naam" size ="10" /></td>
    </tr>
    <tr>
      <td>Postcode:</td>
      <td>
      <input name="postcode" type="text" id="postcode" size="10" /></td>
    </tr>
    <tr>
      <td>Provincie:</td>
      <td>
        <input name="provincie" type="text" id="provincie" size="10" />
      </label></td>
    </tr>
    <tr>
      <td>Land(adres):</td>
      <td><input name="aland" type="text" id="aland" size="10" /></td>
    </tr>
    <tr>
      <td>Land(geboren):</td>
      <td><input name="gland" type="text" id="gland" size="10" /></td>
    </tr>
    <tr>
      <td>leeftijd tussen</td>
      <td><label for="leeftijd min"></label>
      <input type="text" size ="10" name="minleeftijd" id="minleeftijd" />
      en
      <input type="text" size="10" name="maxleeftijd" id="maxleeftijd" /></td>
    </tr>
    <tr>
      <td>sex: </td>
      <td>
        <select name="sex" id="sex">
          <option value=""></option>
          <option value="M">M</option>
          <option value="F">F</option>
      </select></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="button" name="Zoek" id="Zoek" onclick ="zoekresultaten()" value="Zoek" /></td>
    </tr>
  </table>
  <p> </p>
</form>

Zoekresultaten: <div id="txtHint"></div>
</body>
</html>

 

 

<?php 
//db connection
mysql_connect("","","");
mysql_select_db("qualifield");


$postcode= $_GET["postcode"];
$prov = $_GET["provincie"];
$aland = $_GET["aland"];
$gland = $_GET["gland"];
$minAge = $_GET["minleeftijd"];
$maxAge = $_GET["maxleeftijd"];
$sex = $_GET["sex"];

$postcode = mysql_real_escape_string($postcode);
$prov = mysql_real_escape_string($prov);
$aland = mysql_real_escape_string($aland);
$gland = mysql_real_escape_string($gland);
$minAge = mysql_real_escape_string($minAge);
$maxAge = mysql_real_escape_string($maxAge);

$query = "SELECT * FROM respondenten WHERE geslacht = '$sex'";

$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Naam</th>";
$display_string .= "<th>Email</th>";
$display_string .= "<th>Geslacht</th>";
$display_string .= "<th>Geregistreerd op:</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[voornaam]s</td>";
$display_string .= "<td>$row[email]</td>";
$display_string .= "<td>$row[geslacht]</td>";
$display_string .= "<td>$row[TijdRegistratie]</td>";
$display_string .= "</tr>";

}
$display_string .= "</table>";
echo $display_string;
?>

Link to comment
Share on other sites

In order to see if your javascript is sending an ajax call, I always start with the php file it's calling set to this:

<?php
echo 'made it';
?>

When I get the return message I then place the correct info into the file.

 

I just checked your main file. It does not call the php Made It file! Problem is in the queryString

var queryString = "?postcode=" + postcode + "&provincie=" + provincie + "&aland=" + aland + "&gland=" +gland +"&minAge=" + minAge +"&maxAge=" + maxAge + "&sex=" + sex;

 

You were using the wrong identifiers.

 

If the call still does not work for you change this line:

xmlhttp.send();

To:

xmlhttp.send('');

I have never got the thing to work with an empty nor the word 'null' in the parentheses.

 

Try that and after you get 'made it' in the "txtHint" div THEN use your php file.

 

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.