如何使用 PHP 在点击时自动填充其他表单字段

发布于 2024-12-28 23:19:55 字数 2479 浏览 1 评论 0原文

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

<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.按下Tab键 2.按回车键 3.单击获取按钮(获取按钮现在不存在,我想知道如何使用javascript或任何其他适合我的标准的东西创建它)

一旦他执行上述任何操作,它应该自动从数据库。 查询如下所示: $qry=mysql_query("从用户中选择*,其中用户名=$_POST['用户名']"); $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;

I have a simple form right now:

<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" />

What I need is, when the user fills his username and does one of the following:
1.presses tab
2.Presses enter
3.clicks on a fetch button(fetch button does not exist now,i would like to know how to create it using javascript or anything else that suits my criteria)

Once he does any of the above it should automatically generate the remaining fields from the database.
The query would look like this:
$qry=mysql_query("SELECT * from user where username =$_POST['username']");
$row=mysql_fetch_assoc('$qry);
echo $row['posts] and so on..

After it is autogenerated he can edit the fields and submit to update the database fields.

Here is my updated code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2025-01-04 23:19:55

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

当第一个字段失去焦点或更改(或按下按钮)时,通过 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();
    });
});

Make a php endpoint (basically a url in which you can call your special php function, which returns only the data you want) that returns a json_encode'd array with all of the other fields' info.

Call that function via ajax when the first field either loses focus or changes (or a button is pressed), depending on your preference.

Then using javascript, use what you've gotten back from the ajax response to populate the other fields.

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 (using jquery)
//gets the val from the first field

$(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();
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文