Ajax无法提交表单并调用PHP代码

时间:2015-03-12 06:10:03

标签: php ajax

我在PHP代码上解决了我的ajax调用遇到了一些困难。 当我使用action =“bannerDeleteItem.php”对代码进行硬编码时,我能够调用bannerDeleteItem.php,但是当我尝试使用ajax调用转到bannerDeleteItem.php时,它只是刷新页面。我的代码中是否有任何错误?如果我错了,请纠正我,因为我还不熟悉ajax功能。谢谢

这是我的banner.php代码:

<tbody>

                                        <?php

                                            include 'dbConnect.php';
                                            global $conn;

                                            $query = "SELECT * FROM I_Banner WHERE banner_kiosk ='1' ";
                                            $result = sqlsrv_query($conn,$query);

                                            while( $row = sqlsrv_fetch_array ( $result, SQLSRV_FETCH_ASSOC )){
                                        ?>
                                        <tr>
                                                    <td><?php echo $row['banner_name']; ?></td>
                                                    <td><?php echo $row['banner_description']; ?></td>
                                                <form action="" method="post" enctype="multipart/form-data">
                                                    <input type="hidden" name="banner_id" value="<?php echo $row['banner_id']?>"> 
                                                    <td><center/><button id="delete" class="btn btn-info btn-sm" title="Remove"><i class="fa fa-times"></td>
                                                </form>
                                        </tr>
                                        <?php
                                            }
                                            sqlsrv_free_stmt($result);
                                            sqlsrv_close($conn);
                                        ?>


                                        </tbody>
....
$(document).ready(function(){
                //Delete banner item confirm box
                $("button#delete").confirm({

                     confirm: function (el) {
                        $.ajax({
                            url: 'bannerDeleteItem.php',
                            data: el.closest('form').serialize(),
                            type: 'post',
                        }).done(function () {
                            //do something if you want when the post is successfully
                            if(!alert('Form Had Successfully Deleted.')){location.reload(); }
                        }).fail(function () {
                            //if the post is failed show an error message if you want
                            alert('Some error occur. Please try again later.');
                        }).always(function () {
                            //this is executed on success and failure
                            $('#myhiddendiv').hide();
                        })  
                    }
                });

这是我的bannerDeleteItem.php:

<?php
include 'dbConnect.php';

global $conn;


    //Gather all required data
    $item_id = $_POST['banner_id'];

    //Delete file directory function
    $query = "SELECT banner_data FROM I_Banner WHERE banner_id='$item_id'";
    $result = sqlsrv_query($conn,$query);

    while($row = sqlsrv_fetch_array ( $result, SQLSRV_FETCH_ASSOC )){
        //Create the SQL query
        $delete = "DELETE FROM I_Banner WHERE banner_id = '$item_id' ";
        //Execute the query
        $deleteResult = sqlsrv_query($conn,$delete);
        $data = "uploads/$row[banner_data]";
        unlink($data);

    }

    // Close the mysql connection
    sqlsrv_close($conn);


// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>

bannerDeleleItem.php代码正常工作,只是当我按下“删除”按钮时,它会弹出确认框,然后我点击“好”但没有任何反应。看起来我的ajax代码没有进入bannerDeleteItem.php代码。我错过的任何地方?请纠正我

1 个答案:

答案 0 :(得分:0)

我通过替换

中的表单解决了这个问题

看起来像这样:

<td>
    <form class="form" id="form" action="" method="post" enctype="multipart/form-data">
       <input type="hidden" name="banner_id" value="<?php echo $row['banner_id']?>"></input>
       <center/><button id="delete" class="btn btn-info btn-sm" title="Remove"><i class="fa fa-times"></i>
    </form>
</td>

我错误地将表格数据外的表格替换为表格数据中应提交的数据。