2

我正在尝试使用 ajax 在后台导航到一个 php 脚本,以便它执行一些命令,但问题是它没有执行命令,这告诉我我的 ajax 不正确或者我做错了它没有调用ajax。你能看看代码并说明我做错了什么吗?我知道 php 脚本应该导航到insertQuestion.php确实有效,因为如果我使用表单操作导航到该页面,那么它会执行该脚本中的命令,但是当我尝试使用 ajax 在后台导航到脚本时,它只是不这样做:

代码:

<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return myClickHandler()">

....

</form>

 function myClickHandler()
    {
        if (!validation())
            return false;

        if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
            return false;

        $.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false
        });

        return true;
    }

更新:

它仍然没有导航到 insertQuestion.php 页面,现在的问题是它没有检查验证并且没有在 javascript 函数中显示确认框。下面我更新了当前代码现在的样子:

<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" onsubmit="return myClickHandler(); return false;">

....

</form>

         <script type="text/javascript">

    function myClickHandler()
    {
        if (!validation())
            return false;

        if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
            return false;

       $.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false
            type: "POST"
        });

        return true;
    }

</script>
4

2 回答 2

0

使用类型:“POST”,因为

type String 默认值:'GET' 请求的类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他 HTTP 请求方法,例如 PUT 和 DELETE,但并非所有浏览器都支持。

.ajax

$.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false,
            type: "POST"
        });

关于提交:HTML:

<form id="QandA">
...
</form>

JS:

$(function() {
myClickHandler=function(e)
    {
        if (!validation())
            return false;

        if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
            return false;

        $.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false,
            type: "POST"
        });

        return true;
    };



$('#QandA').submit(myClickHandler);



});
于 2012-11-01T03:54:03.613 回答
-1

请删除 htmlentities()

<form id="QandA" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return myClickHandler()">

....

</form>

或者通过添加“return false;”尝试另一种方式 提交。

<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return myClickHandler(); return false;">

....

</form>
于 2012-11-01T03:31:11.787 回答