1

I have this script which is triggered when a button with the class .press_me is pressed.The buttons are on a column from a php generated mysql table:

$result = mysqli_query($con,"SELECT * FROM tbname");
echo "<table id='main'>"; 
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='right-middle user'>" . $row['ID'] . "</td>";
echo "<td class='right-middle user'>" . $row['Nume'] . "</td>";
echo "<td class='right-middle done'>" . $row['Teme_facute'] . "</td>";
echo "<td class='right-middle check'>" . "<img src='img/check.png' class='press_me'>" ."</td>";
echo "<td class='right-middle undone'>" . $row['Teme_nefacute'] . "</td>";
echo "<td class='right-middle uncheck'>" . "<img src='img/uncheck.png'>" . "</td>";
echo "<td class='side-table resetDone'>" . "<img src='img/resetDone.png'>" . "</td>";
echo "<td class='side-table resetUndone'>" . "<img src='img/resetUndone.png'>" . "</td>";
echo "</tr>";
}
echo "</table>";

And the script:

<script>
   $(function (){
            $('.press_me').click(function(){
        var id=<?php echo json_decode('$row[ID]'); ?>;          
            var request = $.ajax({
                                    type: "POST",
                                    url: "counter.php"
                                  });
                                  request.done(function( msg ) {

                                        alert('Success');
                                       location.reload();
                                        return;

                                  });
                                  request.fail(function(jqXHR, textStatus) {
                                        alert( "Request failed: " + textStatus );
                                    });
            });
    }); 
    </script>

And counter.php:

<?php
echo $_POST["id"];
if(!empty($_POST["id"]))
{
    $id = $_POST["id"];

    $connection=mysqli_connect("host","user","pass","db");
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit;
    }

    mysqli_query($connection,"UPDATE tbname SET amount= (amount+ 1) WHERE ID = '" . $id . "'");

    mysqli_close($connection);
    echo 'OK';
}
else
{
    echo 'NO ID PASSED';
}
?>

I'm having trouble updating only the value on the same row as the button pressed.When i run the page in this configuration counter.php returns no id passed and i think the problem is with the passing of the row id. Can anyone help me update only the value on the row with the pressed button?

I'm aware of sql injection but it's not the main problem now

2
  • Try this: var id="<?php echo $row['ID']; ?>"; and in the $.ajax construct data:id, Commented Jan 30, 2016 at 13:25
  • @hherger still returns nothing Commented Jan 30, 2016 at 13:27

3 Answers 3

2

Your id is empty try this

echo "<td class='right-middle check'>" . "<img data-id='{$row['ID']}' src='img/check.png' class='press_me'>" ."</td>";

And in the script use this

  var id=$(this).data("id");  
Sign up to request clarification or add additional context in comments.

6 Comments

it works, it returns the row id, but the value doesn't seem to change. should i modify sth in counter.php aswell?
try this "UPDATE tbname SET amount= amount+ 1 WHERE ID = $id"
unfortunately the values still don't increase
@AlexResiga Look at Rafai lAkhmetshin answer, that is also one of your problems
echo this "UPDATE tbname SET amount= amount+ 1 WHERE ID = $id" in your code and whats result???
|
2

change you javascript, looks like you are not sending data at all

<script>
   $(function (){
            $('.press_me').click(function(){
        var id=<?php echo json_decode('$row[ID]'); ?>;          
            var request = $.ajax({
                                    type: "POST",
                                    url: "counter.php",
                                    // add this line
                                    data: { id: id}
                                  });
                                  request.done(function( msg ) {

                                        alert('Success');
                                       location.reload();
                                        return;

                                  });
                                  request.fail(function(jqXHR, textStatus) {
                                        alert( "Request failed: " + textStatus );
                                    });
            });
    }); 
    </script>

2 Comments

This is also a required change
thank you so much, you and @paranoid both solved my problem and i don't know whose answer should i mark as verified
0

Replace below script:

<script>
 $(function (){
    $('.press_me').click(function(){
      var id=<?php echo $row[ID]; ?>;          
          var request = $.ajax({
                          type: "POST",
                          url: "counter.php"
                        });
        request.done(function( msg ) {

              alert('Success');
             location.reload();
              return;

        });
        request.fail(function(jqXHR, textStatus) {
              alert( "Request failed: " + textStatus );
          });
  });
}); 
</script>

NOTE: I assume that you are working with single record. If not then it will going wrong.

If this is working wrong then replace .press_me line with below:

$id = $row['ID'];
echo "<td class='right-middle check'>" . "<img src='img/check.png' class='press_me' id='<?php print($id);?>' >" ."</td>";

And script is like:

var id = $(this).attr("id");

Hope this help you well!

4 Comments

Not really going to help much, you did spot that there will be more than one $row['id'] as that is collected in a while loop from a multi row result set? Or maybe not
He is not working with single record This will go wrong
well i hoped that he knows to get the row id of the row of the pressed button
You do realise that id="???" has a specific meaning and use in the DOM, you might want to use a slightly different name to id

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.