从javascript和php获取价值在javascript中返回两个值
问题描述:
I'm a newbie in JAVASCRIPT and PHP. I'm sorry if this question is already solve in other problem. I just need the link that disame in my problem.This is my code:
All I want is their a code that xmlhttp.responseText
return two value of my empnum
and fname
to their respective td id="erroremonum"
and td ="errorfname"
.
All I get in this only the combination of two echo.
My table in index.php
:
<tr>
<td >Employee Number</td>
<td>:</td>
<td width="150px"><input type="text" name="empnum" id="empnum" maxlength="4"></td>
</tr>
<tr><td></td><td></td><td id="errorempnum"></td></tr>
<tr>
<td >Firstname</td>
<td>:</td>
<td width="150px"><input type="text" name="fname" id="fname" ></td>
</tr>
<tr><td></td><td></td><td id="errorfname"></td></tr>
<tr>
<td><center><button id="submit" style="margin-top:20px" onClick="reg()">Submit</button></center></td>
<td colspan="2"><button id="cancel" style="margin-top:20px">Cancel</button></td>
</tr>
In my script:
function reg(empnum1)
{
var empnum = document.getElementById("empnum").value;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
if(xmlhttp.responseText)
{
document.getElementById("errorempnum").style.color = "Red";
document.getElementById("errorempnum").innerHTML= "*Employee number is exist!";
}
else
{
document.getElementById("errorempnum").innerHTML="";
}
}
}
xmlhttp.open("GET","checkempnum.php?empnum1="+checkemp+"&fname1="+fname,true);
xmlhttp.send();
}
Check empnum
or fname
is exist in database check.php
:
$empnum = intval($_GET['empnum1']);
$empnum = mysql_real_escape_string($empnum);
$fname = strval($_GET['fname1']);
$fname = mysql_real_escape_string($empnum);
$checkempnum= mysql_query("SELECT * FROM systemuser WHERE employeenumber='{$empnum}'");
$num_rows = mysql_num_rows($checkempnum);
if($num_rows == 1)
{
echo "employee number exist";
}
$checkfname= mysql_query("SELECT * FROM systemuser WHERE firstname='{$fname}'");
$num_rows1 = mysql_num_rows($checkfname);
if($num_rows1 == 1)
{
echo "firstname exist";
}
答
Give your form an id of for example id="myForm"
and use Jquery ajax.
$('#myForm').submit(function(e) {
e.preventDefault();
var empid = $("#empnum").val();
var fname = $("#fname").val();
dataGet = 'empnum='+empid+'&fname='+fname;
$.ajax({
type:'GET',
data:dataGet,
url:'checkempnum.php',
success:function(data) {
$("#errorempnum").html(data);
}
});
});