i have this script which updates a counter in a mysql database but it's not working at all and i can't see what wrong as i am new into php and ajax:
$(function () {
$('.press_me').click(function(){
var button_ID= $(this).parent('tr').attr($row['ID']);
$.post('counter.php', {button_ID: button_ID}, function(result){
$($row['Teme_nefacute']).html(result);
});
request.done(function( msg ) {
alert('Success');
return;
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
});
and counter.php:
<?php
// Connection to database
$connection=mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo 'NOT_OK';
//echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Increasing the current value with 1
mysqli_query($connection,"UPDATE table SET amount= (amount + 1) WHERE ID=' " . $_POST["button_ID"] ." ' ");
$get_count= mysqli_query("SELECT * FROM `table` WHERE `ID`=' " . $_POST["button_ID"] . " ' ");
$count_data= mysqli_fetch_array($new_count_data);
echo($count_data["amount"]);
mysqli_close($connection);
?>
can anyone help me make it work async?
First of all, PHP is a backend technology which works in server and sends result to client (browser, curl, wget etc.) Javascript is a frontend technology which works in the client. So in that point of view
Your php code should be between of <?php //code ?>
tags, and it would executed in backend then result would be printed in javascript code.
As a result your view should be a php file. But it should contain html,js and php mixed.
<script>
$(function () {
$('.press_me').click(function() {
var button_ID= $(this).parent('tr').attr("<?php echo $row['ID']; ?>");
$.post('counter.php', {button_ID: button_ID}, function(result) {
$("<?php echo $row['Teme_nefacute']; ?>").html(result);
});
request.done(function(msg) {
alert('Success');
return;
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
});
</script>
Your php code looks OK. When you try like this what is the result ?
Updated :
If your table name is table then please place it between this characters `` like in SELECT
query. table has a special meaning in SQL.