【问题标题】:jquery gets data from other page and enter into form fieldsjquery 从另一个页面获取数据并输入表单字段
【发布时间】:2013-09-27 12:43:23
【问题描述】:

我只是想从 test2.php 页面获取值并在 test1.php 上相应地填写我的表单输入字段。请帮助和建议。

测试 2 页面

$empidx="Design-1012-1000";
$countries = mysqli_query($con,"select country from countries");
$query = mysqli_query($con,"SELECT * from employee where emprecord='$empidx' ");
$find=mysqli_fetch_array($query);
echo $find['empid'];

测试 1 页

<form id="employees">
    <input type="text" name="empid"><br>
    <input type="text" name="emprecord"><br>
    <input type="text" name="salute"><br>
    <input type="text" name="fname"><br>
    <input type="text" name="mname"><br>
    <input type="text" name="lname"><br>
    <input type="text" name="depart"><br>
    <input type="text" name="desig"><br>
    <input type="text" name="joindate"><br>
    <input type="text" name="leavedate"><br>
    <input type="submit" value="Submit" name="submit">
</form>

我的mysql字段是

Id, empid, emprecord, salute, fname, mname, lname, depart, desig, joindate, leavedate

【问题讨论】:

  • 我还是卡住了请指教。

标签: php jquery mysql json get


【解决方案1】:

你可以试试 jQuery AJAX :) http://api.jquery.com/jQuery.ajax/

或者在test1页面中实现test2页面脚本

【讨论】:

    【解决方案2】:

    在生成页面时填写表单。

    $input1 = '<input type="text" name="empid" value="'.$find['empid'].'"><br>';
    ...
    

    或者在页面加载后填写您的表单AJAX and jQuery。

    $.ajax('test2.php')
        .done(function (data) {
            $('#employees input[name="empid"]').val(data.empid);
            ...
        });
    

    【讨论】:

    • GG 我试过但没用。我的表单字段 empid 仍然是空的
    【解决方案3】:

    如果你自己试试这个很好。以下是一些帮助您入门的链接:

    How to pass PHP array values to another file using jQuery Ajax?

    Pass array in jQuery ajax

    Pass PHP Array via jQuery Ajax

    试试吧,如果这些没有帮助,请告诉我们,我们会为您提供帮助。


    好的,在这里,我有一些时间;-)

    test1.php

    <?php
        $empidx="Design-1012-1000";
    $countries = mysqli_query($con,"select country from countries");
    $query = mysqli_query($con,"SELECT * from employee where emprecord='$empidx' ");
    $result_array=mysqli_fetch_array($query);
    
    //$result_array = array("1"=>"1","2"=>"2","3"=>"3");
    $json = json_encode($result_array);
        print_r($json); 
    ?>
    

    test2.php

        <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
    <head profile="http://gmpg.org/xfn/11">
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.js"></script>
    <script type="text/javascript">
    
            $(document).ready(function() {
                $.ajax({
                    type: 'GET',
                    url: 'test1.php',
                    dataType: 'json',
                    success: function (data) {
                        $('#fname').val(data[1]);    
                    }
                });
            }); 
    </script>
    </head>
    <body>
    
    <form id="form">
        <input id="fname"/>
    </form>
    
    
    
    </body>
    </html>
    

    只需配置您传递的 result_array。默认情况下会调用 test2.php 中的脚本,如果这就是你想要的 - 那么它就可以了。否则做一些点击事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多