我现在有一个简单的表格:
<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.presses tab 2.presses enter 3.clicks on a fetch button(fetch button现在不存在,我想知道如何创建它使用 javascript 或其他任何符合我标准的东西)
一旦他执行了上述任何操作,它应该会自动从数据库中生成剩余的字段。查询如下所示: $qry=mysql_query("SELECT * from user where username = $_POST['username']
"); $row=mysql_fetch_assoc('$qry); 回声 $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;