doumengyin0491 2013-09-23 16:51
浏览 26

动态形式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 .

  • 写回答

2条回答 默认 最新

  • doudeng2057 2013-09-23 17:05
    关注

    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.
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line