使用AJAX获取响应 - 但页面仍在刷新

问题描述:

我在填写表单和按提交以将我的信息发送到数据库时遇到问题。问题是,即使我使用AJAX保留在同一页面上并在“show_label”中收到回复,我的页面也会刷新。使用AJAX获取响应 - 但页面仍在刷新

我已经测试过是否通过更改show_label来调用addCustomerFunc,它确实发生了变化!但仍会刷新,并且在刷新后我会丢失信息。我希望如果有人能够抓住我的错误。

这是我customer.php

<script type="text/javascript"> 

function addCustomerFunc(add_LN,add_FN,add_PN,add_DOB) 
{ 
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) 
    { 
    document.getElementById("show_label").innerHTML=xmlhttp.responseText; 
    } 
    } 

xmlhttp.open("POST","addCustomer.php",true); 
xmlhttp.send("add_LN="+add_LN+"&add_FN="+add_FN+"&add_PN="+add_PN+"&add_DOB="+add_DOB); 
} 
</script> 

    <p>Add New Customer:</p> 
    <div align="center"> 
     <table width="337" border="1"> 
     <tr> 
      <td width="154"><p>Last Name:</p> 
      <p>First Name:</p> 
      <p>Phone Number:</p> 
      <p>Date of Birth:</p> 
      <p>&nbsp;</p></td> 
      <td width="167"><p align="center"> 
      <form> 
       <input type="text" name="add_LN" id="add_LN" /> 
       <br/><br/> 
       <input type="text" name="add_FN" id="add_FN" /> 
       <br /><br /> 
       <input type="text" name="add_PN" id="add_PN" /> 
       <br /><br /> 
       <input type="text" name="add_DOB" id="add_DOB" /> 
       <br /><br /> 
       <input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB)"/> 
      </form> 
      <div id="show_label"/> 
      </p> 
      </td> 
     </tr> 
     </table> 
     </div> 

addCustomer.php

<?php 
$username="****"; 
$password="****"; 
$database="****"; 

$lastName=$_POST['add_LN']; 
$firstName=$_POST['add_FN']; 
$phone=$_POST['add_PN']; 
$dob=$_POST['add_DOB']; 
//$membership==$_POST['membership']; 

mysql_connect("******",$username,$password); 

@mysql_select_db($database) or die("Unable to select database"); 


$query = "INSERT INTO customer (last_name, first_name, phone_no, date_of_birth, membership) VALUES ('$lastName', '$firstName', '$phone', '$dob', 'T')"; 

if (mysql_query($query)){ 
    echo "Thanks"; 
} else 
{ 
    echo "Failed to insert customer into database"; 
} 

mysql_close(); 

?>

而且,我是不是正确地传递变量addCustomerFunc? 我是否正确使用“发布”?

谢谢!

编辑:我现在有与传递变量的问题,因为它说: 未定义指数:add_LN在C:\用户\ EJ \桌面\ cpsc471 \演示\ addCustomer.php 它增加了空白到我的数据库,但。

由于您正在使用表单,因此您需要确保在捕获submit事件之前允许它继续并刷新页面。

这应该可以做到。 (注意return false

<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB); return false;"/> 
+0

谢谢! :D它保持在同一页面! ...但现在即时通讯有另一个问题: 未定义的索引:add_LN在C:\ Users \ ej \ Desktop \ cpsc471 \ presentation \ addCustomer.php 此外,它会将空白元素添加到我的数据库,即使我填写字段形式。 我是否应该为此创建一个新问题,因为如果我将您的答案标记为正确的人会在此后忽略此问题? – 2011-04-09 17:19:09

+0

删除'未定义索引'如果php.ini文件更改'ERROR_REPORTING' – John 2011-04-09 18:29:11

+0

未定义索引是警告;这在特定情况下,这意味着姓氏字段为空。您可以使用isset()函数在使用之前检查索引是否存在,或者像大多数人一样检查,并忽略该错误,因为它很小(请参阅ERROR_REPORTING或ERROR_LEVEL)。当然,如果需要姓氏,您需要在那里进行一些数据验证(即,检查姓氏字段是否为空),然后再将任何内容放入数据库。 – Eric 2011-04-09 18:30:10

更改您的按钮,从类型:

submitbutton

这样,你留1页上,并且刷新将会消失:-)

为什么不使用jQuery $.post()$.get()

顺便说一句,使用方法:的

<input type="button" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB)"/> 

代替:

<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(add_LN, add_FN, add_PN, add_DOB)"/>