I am doing a project where I need to make sure that my form instantly autofills 2 textboxes based on input from the first textbox. Right now I am just testing. I can't use database entries to populate my form, only PHP variables. My logic needs to be like this: user types in a specific 10 digit number into a textbox on my form (specified in a PHP file), then the other two textboxes get autofilled with specific entries after an AJAX call to that PHP file. This is definitely one of the more complex tasks that I've encountered so far (noob). My code doesn't work, so I would really appreciate any help with getting it to function properly.
HTML is like this:
<!--load scripts, libraries, etc.-->
<form method="GET">
Number: <input type="text" name="num" id="num">
First Name: <input type="text" name="first" id="first">
Last Name: <input tupe="text" name"last" id="last">
</form>
PHP:
<?php
$num=$_GET["num"];
$first=$_GET["first"];
$last=$_GET["last"];
if ($num=="0123456789")
{
$fill = array(
'firstname' => $first["John"],
'lastname' => $last["Smith"],
);
echo json_encode($fill);
}
else
{
echo "Bad input.";
}
?>
jQuery:
$(document).ready(function () {
$("#num").keyup(function () {
var el = $(this);
if (el.val().length === 10) {
$.ajax({
url: "http://localhost/test.php",
cache: false,
dataType: "json",
type: "GET",
data: "npi=" + el.val(),
success: function (result) {
$("#first").val(result.firstname);
$("#last").val(result.lastname);
}
});
}
});
});
$first=$_GET["first"]; $last=$_GET["last"];you are not sending these values to php right?data: "npi=" + el.val(),asdata: {npi: el.val()}then the other two textboxes get autofilled with specific entriesso you are not passing these??? you have to get these values from php right??