0

I have problem that Jquery Post is not sending data to other page. Data is sent to LikeMail.php page by clicking image. Date is stored in the id of the image.

             <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

<?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.

This is jquery method

$(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

2
  • Please check for any console messages! Commented Aug 13, 2016 at 17:52
  • It is empty. Nothing there Commented Aug 13, 2016 at 17:53

2 Answers 2

1

Try to prevent default behavior that happens on <a> click, and maybe to prevent event propagation.

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

});
Sign up to request clarification or add additional context in comments.

2 Comments

I have added it.. Jquery started sends data but now anchor tag isn't working
@ZainFarooq You cannot do ajax call with waiting for result if you go to other location in the meantime. How would you wait for data and do $('.respond').html(data); if you went to some location by clicking the anchor tag?
1

If i remove the <a> tag, it works.

See code on https://jsfiddle.net/8ywdb7uh/ (Take a look at the console messages, it will respond with 404)

Please prevent the default behaviour

1 Comment

yes. First I checked code without <a> tag. It worked fine. After adding <a> tag it generated problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.