dongzhuner6981 2015-03-03 07:12
浏览 42

提交按钮不会触发ajax函数[重复]

This question already has an answer here:

i had some problem with my code where i'm trying to separate my form function to another php files and use ajax function to call the form depend on the ids. But however after i separate the form function, the submit button can't call the ajax function.

Here is the sponsor.php sample code where i use to trigger the ajax function and show up the form from another php into sponsor.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>

After the result return success then it will show the form in the here:

<div id="txtHint"><b>Please Select Sponsor Edit Button To Start.</b></div>

Whereby here is the form that retrieve from getSponsorForm.php in order to show in the :

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

When i do so, everything is working except once the form is showing up and i filled up the input field and want to press the submit button, it just won't trigger the ajax function on sponsor.php which is here:

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

            });

Anything i did wrong? Please guide me through.Thanks :)

</div>
  • 写回答

1条回答 默认 最新

  • dongshan1811 2015-03-03 07:19
    关注

    Try this to serialize form data http://api.jquery.com/serialize/ and then

    $.get('/url', serializedData, callback);
    

    That:

    $( "form#form" ).on( "submit", function( event ) {
      event.preventDefault();
      var data = $(this).serialize();
      $.post('sponsorItem.php', data, function(response){
         $('#txtHint').text(response);
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大