php 使用php自动填写html表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13324696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
auto fill html form using php
提问by atiquratik
I have a HTML form which has 3 text input fields: id, name, and contact. My DB table has same named columns and the values are id=1, name=mark& contact=1234. Now, if I enter '1' in my html form's id field & press enter then how can the rest of the form's field (like: name, contact) will be automatically filled?
我有3个文本输入框HTML表单:id,name,和contact。我的数据库表具有相同命名的列,值为id=1, name=mark& contact=1234。现在,如果我在 html 表单的 id 字段中输入“1”并按 Enter,那么表单的其余字段(例如:name,contact)将如何自动填充?
What I've done so far is:
到目前为止我所做的是:
my index.htmlfile(I think its wrong because I didn't use JSON before):
我的index.html文件(我认为这是错误的,因为我之前没有使用 JSON):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type = javascript>
$("#email").bind("change", function(e){
$.getJSON("lookup.php?email=" + $("#email").val(),
function(data){
$.each(data, function(i,item){
if (item.field == "first_name") {
$("#first_name").val(item.value);
} else if (item.field == "last_name") {
$("#last_name").val(item.value);
}
});
});
});
</script>>
</head>
<body>
<form>
<input type="text" name="email" id="email" />
<input type="text" name="first_name" id="first_name" />
<input type="text" name="last_name" id="last_name" />
</form>
</body>
</html>
my lookup.phpfile:
我的lookup.php文件:
<?php
//look up the record based on email and get the firstname and lastname
$email=$_GET['$email'];
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("json", $con); //DB name= json
mysql_select_db("json"); //DB name= json
$result = mysql_query("SELECT * FROM json1
WHERE email LIKE '$email%'"); //DB table name=json1
if (mysql_num_rows($result) == 1) {
while ($myrow = mysql_fetch_array($result)) {
//build the JSON array for return
$json = array(array('field' => 'first_name',
'value' => $firstName),
array('field' => 'last_name',
'value' => $last_name));
echo json_encode($json );
}
}
?>
回答by SaidbakR
Try using some php MVC framework, like Codeigniter, cakephp, yii.
尝试使用一些 php MVC 框架,如 Codeigniter、cakephp、yii。
回答by Mohd Moe
To fill the form without reloading the page you will have to use Ajax to request the data from the server (database) and then fill it using javascript, i suggest reading about ajax functions with jquery.
要在不重新加载页面的情况下填写表单,您必须使用 Ajax 从服务器(数据库)请求数据,然后使用 javascript 填写它,我建议阅读有关使用 jquery 的 ajax 函数的信息。
if you want for example to fill the id and click a button and the page will reload with the data, use the php post method, a quick example:
例如,如果您想填写 id 并单击一个按钮,页面将重新加载数据,请使用 php post 方法,一个快速示例:
<?php
if($_POST['getdata']){
$id = $_POST['itemID'];
/*
connect to the database and get the data here and return in with an array called $data for example
*/
}
if($_POST['savedata']){
/*
use this if you want to do another action to the form, update the values for example
*/
}
?>
<form action='' method='POST'>
<input name="id" value="<?php echo $data['id'];?>" type="text" />
<input name="name" value="<?php echo $data['name'];?>" type="text" />
<input name="contact" value="<?php echo $data['contact'];?>" type="text" />
<input name="getdata" value="Get Data" type="submit" />
<input name="savedata" value="Save Data" type="submit" />
</form>
UPDATE:
更新:
regarding your update
关于你的更新
first of all you better use PDO or MySQLi database instead of MySQL function because mysql_* is no longer maintained so since you are fresh to php learn PDO instead.
首先,您最好使用 PDO 或 MySQLi 数据库而不是 MySQL 函数,因为 mysql_* 不再维护,因此您刚开始学习 php 学习 PDO。
to your code, if you are expecting a single row from the database select statement, you should not use while()because its used to loop through the mysql object which has more than one row.
对于您的代码,如果您希望从数据库 select 语句中获得一行,则不应使用while()它,因为它用于循环遍历具有多行的 mysql 对象。
if we assume your table has exactly email, firstname, lastnamefetch it directly into an array:
如果我们假设您的表已将email, firstname, lastname其直接提取到数组中:
$myrow = mysql_fetch_assoc($result);
then convert that array into json and print it out
然后将该数组转换为 json 并打印出来
echo json_encode($myrow);
exit();

