Javascript or php call to load/edit html table row data inside same page

I??�ve created an html table reading records from SQL trough PHP and managed to perform add, edit and delete actions. Anyway my edit sciprt do it loading a blank page while I??�d rather want to do it in the sama page. Of course could try CRUD but that seems me over my knwoledge skills, so wuold like to use Java to pass them in a form already shown in the page (which I use to add new data). I figured out at least two ways to do it:

  • tag single html td contents and use java to read them in the form
  • call back action from php and load them in the form

to avoid PHP/SQL call which may be slower, I??�d prefer first option. So created a table structure like that and tried to use variable $row['id_contact'] which match the unique ID from SQL table that way in the edit button:

while($row = mysqli_fetch_array($result))   {

echo "<tr>";
echo "<td><span id='rubricaCONTACTid_contact" . $row['id_contact'] . "'>" . $row['id_contact'] . "</span></td>";
echo "<td><span id='rubricaCONTACTname" . $row['id_contact'] . "'>" . $row['name'] . "</span></td>";
echo "<td><span id='rubricaCONTACTsurname" . $row['id_contact'] . "'>" . $row['surname'] . "</span></td>";
echo "<td><span id='rubricaCONTACTtel1" . $row['id_contact'] . "'>" . $row['tel1'] . "</span></td>";
echo "<td><span id='rubricaCONTACTtel2" . $row['id_contact'] . "'>" . $row['tel2']. "</span></td>";
echo "<td><span id='rubricaCONTACTmail" . $row['id_contact'] . "'>" . $row['mail'] . "</span></td>";
echo "<td><span id='rubricaCONTACTorganization" . $row['id_contact'] . "'>" . $row['organization'] . "</span></td>";
echo "<td> <button onclick='loadDATAtoCHANGE(" . $row['id_contact'] . ");'>edit</button></td>";
echo "<td> <a href=DELETEcontact/removeCONTACT.php?id=" . $row['id_contact'] . " \">&#9746;</a></td></td>";
echo "</tr>";


}

Despite my tries didn??�t succeded in make Javascript recognize the row value.

Anyway was able to call static data load - setting it for example equal to row 10 contents, i.e. value='"+document.getElementById('rubricaCONTACTname10').innerHTML+"'

function loadDATAtoCHANGE()
{
document.getElementById("rubricaCONTACTname").innerHTML = 
"<div class='tooltipLEFT'>&#x1F6C8;<span class='tooltiptext'>nome</span></div><input type='text' name='save1' id='enteringCONTACTname' placeholder='name' onkeyup='updateCONTACTname()' value='"+document.getElementById('rubricaCONTACTname10').innerHTML+"' /></br>";
}

As a matter of fact still hasn??�t understood how to pass $row['id_contact'] inside the very java call argument in order to make it dinamically choose the rows. Basically I thought it should be simply placed inside the function argument () but, if so, my syntax didn??�t work out. Does some one has some suggestions to make it happen?

Should you find any other solutions would anyway give it chance ;)

Answer

Solution:

You have to change your onclick statement to:

echo "<td> <button onclick='loadDATAtoCHANGE(\'" . $row['id_contact'] . "\')'>

Because if you miss quotes around PHP var, JavaScript read the value like var and not like string.

In the function you have to use

function loadDATAtoCHANGE(id) {
    let dataToChange = "rubricaCONTACTname"+id;
    document.getElementById(dataToChange).innerHTML = 
    "<div class='tooltipLEFT'>&#x1F6C8;<span class='tooltiptext'>nome</span></div><input type='text' name='save1' id='enteringCONTACTname' placeholder='name' onkeyup='updateCONTACTname(id)' value='"+document.getElementById(dataToChange).innerHTML+"' /></br>";
}

Answer

Solution:

Other way will be passing this inside your function where this refer to current button which is click and then use .closest("tr").querySelectorAll("span[id*=rubricaCONTACT]").forEach... to loop through all span tags which is inside your tr and then inside loop append new inputs in some variable and then add them inside your form tag .

