动态形式php ajax

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>&nbsp;</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>&nbsp;</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:

  1. We use the jQuery blur() method to detect when user leaves the text field
  2. Then get contents of that field (var myId)
  3. Don't do an AJAX lookup if the field is empty
  4. Send the assoc var with key name "theID" and contents == myId to file myFile.php, via POST method
  5. On the PHP side, receive the ID and call it $id (on this side)
  6. Do you mysql lookup and get the "name" and "apellido" values
  7. Join them together as a simple string, separated by "|" char
  8. ECHO them back to the AJAX code block
  9. IMPORTANT: All data returned from PHP must be handled inside the success function!
  10. Extract the name and apellido values again
  11. Inject into the correct fields.

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);
?>