Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJax未正确调用php文件、html表单_Javascript_Php_Ajax - Fatal编程技术网

Javascript AJax未正确调用php文件、html表单

Javascript AJax未正确调用php文件、html表单,javascript,php,ajax,Javascript,Php,Ajax,更新: 这就是错误: 412 (Precondition Failed) 我试图从ajax调用一个php脚本,我现在有下面的ajax,当点击表单中的按钮(也在下面)时,它将调用一个php脚本传递表单数据,然后将表单数据提交到数据库 但是,它不起作用;更重要的是,我只是得到了一个空白的错误,所以我甚至不知道哪里出了问题 能找个人给我指一下正确的方向吗 谢谢 HTML格式: <form name="report-form" id="report-form" action="" method

更新: 这就是错误:

412 (Precondition Failed) 
我试图从ajax调用一个php脚本,我现在有下面的ajax,当点击表单中的按钮(也在下面)时,它将调用一个php脚本传递表单数据,然后将表单数据提交到数据库

但是,它不起作用;更重要的是,我只是得到了一个空白的错误,所以我甚至不知道哪里出了问题

能找个人给我指一下正确的方向吗

谢谢

HTML格式:

<form name="report-form" id="report-form" action="" method="POST">              
                <textarea id="reason-box" type="text" name="reason-box" cols="40" rows="5" maxlength="160" onkeypress=""></textarea>    
                <input id="reportedID" name="reportedID" type="text" />
                <!--<input id="report-submit" value="" name="submit" type="submit" onclick="submitReport()"/>   -->
                <button id="report-submit" name="submit" onclick="submitReport()"></button>

            </form>

现在我已经测试了php脚本,效果很好,问题是从添加ajax调用时开始的,因此我知道这与ajax有关,而不是与php有关。

这应该可以纠正提交时的问题:

  • jQuery Ajax调用不会成功,因为
    POST
    数据的格式不正确
  • 如果ajax成功,表单也会被发布,导致
    405
    错误
  • 
    
    函数提交端口(事件)
    {           
    event.preventDefault();
    ……您的代码
    }
    
    现在将阻止表单的默认操作(导致405错误)。并且只提交ajax请求

    在按钮元素中,我们将
    事件
    对象传递给函数。我们使用
    event.preventDefault()
    确保按钮不会运行其默认操作,即提交表单

    您也可以通过删除作为包装器的表单元素来防止这种情况,但是您可能希望在表单上使用其他功能(如验证)

    jQuery ajax请求中的表单数据需要是一个名为data的对象:

    var formData = {"ID" : ID, "reason" : reason};
    

    jQuery将为提交将其转换为正确的查询字符串。

    我将这样做:

    <form name="report-form" id="report-form" action="" method="POST">              
          <textarea id="reason-box" type="text" name="reason-box" cols="40" rows="5" maxlength="160"></textarea>    
          <input id="reportedID" name="reportedID" type="text" />
          <button id="report-submit" type="submit" name="submit" value="submit"></button>
    </form>
    
    <script type="text/javascript">
    jQuery("document").ready(function(){
        var $ = jQuery
        $("form").submit(function(){
            var data = "";
            data = $(this).serialize() + "&" + $.param(data);
    
            $.ajax({
                type: "POST",
                url: "submit_report.php", 
                data: data,
                 success: function(data)
                   {                     
                     alert("Report Submitted!");
                   },           
                   error: function(xhr,err)
                   {                     
                      alert(err.message);
                        alert("responseText: "+ xhr.responseText);
                   }
            });
            return false;
        });
    });
    </script>
    
    
    jQuery(“文档”).ready(函数(){
    var$=jQuery
    $(“表格”)。提交(函数(){
    var数据=”;
    data=$(this.serialize()+“&”+$.param(data);
    $.ajax({
    类型:“POST”,
    url:“submit_report.php”,
    数据:数据,
    成功:功能(数据)
    {                     
    警报(“报告已提交!”);
    },           
    错误:函数(xhr,err)
    {                     
    警报(错误消息);
    警报(“responseText:+xhr.responseText”);
    }
    });
    返回false;
    });
    });
    

    然后使用
    $reason=$\u POST['reason-box']
    $ID=$\u POST['reportedID']在您的PHP脚本中

    选择提交数据的表单是可选的,或者您可以不使用HTML表单进行提交这就是我要做的

     <textarea id="reasonbox" type="text" name="reason-box" cols="40" rows="5" maxlength="160" onkeypress=""></textarea>    
    <input id="reportedID" name="reportedID" type="text" />
     <button id="report-submit"  ></button>
    
    
    
    并使用下面的javascript和jquery风格

    <script type="text/javascript">
    $(function() {
    
     $("#report-submit").click(function(){
                try
                {
                    $.post("your php page address goes here like /mypage.php",
                        {
    //in this area you put the data that is going to server like line below
                            'reasonbox':$("#reason-box").val().trim(),
                            'reportedID':$("#reportedID").val().trim()
    
                        }, function(data){
                            data=data.trim();
    //this is data is sent back from server you can send back data that you want
    //like message or json array
                        });
                }
                catch(ex)
                {
                    alert(ex);
                }
            });
    
    });
    </script>
    
    
    $(函数(){
    $(“#报告提交”)。单击(函数(){
    尝试
    {
    $.post(“您的php页面地址类似于/mypage.php”,
    {
    //在这个区域中,您将要发送到服务器的数据放在下面的行中
    '原因框':$(“#原因框”).val().trim(),
    'reportedID':$(“#reportedID”).val().trim()
    },函数(数据){
    data=data.trim();
    //这是从服务器发回的数据,您可以发回所需的数据
    //比如消息或json数组
    });
    }
    捕获(ex)
    {
    警报(ex);
    }
    });
    });
    

    我希望这能有所帮助

    你又犯了什么错误?404?a 500?这是问题之一,我得到了一个空白错误。不确定是否错误:部分中的代码。对于故障排除,请将成功功能设置为:
    console.log(数据)
    并将错误函数设置为:
    console.log(xhr);控制台日志(err)然后查看控制台输出您可以发布submit_report.php吗?即使它在您的内部测试期间起作用,我们仍然需要查看它返回了什么。另外,在浏览器中点击control+shift+i并转到控制台,报告错误。首先,在提交代码中使用
    event.preventDefault()
    ,以防止提交表单。您好,感谢您的回复。不幸的是,这并没有解决这个问题。在我的示例中,语法是什么样子的?对不起?嗯,这似乎也不起作用。它表示成功,但实际上并没有提交查询。但是也没有sql错误,所以我知道它不是sql。它仍然可以在没有“事件”作为参数的情况下工作,但是为了易读性,最好使用这种方法。是的,我说成功了,但是不再调用php脚本。
    <script type="text/javascript">
    $(function() {
    
     $("#report-submit").click(function(){
                try
                {
                    $.post("your php page address goes here like /mypage.php",
                        {
    //in this area you put the data that is going to server like line below
                            'reasonbox':$("#reason-box").val().trim(),
                            'reportedID':$("#reportedID").val().trim()
    
                        }, function(data){
                            data=data.trim();
    //this is data is sent back from server you can send back data that you want
    //like message or json array
                        });
                }
                catch(ex)
                {
                    alert(ex);
                }
            });
    
    });
    </script>