Reputation: 46
I'm novice in AJAX and PHP programming. I believe this is quite simple, but I'm with difficult.
What I need is that user select a date (using jquery date picker) and click in OK button.
The form will call page query.php (via Ajax). The query.php will check the date. If the date have registries in the database, query.php will return a table populated for index.php.
I have based my script according this example: http://www.w3schools.com/php/php_ajax_database.asp
But in that example, the trigger is the event ( onchange="showUser(this.value)"). I need to call AJAX function after submit the form.
I did that using (), but when I try to get the date using $q = $_GET['q']; the value come as blank.
Index.php (relevant information):
<!DOCTYPE html>
<html lang='pt-BR'>
<head>
<script>
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","checar_data.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="dateForm" onsubmit="showUser(); return false;" method="GET">
Data:
<input name="datepicker" id="datepicker" type="text" size="10" maxlength="8" />
<input name="OK" value="OK" type="submit" >
</form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
query.php:
<?php
$q = intval($_GET['q']);
echo "$q -> bla bla";
?>
How can I solve this?
Upvotes: 1
Views: 1332
Reputation: 896
query.php
change to
<?php
$q = $_GET['q'];
echo "$q";
?>
send data page is not correct
xmlhttp.open("GET","checar_data.php?q="+str,true);
change to
xmlhttp.open("GET","query.php?q="+str,true);
your function have one parameter and leave empty form declare at startup,
change from
<form id="dateForm" onsubmit="showUser(); return false;" method="GET">
to
<form id="dateForm" onsubmit="showUser(this.datepicker.value); return false;" method="GET">
Upvotes: 1