无法调用ajax函数

Unable to call ajax function

本文关键字:函数 ajax 调用      更新时间:2023-09-26

单击提交按钮时,我在调用ajax函数时遇到了一些困难。当我尝试用其他形式对ajax函数进行故障排除时,它工作得很好,但当我尝试将数据插入数据库的实际代码时,ajax函数就是无法调用。为了让它发挥作用,我需要改变什么?请引导我,谢谢。

下面是我的sponsor.php示例:

//File and text upload with formDATA function
                $("form#form").submit(function(){
                var formData = new FormData($(this)[0]);    
                    $.ajax({
                        url:'sponsorItem.php',
                        type: 'POST',
                        data: formData,
                        async: false,
                        beforeSend: function(){
                         if(confirm('Are you sure?'))
                              return true;
                          else
                              return false;
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                    }).done(function () {
                            //do something if you want when the post is successfully
                            if(!alert('Banner Had Successfully Updated.')){document.getelementbyclassname('form').reset()}
                        });
                    return false;
            });

其他地方是getSponsorForm.php,它从ajax中获取项目id,ajax将值传递给它以调用表单。

以下是将变量传递给getSponsorForm.php的函数:

<!-- Ajax show form function-->
<script>
function showSponsor(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getSponsorForm.php?sponsor="+str,true);
        xmlhttp.send();
    }
}
</script>

这是getSponsorForm.php:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$q = intval($_GET['sponsor']);
include 'dbConnection.php';
global $dbLink;

$query="SELECT * FROM sponsor_item WHERE sponsor_item_id = '".$q."'";
$result = $dbLink->query($query);

// Numeric array
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<!--Banner Item No 1 Start-->
                            <div class="box box-primary1">
                                <div class="box-header">
                                    <h3 class="box-title">Edit Sponsor No.'.$row["sponsor_item_id"].' <small>编辑器</small></h3>
                                </div>
                                <div class="box-body">
                                <form class="form" id="form" action="" method="POST" enctype="multipart/form-data">
                                    <div class="box-body">
                                        <input type="hidden" name="sponsor_id" value="'.$row["sponsor_item_id"].'"></input>
                                        <div class="form-group" >
                                            <label for="sponsorTitle">Sponsor Title 赞助称号</label>
                                            <input type="text" class="form-control" name="sponsorTitle" id="sponsorTitle" placeholder="Please Enter Name" onChange="checkDisabled(testing);">
                                        </div>
                                        <div class="form-group" >
                                            <label for="sponsorDescription">Sponsor Description 赞助描述</label>
                                            <input type="text" class="form-control" name="sponsorDescription" id="sponsorDescription" placeholder="Please Enter Name" onChange="checkDisabled(testing);">
                                        </div>
                                         <div class="form-group">
                                            <label for="exampleInputFile">File input</label>
                                            <input type="file" id="uploaded_file" name="uploaded_file"  onChange="checkDisabled(testing);"><br>
                                            <p class="help-block">Your picture size not more than 2MB.  (Only JPEG or JPG is allowed)</p>
                                        </div>  
                                        <div class="checkbox">
                                            <button id="testing" type="submit" class="btn btn-primary" disabled>Update</button>      
                                        </div>
                                    </div><!-- /.box-body -->

                                </form>                  <!-- Date range -->
                                    <!-- /.form group -->
                                    <!-- Date and time range -->


                                    <!-- /.form group -->
                                    <!-- Date and time range -->
                                    <!-- /.form group -->
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                            <!--Banner Item No 1 End-->';
}       
mysqli_free_result($result);                            
// Close the mysql connection
$dbLink->close();
?>
</body>
</html>

我不知道为什么我点击提交按钮,它只是不会调用sponsorItem.php中的函数并将数据插入数据库。

仅供参考:我曾尝试将表单的getSponsorForm.php代码放入sponsor.php中,然后当我单击提交按钮时,它就可以工作了。但我把getSponsorForm.php中的表单函数分离出来,它就不起作用了。有线索吗?

如果手动从表单字段中获取值,然后构造一个数据字符串发送到PHP处理器文件sponsorItem.php ,您可能会获得更好的成功

请注意,根据您的代码,sponsorItem.php将接收表单数据并将其插入数据库。由于这是不起作用的部分,您应该在问题中显示该代码。

通常,数据以text/html格式发送到PHP处理器文件,而不是作为任何类型的对象。因此,应该可以访问alert(FormData)并查看您发送到sponsorItem.php的字符串。我认为你做不到。

看看这三个简单的AJAX示例,看看如何更改代码,可能会对您非常有帮助。

使用ajax获取内容并直接显示,无需刷新页面

首先,手动获取表单数据值(例如var userName = $('#username').val();,而不是尝试一次自动全局化所有内容。既然你在学习,就去控制,而不是自动化。


由于sponsorItem.php正在工作,那么您需要查看AJAX代码块是否正在工作。

暂时将sponsorItem.php的内容仅替换为以下两行:

<?php
echo 'Got to sponsorItem page';

现在,更改您的AJAX代码块如下:

}).done(function (data) {
    alert( data );
});

data表示从sponsorItem.php回显的任何数据,因此它应该提醒Got to sponsorItem page

当它工作时,你就会知道你的AJAX正在工作。