2
  <script type="text/javascript"src="prototype.js"></script>
<script type="text/javascript">
//<![CDATA[
document.observe("dom:loaded", function() {

    function sendRequest() {

        var oform = document.forms[0];
        var sBody = getRequestBody(oform);

        var oOptions = {
            method: "post",
            parameters: sBody,
            onSuccess: function (oXHR, oJson) {
                saveResult(oXHR.responseText);
            },
            onFailure: function (oXHR, oJson) {
                saveResult("An error occurred: " + oXHR.statusText);
            }
        };

        var oRequest = new Ajax.Request("edit_status.php", oOptions);      
    }

    function saveResult(sMessage) {
        var divStatus = document.getElementById("divStatus");
        divStatus.innerHTML = "Request completed: " + sMessage;            
    }


    });
//]]>
</script>

我是ajax的新手。我手头有一个真正需要大量 ajax 功能的项目。我正在从我买的一本书中遵循上面的代码。当我在本地服务器上复制此代码时,当我单击提交按钮时,ajax.request 函数不起作用。它带我直接进入 php 页面。请问有人可以帮我看看这个吗?

**

<form method="post" action="SaveCustomer.php" 
      onsubmit="sendRequest(); return false">
<p>Enter customer information to be saved:</p>
<p>Customer Name: <input type="text" name="txtName" value="" /><br />
Address: <input type="text" name="txtAddress" value="" /><br />
City: <input type="text" name="txtCity" value="" /><br />
State: <input type="text" name="txtState" value="" /><br />
Zip Code: <input type="text" name="txtZipCode" value="" /><br />
Phone: <input type="text" name="txtPhone" value="" /><br />
E-mail: <input type="text" name="txtEmail" value="" /></p>

</form>
<div id="divStatus"></div>

**

**

header("Content-Type: text/plain");

//get information
$sName = $_POST["txtName"];
$sAddress = $_POST["txtAddress"];
$sCity = $_POST["txtCity"];
$sState = $_POST["txtState"];
$sZipCode = $_POST["txtZipCode"];
$sPhone = $_POST["txtPhone"];
$sEmail = $_POST["txtEmail"];

//status message
$sStatus = "";

//database information
$sDBServer = "localhost";
$sDBName = "ajax";
$sDBUsername = "root";
$sDBPassword = "";


//create the SQL query string
$sSQL = "Insert into Customers(Name,Address,City,State,Zip,Phone,`Email`) ".
          " values ('$sName','$sAddress','$sCity','$sState', '$sZipCode'".
          ", '$sPhone', '$sEmail')";

$oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
@mysql_select_db($sDBName) or $sStatus = "Unable to open database";

if ($sStatus == "") {
    if(mysql_query($sSQL)) {
        $sStatus = "Added customer; customer ID is ".mysql_insert_id();
     } else {
        $sStatus = "An error occurred while inserting; customer not saved.";
    }
}
mysql_close($oLink);

echo $sStatus;

?> **

4

1 回答 1

0

你没有触发 ajax 我看到你定义了选项但那是它尝试使用 jquery 你可以等待表单提交

$('your form').on('submit', function(event){
    event.preventDefault();
    $.ajax({
        url:'your url',
        type:'post',
        data:'your data',
        success:function(data, jxhr){
            //your success function
        },
        error:function(){}
    });
});

e.preventDefault() 防止同步提交触发默认方法

查看您的代码,可以将 sendRequest() 更改为 sendRequest(event),然后添加 event.preventDefault。我总是遇到 return false 的问题

于 2014-12-07T00:10:55.547 回答