简体   繁体   English

jQuery不发送数据到PHP?

[英]Jquery is not sending post data to php?

I have problem that Jquery Post is not sending data to other page. 我有一个问题,jQuery Post没有将数据发送到其他页面。 Data is sent to LikeMail.php page by clicking image. 单击图像将数据发送到LikeMail.php页面。 Date is stored in the id of the image. 日期存储在图像的ID中。

             <a href="viewProfile.php?id=<?php echo $record['user_id']; ?>"><img class="img-rounded" id="<?php echo $record['user_id']; ?>" src=" <?php echo "../shadi/images/" . $record['user_photo1'] ?>" alt=""
                     width="70%" height="20%"> </a>

This is my LikeMail.php page 这是我的LikeMail.php页面

<?php
session_start();
?>
<html>
<head>
    <script src="jquery-1.12.2.min.js"></script>
    <script src="test.js"></script>
</head>
</html>
<?php

include("db.php");
if(!isset($_SESSION['login'])) {
echo "";


}
else {
    $user1 = $_SESSION['user_id'];
//    echo $user1;


    if(isset($_POST['fname'])) {//This is being received from jquery
        $user2 = $_POST['fname'];
//$lname = $_POST['surname'];


//echo $lname;
        /*$sql = "UPDATE notification SET alert='$fname' WHERE id = '1'";
        if(mysqli_query($conn,$sql))
            echo "updated";*/


        $check_for_likes = mysqli_query($conn, "SELECT * FROM liked WHERE user1='$user1' AND user2='$user2'");
        $numrows_likes = mysqli_num_rows($check_for_likes);
        if (false == $numrows_likes) {
            echo mysqli_error($conn);
        }

        if ($numrows_likes >= 1) {
            echo '<input type="submit" name="unlikebutton_' . '" value="Unlike" id="Unlike" class="btn btn-lg btn-info edit">';


        }
        if ($numrows_likes == 0) {
            echo '<input type="submit" name="likebutton_' . '" value="Like" id="Like" class="btn btn-lg btn-info edit">';
        }
    }
}?>

POST array with variable fname (Also mentioned in the comments in the code) is being received from the previous page. 从上一页接收到具有变量fname POST数组(代码中的注释中也提到了)。

This is jquery method 这是jQuery方法

$(document).ready(function(){

    $('.img-rounded').click(function(){
        $.post("LikeMail.php",
            {fname: this.id},
            function(data){
                $('.respond').html(data);
            }
        );

    });

});

Can you please tell me that what is the bug there. 您能告诉我那里的错误是什么。 I have run this jquery method on other page other than LikeMail.php and it runs perfectly but Jquery post is not sending data to LikeMail.php 我已经在LikeMail.php以外的其他页面上运行了此jquery方法,它运行得很好,但Jquery帖子未将数据发送到LikeMail.php

Try to prevent default behavior that happens on <a> click, and maybe to prevent event propagation. 尝试防止<a>单击时发生的默认行为 ,并可能防止事件传播。

$('.img-rounded').click(function(e){
    e.stopPropagation(); // not sure if needed
    e.preventDefault();
    $.post("LikeMail.php",
        {fname: this.id},
        function(data){
            $('.respond').html(data);
        }
    );

});

If i remove the <a> tag, it works. 如果我删除了<a>标记,它将起作用。

See code on https://jsfiddle.net/8ywdb7uh/ (Take a look at the console messages, it will respond with 404) 参见https://jsfiddle.net/8ywdb7uh/上的代码(看看控制台消息,它将以404响应)

Please prevent the default behaviour 请防止默认行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM