1

i want to select row contents to display it in a form so i can update it with jquery ajax but i have a problem with it. Can someone help me please? It is really so important for me so i can anderstand it

When i run the code, i can only display the name which is the primary key of the actors table

gestionActors.php

<?php

    require_once("config.php");
    include('getActors.php');

    $actors = get_actors();
            ?>
            <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Ajouter un acteur</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <style type="text/css">
        table, tr, td{
            border :1px solid black;
        }
    </style>
    <script src="jquery-3.2.1.js"></script>
    <script >
        function update_actor(name){
            jQuery.ajax({
            type: "POST",
            data: 'name='+name,     // <-- put on top
            url: "update.php",
            cache: false,
            success: function(response)
            {
            alert("Record successfully updated");
            }
            });
 }
  function updateFunction(){
         $("#update").click(function(){
            name=(this).getAttribute('name');
            var address=$("#address"+name).text();
            var gender=$("#gender"+name).text();
            var birthdate=$("#birthdate"+name).text();
             $("#name").val(name);
            $("#address").val(address);
            $("#gender").val(gender);
            $("#birthdate").val(birthdate);
      });
     }
        function delete_actor(){
            var name=document.getElementById('name').value;
            var req = new XMLHttpRequest();
            req.onreadystatechange=function(){
                if(req.readyState==4 && req.status==200){
                    document.getElementById('div2').innerHTML="";
                    document.getElementById('div2').innerHTML=req.responseText;
                }
            }
            req.open('GET', 'delete.php?name='+name,true);
            req.send();

        }

    </script>
</head>
<body>
        <h1> *** Gestion Actors ***</h1>
        <table>
                <tr><th>Name</th>
                <th>Address</th>
                <th>Gender</th>
                <th>Birthdate</th>
                <th>Update Actor</th>
                <th>Delete Actor</th>
            </tr>
                <?php foreach($actors as $cle=>$actor) {?>
            <tr>

                <td><span id="<?php echo $actor['name']; ?>" name='name'><?php echo $actor['name']; ?></span></td>
                <td><span id="address" name='address'><?php echo $actor['address']; ?></span></td>
                <td><span id="gender" name='gender'><?php echo $actor['gender']; ?></span></td>
                <td><span id="birthdate" name='birthdate'><?php echo $actor['birthdate']; ?></span></td>
                <td><input type="button" id="update" name="<?php echo $actor['name'];?>" value="update" onclick="updateFunction();"></td>
                <td><input type="button" name="delete" value="delete" onclick="delete_actor();"></td>
            </tr>
            <?php } ?>
        </table>
        <div id="update">
            <h2>Update Actor</h2>
            <table>
            <tr>
                <td><label>Name : </label></td><td><input type="text" name="name" id="name" placeholder="Actor Name..." ></td>
            </tr>
            <tr>
                <td><label>Address : </label></td><td><input type="text" id="address" name="address" placeholder="Actor Address.." ></td>
            </tr>
            <tr>
                <td><label>Gender : </label></td><td>
                <input type="radio" name="gender" id="gender" value="M"><span> M</span>
                <input type="radio" name="gender" id="gender" value="F"><span> F</span></td>
            </tr>
            <tr>
                <td><label>Birthdate : </label></td><td><input type="date" id="birthdate" name="birthdate" placeholder="Actor Birthdate.."></td>
            </tr>

            <tr>
                <td colspan="2"><input type="submit" name="update" value="Save" onclick="update_actor();"></td>
        </tr>
        </table>

        </div>
</body>
</html>

update.php

<?php 
    include('config.php') ;
    global $con;
    if(!empty($_POST)){
        $name = htmlspecialchars($_POST['name']);
        $address = htmlspecialchars($_POST['address']);
        $gender = htmlspecialchars($_POST['gender']);
        $birthdate = htmlspecialchars($_POST['birthdate']);
        $sql_query="UPDATE stars set address = '$address', gender= '$gender', birthdate= '$birthdate' WHERE name like '$name'";
        $res=mysqli_query($con,$sql_query);
        if($res) echo "Actor updated";
        else{
            echo "update Problem :". mysqli_error($con);        
        }
    }


?>

enter image description here

2 Answers 2

2
function updateFunction(e){
    var elem = $(e.target);
    var parentRow = elem.parents('tr');
    $("#row_name").val(parentRow.find("[name='name']").text());
    $("#name").val(parentRow.find("[name='name']").text());
    $("#address").val(parentRow.find("[name='address']").text());
    //considering you saved the values are male and female in db
    if(parentRow.find("[name='gender']").text() == 'male') {
        $('[value="M"]').attr('checked', true)
    }
    else {
        $('[value="F"]').attr('checked', true)
    }
    $("#birthdate").val(parentRow.find("[name='birthdate']").text());
}

function update_actor(name){
    jQuery.ajax({
        type: "POST",
        data: 'name='+name,     // <-- put on top
        url: "update.php",
        cache: false,
        success: function(response)
        {
                alert("Record successfully updated");
                var rowToUpdate = $('#row_name').val();
                var row = $("tr[data-row-name='"+rowToUpdate+"']");
                row.find("[name=name]").text($("#name").val());
                row.find("[name=address]").text($("#address").val());
                row.find("[name=gender]").text($('input[name=gender]:checked').val());
                row.find("[name=birthdate]").text($("#birthdate").val());
        }
    });
}

The problem is you are using id param inside your loop, which needs to be removed. And your updateFunction just binding a click to the update button.

I have removed the id attribute from the data rows and fetched the values with respect to the target element.

       <?php foreach($actors as $cle=>$actor) {?>
        <tr data-row-name="<?php echo $actor['name']; ?>">
            <td><span name='name'><?php echo $actor['name']; ?></span></td>
            <td><span name='address'><?php echo $actor['address']; ?></span></td>
            <td><span name='gender'><?php echo $actor['gender']; ?></span></td>
            <td><span name='birthdate'><?php echo $actor['birthdate']; ?></span></td>
            <td><input type="button" id="update_row" name="<?php echo $actor['name'];?>" value="update" onclick="updateFunction(event);"></td>
            <td><input type="button" name="delete" value="delete" onclick="delete_actor();"></td>
        </tr>
        <?php } ?>

Update your html like above and update your updateFunction like i mentioned. You can see all the values populating.

And above your save button in the update form add one hidden field as row_name to know which row we gonna update so that we can use this value to update the rows manually once update ajax is success.

<input type="hidden" name="name" id="row_name" placeholder="" >
<td colspan="2"><input type="submit" name="update" value="Save" onclick="update_actor();"></td>
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much, it was really helpfull
You are welcome, does it solved all your problems now ? i have only checked your html and js codes, so haven't checked the php part.
A last thing when i update or i delete a row from the table what can i do so that the table can display database content without refreshing the page??
I think its better to refresh the page once the delete ajax call is finished, because if you want to update the content without refreshing the page, you need to call one more api to fetch all the records from db and manually update entire list(need to construct the rows in js itself) with the newly fetched data. What was the issue you are facing now ?
the only problem is when i update a row i have to refresh the page manually so it displays the updated row
|
2

Please update your form as below

<table>
      <tr><th>Name</th>
        <th>Address</th>
        <th>Gender</th>
        <th>Birthdate</th>
        <th>Update Actor</th>
        <th>Delete Actor</th>
      </tr>
      <?php foreach($arrdata as $cle=>$actor) {?>
       <tr>
        <td><span class="actorname"><?php echo $actor['name']; ?></span></td>
        <td><span class="address"><?php echo $actor['address']; ?></span></td>
        <td><span class="gender"><?php echo $actor['gender']; ?></span></td>
        <td><span class="birthdate"><?php echo $actor['birthdate']; ?></span></td>
        <td><input type="button" id="update" name="<?php echo $actor['name'];?>" value="update" onclick="updateFunction(event

);"></td>
    <td><input type="button" name="delete" value="delete" onclick="delete_actor();"></td>
  </tr>
  <?php } ?>
</table>
<div id="update">
  <h2>Update Actor</h2>
  <form name="actor" id="actordata">
  <table>
    <tr>
      <td><label>Name : </label></td><td><input type="text" name="name" id="name" placeholder="Actor Name..." ></td>
    </tr>
    <tr>
      <td><label>Address : </label></td><td><input type="text" id="address" name="address" placeholder="Actor Address.." ></td>
    </tr>
    <tr>
      <td><label>Gender : </label></td><td>
      <input type="radio" name="gender" id="gender" value="M"><span> M</span>
      <input type="radio" name="gender" id="gender" value="F"><span> F</span></td>
    </tr>
    <tr>
      <td><label>Birthdate : </label></td><td><input type="date" id="birthdate" name="birthdate" placeholder="Actor Birthdate.."></td>
    </tr>

    <tr>
      <td colspan="2"><input type="button" name="update" value="Save" onclick="update_actor();"></td>
    </tr>
  </table>
</form>
</div>

The updated ajax function as below.

 function update_actor(){
    jQuery.ajax({
      type: "POST",
      data:  $('#actordata').serialize(),     // <-- put on top
      url: "update.php",
      cache: false,
      success: function(response)
      {
        alert("Record successfully updated");
      }
    });
  }
  function updateFunction(e){
    $('[value="M"]').attr('checked', false);
    $('[value="F"]').attr('checked', false);
    var elem = $(e.target);
    var parentRow = elem.parents('tr');
    $("#name").val(parentRow.find("[class='actorname']").text());
    $("#address").val(parentRow.find("[class='address']").text());
    if(parentRow.find("[class='gender']").text() == 'M') {
        $('[value="M"]').attr('checked', true)
    }
    else {
        $('[value="F"]').attr('checked', true)
    }
    $("#birthdate").val(parentRow.find("[class='birthdate']").text());
 }

3 Comments

Thanks mohsin for your answer but what about when we click on the button update in the table it displays us the contents of the selected row in the inputs of the form. It doesn't work as well for me it displays just the name but the other inputs are empty ??
yes and you can see a capture i will add it to my question so that you can anderstand what i want to do
ok please share I'm working on it I will share you full code soon.

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.