使用php自动填充html表单

时间:2012-11-10 18:09:25

标签: php html mysql

我有一个HTML表单,其中包含3个文本输入字段:idnamecontact。我的数据库表具有相同的命名列,值为id=1name=mark& contact=1234。现在,如果我在我的html表单的id字段&中输入'1'按Enter键,表单的其余部分(如:namecontact)将如何自动填充?

到目前为止我所做的是:

我的 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>

我的 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 );
        }
}
?>

2 个答案:

答案 0 :(得分:1)

尝试使用一些php MVC框架,如Codeigniter,cakephp,yii。

答案 1 :(得分:1)

要填写表单而不重新加载页面,您将不得不使用Ajax从服务器(数据库)请求数据,然后使用javascript填充它,我建议用jquery阅读有关ajax函数的内容。

如果你想要例如填充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>

更新:

关于您的更新

首先,你最好使用PDO或MySQLi数据库而不是MySQL函数,因为不再维护mysql_ *,因为你刚刚开始学习PDO而不是。

到你的代码,如果你期望数据库select语句中有一行,你不应该使用while(),因为它用于遍历具有多行的mysql对象。

如果我们假设您的表格完全email, firstname, lastname将其直接提取到数组中:

$myrow = mysql_fetch_assoc($result);

然后将该数组转换为json并将其打印出来

echo json_encode($myrow);
exit();
相关问题