dongrunying7537 2013-09-05 15:57
浏览 18
已采纳

当用户更改类别时自动填充表单时使用JSON

i'm trying to fill the form with database information when user changes the category(select tag changes). here is my code.

<form onsubmit='return false'>
select<select id="select">
       <option value='5'>5</option>
       <option value='6'>6</option>
       <option value='7'>7</option>
       <option value='8'>8</option>
       <option value='9'>9</option>
       <option value='10'>10</option>
       </select>
       fname<input type="text" id='fname' name="fname" /><br />
       lname<input type="text" id='lname' name="lname" /><br />
</form>


<script type="text/javascript">

$(document).ready(function(){
    $("#select").change(function(){
        selectid = $(this).val();
          // using post method to get firstname   
                     $.post("retreive.php", {selectid:selectid}, function(result){
                            $("#fname").val();
                            //again using post method here get lastname
                           $.post("one.php", {selectid:selectid}, function(result){
                             $("#lname").val();

                           });
                      });   
     });    
});

retreive.php

<?php
//for firstname
if( isset($_POST['selectid']) ){
     //using this selectid i'm getting firstname
     echo $firstname;
     exit();
}

//again for last name
if( isset($_POST['selectid']) ){
     //using this selectid i'm getting firstname
     echo $lastname;
     exit();
}
?>

Finally this procedure auto filling the form fields i.e; firstname and lastname. I know this is foolish and time taken procedure. I've heard that this work done easily and no need to make our php bare all that burden with using the concept of JSON. I have tried to understand but couldn't. I hope you guys can understand the problem i'm facing, hoping for a solution. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • donglijuan8227 2013-09-05 16:12
    关注

    You are made ajax request, but do nothing with result data.
    Try this

    $(document).ready(function(){
        $("#select").change(function(){
           var selectid = $(this).val();
              // using post method to get firstname   
                         $.post("retreive.php", {selectid:selectid}, function(result){
                                $("#fname").val(result);
                                //again using post method here get lastname
                               $.post("one.php", {selectid:selectid}, function(result){
                                 $("#lname").val(result);
    
                               });
                          });   
         });    
    });
    

    You don't need two ajax requests - you can make one request, and get json data.
    Read about jquery ajax method and json format

    $(function(){
     $('#select').change(function(){
     var selectid = $(this).val();
     $.ajax({
      url: 'retreive.php',
      data: {selectid: selectid},
      type: 'POST',
      dataType: 'JSON',
      success: function(data)
      {
        $("#fname").val(result.fname);
        $("#lname").val(result.lname);
      }
    });
    });
    });
    

    And php

    <?php
     if (isset($_POST['selectid'])) {
      echo json_encode(array('fname' => $fname, 'lname' => $lname));
      exit;
     }
    

    And if something goes wrong, when you made ajax request, open console, and find errors. If you have no errors, go for the network and see what server returns you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度