如何防止AJAX调用后刷新页面

时间:2019-01-05 09:37:57

标签: javascript php ajax

我在这个php页面上有javascript ajax,该页面调用了另一个php文件。但是每次调用时,它都会刷新页面。

我尝试了这两个代码,但是在调用`php文件之后,它仍然保持刷新:

  e.preventDefault(); 
  //and   
  return false;

但是仍然保持重定向/刷新到同一页面。 我什至在我调用的php文件中都没有重定向头。

这是我的HTML

 <div class="col-md-3 col-sm-6 col-6 ad-image">
                        <label for="file1">
                            <img id="blah1" src="http://placehold.it/500" alt="..." class="img-thumbnail">
                            <input type="button" value="Remove Photo" style="margin-top: 5px;" class="btn btn-danger btn-sm" id="image-remove-btn-1">
                            <small id="textCount" class="form-text text-center bold">Thumbnail</small>
                        </label>
      </div> 

这是我的javascript

       $("#image-remove-btn-1").click(function (e) {
            e.preventDefault(); //doesn't work still page keeps refreshing

            $('#blah1').attr('src', 'http://placehold.it/500');

            var userId =<?php echo $userId ?>;
            var adId =<?php echo $adId ?>;

            deletePhoto('blah1', userId, adId); //this is the function with ajax               
            $(this).hide();

            return false; //doesn't work still page keeps refreshing
        });

这是deletePhoto()函数:

        function deletePhoto(imgeName, userid, adId) {
            $(document).ready(function () {                                      
                $.ajax({
                    url: 'includes/remove-ad-image-inc.php',
                    dataType: 'text', // what to expect back from the PHP script, if anything                        
                    data: {
                        userId: userid,
                        adId: adId,
                        imgeName: imgeName
                    },
                    type: 'post',
                    success: function (php_script_response) {
                        alert(php_script_response); // display response from the PHP script, if any
                    }

                });

            });                               
        }

这是我的php remove-ad-image-inc.php,我是通过ajax上面的电话

<?php

include_once './dbConnection.php';

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

 $userId = mysqli_real_escape_string($conn, filter_input(INPUT_POST, "userId"));
$adId = mysqli_real_escape_string($conn, filter_input(INPUT_POST, "adId"));
$imgeName = mysqli_real_escape_string($conn, filter_input(INPUT_POST, "imgeName"));



if (isset($userId) && isset($adId) && isset($imgeName)) {


$sql = "SELECT * FROM adimage WHERE adimageno=? AND adid=? AND userid=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "error!";
} else {
    mysqli_stmt_bind_param($stmt, "sii", $imgeName, $adId, $userId);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    while ($row = mysqli_fetch_assoc($result)) {
        $ImageId = $row['adimageid'];

    }


    $fileName = "../uploads/ad/adImage-" . $ImageId . "-" . $adId . "-" . $userId . "*";
    $fileInfo = glob($fileName);

    $fileExt = explode(".", $fileInfo[0]);
    $fileActualExt = $fileExt[1];

    $file = "../uploads/ad/adImage-" . $ImageId . "-" . $adId . "-" . $userId . "." . $fileActualExt;

    array_map('unlink', glob($fileName));

    $sql = "UPDATE adimage SET adimagestatus=1 WHERE adimageid='$ImageId';";
    mysqli_query($conn, $sql);
    exit();
 }
}

有人可以帮助我解决问题吗?

0 个答案:

没有答案