Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jQuery Ajax sending data to php with one button click but two different actions

What I’m trying to achieve is a user to follow another user without having to refresh the page. So far I’ve played around and had no problem inserting and deleting the rows in mysql table, but now when I’m trying with AJAX I can’t get it to work.

jquery

//the button for following and unfollowing a user
$(document).ready(function(){
$("#followbutton").click(function(e) {
e.preventDefault();
var theuserid = $('#theuserid').val();
var thefollower = $('#thefollower').val();
$.ajax({
url: 'includes/followuser.inc.php',
type: 'post',
data: {'theuserid': theuserid, 'thefollower': thefollower, 'followbutton': true},
success: function(response){

$('#followmessage').html(response);   
$("#followmessage").show().delay(3000).fadeOut();
//I want the button to change its text to Following and when hovering it should say unfollow if user is followed
$('#followbutton').text("Unfollow"); 

}
});
});
});

followuser.inc.php

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if (isset($_POST["submitFollow"])){
$userthatisfollowed = $_POST["thefollower"];
$theuserid = $_POST["theuserid"];

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('INSERT INTO userfollow (thefollower, theuserid, followstatus) VALUES (?,?,?)');
$followstatus = 1;
$stmt->bind_param('sss', $userthatisfollowed, $theuserid, $followstatus);
$stmt->execute();
echo $response = "<span>Followed!</span>";
$stmt->close();

} else if(isset($_POST["submitUnfollow"])){
$userthatisfollowed = $_POST["thefollower"];
$theuserid = $_POST["theuserid"];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('DELETE userfollow FROM userfollow WHERE thefollower = ? AND theuserid = ?');

$stmt->bind_param('ss', $userthatisfollowed, $theuserid);
$stmt->execute();
echo $response = "<span>Unfollowed!</span>";
$stmt->close();
} else {
    echo "DID NOT WORK";
}

profile.php

if(isset($_SESSION["userid"]) && $_SESSION["userid"] != $userthatisfollowed) {
?>
<form action="<?php echo htmlspecialchars("includes/followuser.inc.php");?>" id="followform" method="post">
<?php

//if record is found in table, show Unfollow button, else Follow
if ($result->num_rows > 0){
$subscribe_status = "Unfollow";
} else {
$subscribe_status = "Follow";
}

//Here the button should change to Unfollow or Follow
echo "<button name='submit".$subscribe_status."' id ='followbutton' type='submit'>";
echo "<span>Follow</span>";
echo "</button>";

//this is just a notification bell for later
echo "<button name='submit".$subscribe_status."' id ='notificationbell' type='submit'>";
echo "<i class='fa fa-bell'></i>";
echo "</button>";

//the feedback message
echo "<div id='followmessage'></div>";

?>
<input type="hidden" name="theuserid" id="theuserid" value="<?php echo $_SESSION["userid"] ?>">
<input type="hidden" name="thefollower" id="thefollower" value="<?php echo $userthatisfollowed; ?>">
</form>
<?php
}

What’s worth noting is that I’m getting the response DID NOT WORK which tells me that if(isset($_POST["submitUnfollow"])) is not set. However, If I try with if(isset($_POST["theuserid"]) && (isset($_POST["thefollower"])) then it actually works for the insert query but not for the delete query.

>Solution :

You’re missing the submitFollow parameter in the data: object. Instead, you have followbutton: true, which isn’t used by the PHP code. So change that to:

data: {'theuserid': theuserid, 'thefollower': thefollower, 'submitFolow': 'true'},

And for the unfollow button, use submitUnfollow instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading