I have been looking around on the forum and tried everything but I cant get my button to disable after clicking it, I just started using php and this is one of my first projects. Its a like and dislike system. Does anyone know the awnser? when you press a like button or dislike button I want both buttons to be disabled.
<!--Like System-->
<center>
<?php
$sql = "SELECT * FROM likes";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?> <h1 class="lcount"><?php echo $row["average"];?> <span class="glyphicon glyphicon-signal"></span></h1></br><?php
}
} else {
echo "0 results";
}
?>
<?php
$sql = "SELECT * FROM likes";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?> <h1 class="lcount"><?php echo $row["like"];?>
<span style="color: green;"class="glyphicon glyphicon-thumbs-up"></span></h1></br>
<?php
}
} else {
echo "0 results";
}
?>
<?php
$sql = "SELECT * FROM likes";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?> <h1 class="lcount"><?php echo $row["dislike"];?>
<span style="color: red;" class="glyphicon glyphicon-thumbs-down"></span></h1>
<?php
}
} else {
echo "0 results";
}
?>
<?php
if(isset($_POST['like'])){
$sql = "UPDATE `likes` SET `like` = `like` + 1 , `average` = `average` + 1";
if (mysqli_query($con, $sql)) {
} else {
echo "Error updating record: " . mysqli_error($con);
}
}
?>
<?php
if(isset($_POST['dislike'])){
$sql = "UPDATE `likes` SET `dislike` = `dislike` + 1 , `average` = `average` - 1";
if (mysqli_query($con, $sql)) {
} else {
echo "Error updating record: " . mysqli_error($con);
}
}
?>
<form type="#" method="post">
<button name="like" type="submit" class="btn btn-success">
<span class="glyphicon glyphicon-thumbs-up"></span></button>
<button name="dislike" type="submit" class="btn btn-danger">
<span class="glyphicon glyphicon-thumbs-down"></span></button>
</form>
</center>
<!--Like System END-->
If you want to only PHP use like this:
<?php
$status = 'enabled';
if (isset($_POST['like'])) {
$status = 'disabled';
}
?>
<form type="#" method="post">
<button name="like" type="submit" class="btn btn-success" <?=$status?>>
Button is <?=$status?>
</button>
</form>
You can use Jquery like this:
https://jsfiddle.net/y4qLew3s/1/
Updated:
I updated. It works with like or dislike button:
in javascript
with setAttribute()
function disable(){
document.getElementById('submit').setAttribute("disabled", "disabled");
}
updated if you disable both button at same time
<script>
function disable(){
document.getElementById('submit').setAttribute("disabled", "disabled");
document.getElementById('submit1').setAttribute("disabled", "disabled");
}
</script>
<input title="" id="submit" type="submit" name="button" onclick="disable()" value="like">
<input title="" id="submit1" type="submit" name="button" onclick="disable()" value="unlike">