【问题标题】:How to autofill other form fields upon click with PHP如何在点击 PHP 时自动填充其他表单字段
【发布时间】:2012-02-18 21:32:15
【问题描述】:

我现在有一个简单的表格:

<form action='<? echo $PHP_SELF;?>' method='POST'>
Username:<input type='text' name='username'><br>
Email:<input type='text' name='email'><br>
Posts:<input type='text' name='posts'><br>
Joindate<input type='text' name='join'><br>
<input type="submit" value="Submit" />

我需要的是,当用户填写他的用户名并执行以下操作之一时: 1.按标签 2.按回车 3.点击获取按钮(获取按钮现在不存在,我想知道如何使用 javascript 或其他任何适合我的标准来创建它)

一旦他执行了上述任何操作,它应该会自动从数据库中生成剩余的字段。 查询将如下所示: $qry=mysql_query("SELECT * from user where username =$_POST['username']"); $row=mysql_fetch_assoc('$qry); echo $row['posts] 等等..

自动生成后,他可以编辑字段并提交以更新数据库字段。

这是我更新的代码:

<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>

</head>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="POST">
<fieldset>
<legend>Form</legend>
<label for="username">Username: </label>

<input type="text" id="username"  name="username" /> 
<button onclick="myrequest()">fetch</button>
<label for="posts">Posts: </label>
<input type="text" id="posts" name="posts"  size="20"/>
<label for="joindate">Joindate: </label>
<input type="text" id="joindate" name="joindate"  size="20"/>



<p><input type="submit" name="submitBtn" value="Submit" /></p>

</fieldset>
</form>
<script type="javascript/text>
function myrequest() {
var name = $('.username').val();
$.ajax({
  method: "GET",
  url: "http://url.com/test/autofill.php",
  dataType: 'json',
  data: {
    username: username
  }).success(function( responseObject ) {
    // assuming you have an array of stuff like name, id, email, etc.
    // now i populate another field from the ajax response
    $('.posts').val( responseObject.posts );
  });

} 
And then in the autofill.php

    //connect to database
    $name = $_GET['username'];
    $return = mysql_query("SELECT * FROM user WHERE username = '$name' LIMIT 1");
    $rows = mysql_fetch_array($return);
    $formattedData = json_encode($rows);
    print $formattedData;

【问题讨论】:

    标签: php autofill


    【解决方案1】:

    创建一个 php 端点(基本上是一个 url,您可以在其中调用您的特殊 php 函数,它只返回您想要的数据)返回一个包含所有其他字段信息的 json_encode 数组。

    当第一个字段失去焦点或更改(或按下按钮)时,通过 ajax 调用该函数,具体取决于您的偏好。

    然后使用 javascript,使用您从 ajax 响应返回的内容来填充其他字段。

    autoFill.php

    $name = $_GET['name'];
    $return = mysql_query("SELECT * FROM someTable WHERE username = '$name' LIMIT 1");
    $rows = mysql_fetch_array($return);
    $formattedData = json_encode($rows);
    print $formattedData;
    

    Javascript(使用 jquery) //从第一个字段获取val

    $(document).ready(function() {
        function myrequest(e) {
            var name = $('.username').val();
            $.ajax({
                method: "GET",
                url: "/echo/json/", /* online, change this to your real url */
                data: {
                    username: name
                },
                success: function( responseObject ) {
                    alert('success');
                    $('#posts').val( 'posts' );
                    $('#joindate').val('testing join date');
                    /*
                    once you've gotten your ajax to work, then go through and replace these dummy vals with responseObject.whatever
                    */
                },
                failure: function() {
                    alert('fail');
                }
            });
        }
    
        $('#fetchFields').click(function(e) {
            e.preventDefault();
            myrequest();
        });
    });
    

    【讨论】:

    • 哇……这对我来说很新鲜……你能提供一些例子吗?我试着用谷歌搜索它,但无法想出对我有帮助的东西。
    • 谢谢,在 php 方面我必须调用这样的东西吗?
      用户名:
    • 不——在你刚刚提供的代码中,你有 method="GET" ,它基本上告诉浏览器在页面刷新时使用什么 http 方法。 Ajax 还需要知道这一点,因为它不会在此过程中进行页面刷新。查看我上面的 JS 代码,上面写着“方法:“GET””
    • 我再次更新了上面的代码..这次没有错误..但是页面只是刷新而没有返回任何内容..
    • 请检查您的标记是否有错误。我看到你写的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2021-05-25
    相关资源
    最近更新 更多