Changes you can make in your php code :

while($row = mysqli_fetch_array($result))   {
//you can give any name inside data-name..it will label and name="" for input
echo "<tr>";
echo "<td><span id='rubricaCONTACTid_contact" . $row['id_contact'] . "' data-name='id'>" . $row['id_contact'] . "</span></td>";
echo "<td><span id='rubricaCONTACTname" . $row['id_contact'] . "' data-name='name'>" . $row['name'] . "</span></td>";
echo "<td><span id='rubricaCONTACTsurname" . $row['id_contact'] . "' data-name='surname'>" . $row['surname'] . "</span></td>";
echo "<td><span id='rubricaCONTACTtel1" . $row['id_contact'] . "' data-name='tel1'>" . $row['tel1'] . "</span></td>";
echo "<td><span id='rubricaCONTACTtel2" . $row['id_contact'] . "' data-name='tel2' >" . $row['tel2']. "</span></td>";
echo "<td><span id='rubricaCONTACTmail" . $row['id_contact'] . "' data-name='email'>" . $row['mail'] . "</span></td>";
echo "<td><span id='rubricaCONTACTorganization" . $row['id_contact'] . "' data-name='organization'>" . $row['organization'] . "</span></td>";
echo "<td> <button onclick='loadDATAtoCHANGE(this);'>edit</button></td>";
echo "<td> <a href=DELETEcontact/removeCONTACT.php?id=" . $row['id_contact'] . " \">&#9746;</a></td></td>";
echo "</tr>";


}

Demo Code :

function loadDATAtoCHANGE(selector) {
  var htmls = "";
  //loop through spans...
  selector.closest("tr").querySelectorAll("span[id*=rubricaCONTACT]").forEach(function(el) {
    //append new inputs ...
    htmls += `<div class="form-group"><label>${el.getAttribute("data-name").toUpperCase()}</label><input type="text" name="${el.getAttribute("data-name")}" class="form-control" value="${el.textContent}"> </div>`
  })
  htmls += `<button type="submit" class="btn btn-primary">Submit</button>`
  document.getElementById("forms").innerHTML = htmls //add them inside form..
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<table class="table">
  <tr>
    <td><span id='rubricaCONTACTid_contact1' data-name="id">1</span></td>
    <td><span id='rubricaCONTACTname1' data-name="name">abc</span></td>
    <td><span id='rubricaCONTACTsurname1' data-name="surname">xyz</span></td>
    <td><span id='rubricaCONTACTtel11' data-name="tel1">kokok</span></td>
    <td><span id='rubricaCONTACTtel21' data-name="tel2">1345</span></td>
    <td><span id='rubricaCONTACTmail1' data-name="email">abc@gmail.com</span></td>
    <td><span id='rubricaCONTACTorganization1' data-name="org">somthinsghjk</span></td>
    <td> <button onclick='loadDATAtoCHANGE(this);'>edit</button></td>
    <td> <a href="DELETEcontact/removeCONTACT.php?id=1">&#9746;</a></td>
  </tr>
  <tr>
    <td><span id='rubricaCONTACTid_contact2' data-name="id">2</span></td>
    <td><span id='rubricaCONTACTname2' data-name="name">abc2</span></td>
    <td><span id='rubricaCONTACTsurname2' data-name="surname">xyz2</span></td>
    <td><span id='rubricaCONTACTtel12' data-name="tel1">kokok</span></td>
    <td><span id='rubricaCONTACTtel22' data-name="tel2">1345</span></td>
    <td><span id='rubricaCONTACTmail2' data-name="email">abc@gmail2.com</span></td>
    <td><span id='rubricaCONTACTorganization2' data-name="org">somthinsghjk</span></td>
    <td> <button onclick='loadDATAtoCHANGE(this);'>edit</button></td>
    <td> <a href="DELETEcontact/removeCONTACT.php?id=1">&#9746;</a></td>
  </tr>

</table>

<form id="forms">
  <!--data will come here-->
</form>

Source