0

I have the following Ajax code within jQuery that uses a save.php page to save data from a form to the MySQL database:

    $('#frmSurvey').append("<input type='hidden' name='page' value='"+$page+"' />"); // NOTE: this is done before the submit.

    document.frmSurvey.submit(); // There is some validation done and then the submit is called

    var url = "save.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#frmSurvey").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

And the HTML:

<form id="frmSurvey" name="frmSurvey" action="save.php" method="post" onsubmit="return false">

Hope that all makes sense.

I understood that the Ajax call to the save.php page would mean that the users doesn't see the page and it happens in the background. However, the save.php page still appears in the web address bar for a second or two - is this correct - any way to stop this happening?

4

1 回答 1

1

导航到另一个 URL 的发生是因为您实际上是在 AJAX 调用之前执行正确的表单提交document.frmSurvey.submit();——实际上这会阻止 AJAX 请求的发生,因为导航到表单操作 URL 会停止从当前页面处理 JS。

如果您删除该行,您的表单将通过 AJAX 提交而无需任何重定向。

于 2013-08-09T20:29:34.303 回答