i have this form.
<form id="form1" name="form1" method="post" action="">
<p>
<label for="textfield"></label>
Id:
<input type="text" name="textfield" id="textfield" />
</p>
<p>
<label for="textfield2"></label>
Name:
<input type="text" name="textfield2" id="textfield2" />
</p>
<p>
<label for="textfield3"></label>
Apellido:
<input type="text" name="textfield3" id="textfield3" />
</p>
<p>
<input type="submit" name="button" id="button" value="Enviar" />
</p>
<p> </p>
</form>
when i wrote de id and then pulse key tab, in the input name and apellido must get the my databases mysql this information .
i hope, you can understand me .
Your code will look something like this:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#textfield').blur(function() {
var myId = $(this).val();
if (myId == '') return false;
$.ajax({
type: "POST",
url: "myFile.php",
data: "theID=" +myId,
success: function(fromPHP) {
var allRecd = fromPHP.split('|');
var name = allRecd[0];
var apel = allRecd[1];
$('#textfield2').val(name);
$('#textfield3').val(apel);
}
})
});
}); //END $(document).ready()
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="textfield"></label>
Id:
<input type="text" name="textfield" id="textfield" />
</p>
<p>
<label for="textfield2"></label>
Name:
<input type="text" name="textfield2" id="textfield2" />
</p>
<p>
<label for="textfield3"></label>
Apellido:
<input type="text" name="textfield3" id="textfield3" />
</p>
<p>
<input type="submit" name="button" id="button" value="Enviar" />
</p>
<p> </p>
</form>
</body>
</html>
PHP Side: myFile.php
<?php
$id = $_POST['theID'];
//Do your mysql query here, for example
$result = mysql_query("SELECT * FROM users WHERE user_id = '$id' ");
$n = $result['name'];
$a = $result['apellido'];
$response = $n . "|" . $a;
echo $response;
Notes:
blur()
method to detect when user leaves the text fieldsuccess
function!You can use jQuery to use AJAX and send this form data to MySQL.
$(document).ready(function(){
$('#button').on('click',function(e){
var data = $('#form1').serialize();
$.ajax({
url:'get_data.php',
type: 'post',
data: data,
success: function(response) {console.log(response);},
error: function() {console.log('Request failed.')}
});
return false;
});
});
And on server side you can get this data like below and save into mysql database. I have assumed you have a table name Persons.
<?php
$textfield = $_POST['textfield'];
$textfield2= $_POST['textfield2'];
$textfield3= $_POST['textfield3'];
$con=mysqli_connect("localhost","dbusername","dbpass","dbname");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO Persons (textfield , textfield2, textfield3)
VALUES ('$textfield', '$textfield2','$textfield3')");
mysqli_close($con);
?>