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:
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'] . " \">☒</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'>🛈<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 ;)
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'>🛈<span class='tooltiptext'>nome</span></div><input type='text' name='save1' id='enteringCONTACTname' placeholder='name' onkeyup='updateCONTACTname(id)' value='"+document.getElementById(dataToChange).innerHTML+"' /></br>";
}
Other way will be passingthis
inside your function wherethis
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 yourform
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'] . " \">☒</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">[email protected]</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">☒</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">[email protected]</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">☒</a></td>
</tr>
</table>
<form id="forms">
<!--data will come here-->
</form>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.