Skip to content
Advertisement

Update database with a live choice made by client

How to update a specific cell in phpmyadmin with jQuery after appending a specific div to a group, code is below

<div id="item1">Item 1
  <input type="button" value="put me in Group1" name="I1G1"> <!-- I1=Item1 / G1=Group1-->
  <input type="button" value="put me in Group2" name="I2G2">
</div><br><br>

<div id="item2">Item 2
  <input type="button" value="put me in 1" name="I2G1">
  <input type="button" value="put me in 2" name="I2G2">
</div><br><br>


<div id="Group1">Group 1</div><br>

<div id="Group2">Group 2</div><br>

<script>
    $('input[name$="I1G1"]').click(function(){
        $("#item1").appendTo("#Group1");
        $(this).hide();
        $('input[name$="I1G2"]').hide();
        <?php $UPDATE = mysqli_query($conn, "UPDATE results SET round = 'Group 1' WHERE team = 'Item 1'"); ?>
    });
    $('input[name$="I1G2"]').click(function(){
        $("#item1").appendTo("#Group2");
        $(this).hide();
        $('input[name$="I1G1"]').hide();
        <?php $UPDATE = mysqli_query($conn, "UPDATE results SET round = 'Group 2' WHERE team = 'Item 1'"); ?>
    });
//same jQuery code with 2nd item
</script>

the problem is when the item getting updated in database, it became always Group 2 which is the second update. So how to update the database with the selected group on a side question is it better to use dropdown list or buttons in selecting groups

Advertisement

Answer

You need to create a <form> element that uses the POST method to a php script.

<form id='my_form' action='my_db_script.php' method='POST'>
// form elements
</form>

In your php script you would use $_POST['my_option'] to get a form option by its name… you can execute your sql in the php file.

Since you tagged jquery, you could also use the jquery onsubmit event with $.ajax: (documentation)

$("#my_form").onsubmit(function() {
 $.post({
   // my options
 });
});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement