user2781149
user2781149

Reputation: 29

ajax request function does not work when its called

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

I am new to ajax. i have a project at hand that really need a lot of ajax functionality. I am following this above code from a book i bought. when i copy this code on my local server, the ajax.request function is not working when i click the submit button. It takes me straight to the php page. Please can someone help me look at this?

**

<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;

?> **

Upvotes: 2

Views: 349

Answers (1)

Snymax
Snymax

Reputation: 357

you arent firing the ajax i see you define the options but thats it try using jquery u can wait for form submission

$('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(){}
    });
});

the e.preventDefault() prevents the synchronous submission from firing default methods

looking at your code the sendRequest() can be changed to sendRequest(event) then add the event.preventDefault. I always have issues with return false

Upvotes: 0

Related Questions