简体   繁体   中英

How can I update any row rather than only the top row with the update button on each side in PHP and AJAX

just starting out so gonna sound like a noob question but how can I use the update button to update individual rows rather than only the top row. Whenever I use the update button, the request only goes through if it was the top row.

This is the code for the index.php

<html>
<head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <center>
    <h3>UPDATE DATA</h3>

    <?php
    $run = 0;
    $conn = new mysqli('localhost', '', '', '');
    $sql = "SELECT * FROM moderator";
    $result = $conn->query($sql);
    while ( $row=mysqli_fetch_assoc($result)) {
        $run = $run + 1;
       ?>
       <div>

       ID<input type="text" id="id" value="<?php echo $row['id']; ?>">;
       Moderator<input type="text" id="mod" value="<?php echo $row['name']; ?>">;         
       Category<input type="text" id="ctgr" value="<?php echo $row['category']; ?>">;

       <button type="submit" id="update" rel="<?php echo $run; ?>">UPDATE</button>
       </div>
       <?php
    }?> 

    </center>
    <script>
        $(document).ready(function(){
            $("#update").click(function(){
                var name=$("#mod").val();
                var ctgr=$("#ctgr").val();
                var id=$("#id").val();
                $.ajax({
                    url:'update.php',
                    method:'POST',
                    data:{
                        name:name,
                        ctgr:ctgr,
                        id:id
                    },
                    success:function(response){
                        alert(response);
                    }
                });
            });
        });
    </script>
</body>

This is the code for the update.php

<?php

$conn = new mysqli('localhost', '', '', '');
$name=$_POST["name"];
$ctgr=$_POST["ctgr"];
$id=$_POST["id"];
$sql="UPDATE moderator set name='$name', category='$ctgr' where id='$id'";
if($conn->query($sql)===TRUE){
    echo "DATA updated";
}
?>

You can probably see I'm trying to create an individual variable for each row but I can't figure it out. Sorry, code's very messy too.

I've removed datebase information but the rows display when credentials are inserted.

All your rows input elements have same id attribute

Like id of first rows elements are like id, mod, ctgr

Also id of second rows elements are id, mod ctgr

Jquery find first element, if multiple elements of same id there

You can make unique id by apppending a uniqe incremental variable $run to it as

ID<input type="text" id="id"+<?php echo $run; ?> value="<?php echo $row['id']; ?>">; Moderator<input type="text" id="mod"+<?php echo $run; ?> value="<?php echo $row['name']; ?>">; Category<input type="text" id="ctgr"+<?php echo $run; ?> value="<?php echo $row['category']; ?>">; <button type="submit" id="update" rel="<?php echo $run; ?>" onclick="updatetodb(  <?php echo $run; ?>);“  >UPDATE</button>

Then create a javascript function with name updatetodb with one argument, and identify row by that argument as

function updatetodb(var run) {
var id=$('#id' +run).value;

This is because you have duplicated input ids, you should have only one ID with the same value on html, this is valid some how, you don't get an error, but you will have trouble in javascript when you try to access those id, you will get only the first one.

To fix this you can remove id and put it as class:

   <div>

   ID<input type="text" class="id" value="<?php echo $row['id']; ?>">;
   Moderator<input type="text" class="mod" value="<?php echo $row['name']; ?>">;         
   Category<input type="text" class="ctgr" value="<?php echo $row['category']; ?>">;

   <button type="submit" class="update" rel="<?php echo $run; ?>">UPDATE</button>
   </div>

and use this JS:

    $(document).ready(function(){
        $(".update").click(function(){
            var container = $(this).closest('div'); // parent div
            var name = container.find('.mod').val();
            var ctgr = container.find('.ctgr').val();
            var id = container.find('.id').val();
            $.ajax({
                url:'update.php',
                method:'POST',
                data:{
                    name:name,
                    ctgr:ctgr,
                    id:id
                },
                success:function(response){
                    alert(response);
                }
            });
        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM