HTML和PHP中的Onchange

时间:2014-06-19 17:30:08

标签: javascript php jquery html ajax

我是新手,请帮我这个.. 我只能显示名称,但我必须在索引页面中显示年龄,dob,因为我已创建输入表单

在check.php中我只能发送一个数据,所以我如何发送年龄,dob以及如何在索引页中获取

的index.php

<html>
<head>
<script type='text/javascript'>
function validate(field)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() // Checking if readyState changes
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) // Validation Completed
{
document.getElementById("name").value = xmlhttp.responseText;
}
}
xmlhttp.open("GET","check.php?field="+field+, false);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="gt" id="gt" action="Form_Action.php" method="post" >
<table>
<tr>
<td>Userid</td>
<td><input type='text' name='userid' id='userid' onchange="validate( this.value)"></td>
<td></td></tr>
<tr><td>name</td>
<td><input type='text' name='name' id='name' ></td>
<td></td>
</tr>
<tr><td>age</td>
<td><input type='text' name='age' id='age'></td>
<td></td>
</tr>
<tr><td>dob</td>
<td><input type='text' name='dob' id='dob'></td>
<td></td>
</tr>
</table>
<input type='submit' value='Submit'>
</form>
</body>
</html>

check.php

<?php
include('connect.php');

$field= $_GET['field'];

$res=mysql_query("Select userid,name,age,dob from user where userid='$field' ");
while($row=mysql_fetch_row($res))
{
echo "$row[0]";
}
?>

1 个答案:

答案 0 :(得分:0)

I don't know if i understood you..without javascript you can simply do as follows:

<强>的index.php

 <html>
    <head>
        <title>whatever</title>
    </head>
<body>
 <form name="gt" id="gt" action="check.php" method="post" >
    <!-- notice the form action is in the check.php make sure you give correct location-->
    <table>
    <tr>
        <td>Userid</td>
        <td><input type='text' name='userid' id='userid' ></td>
        <td></td></tr>
    <tr><td>name</td>
        <td><input type='text' name='name' id='name' ></td>
        <td></td>
    </tr>
    <tr><td>age</td>
        <td><input type='text' name='age' id='age'></td>
        <td></td>
    </tr>
    <tr><td>dob</td>
        <td><input type='text' name='dob' id='dob'></td>
        <td></td>
    </tr>
    </table>
 <input type='submit' value='Submit'>
 </form>
</body>
</html>

<强> check.php

<?php
$userid = $_POST['userid'];//since the form has post method in index.php
$name = $_POST['name'];
$age = $_POST['age'];

//You can either put it to database by mysql query or just display as below
echo $userid;
echo "<br/>";
echo $name;
echo "<br/>";
echo $age;
echo "<br/>";
?>