How to launch PHP script using JS? | Sololearn: Learn to code for FREE!

+1

How to launch PHP script using JS?

Hello. I am working on a complex project and I hit a problem. Pretend that I have a webpage with table that is generated by PHP script and contains data from database. This table has exactly same columns as the table in SQL database plus there is one extra column with delete button for each row. What I want to do is to remove the deleted row from the webpage (DOM) using JavaScript and then delete the row from the database using PHP after clicking the delete button in the row. My plan: 1) Clicking the button 2) Getting inner HTML of the first column (contains ID of the row) into a variable 3) Using JS to delete the <tr> element from the table 4) Launch my PHP function and pass the variable containing row's ID into it 5) Delete the row whith id stored in the variable from the database using PHP I can do all of the points above EXPECT the point 4). Can you tell me how can I do it?

9/11/2018 9:09:14 AM

Jan Štěch

4 Answers

New Answer

+4

So your concern here is to delete a row using javaScript by clicking a button? Give me time, I'll make a code.

+1

Ok, after a lot of googling I managed to solve it myself. I am writing this answer here in case somebody with the same problem finds this questuon. The solution is AJAX (asynchronus java script something). I simply pass name of the file with the PHP script to a HTTP request and I pass the argument via cookie. In JS I use document.cookie = "value = " + variable; and in PHP I use $_COOKIE['value']; to get the variable. Removing the row from DOM is also improved now. Instead of calculating row's id using viewport height and y-coordinate of the mouse click, I use event.target.parentNode.parentNode to get the tr element.

0

Please,I realy need to know this. At least send me a link to some online tutorial about it or simply some thread on different platform with similar question.

0

No, no. I can delelte the table row from my page with JavaScript. It's the part I already have. The problem is that I need to launch PHP function saved in different file and pass a single argument into it to delete the row from databese as well. BTW removing the row from my page is also quite bad. I get the vertical coordinates of the mouse click and then I calculate in which row a user clicked. Is there any better way to get parrent nodes of clicked button and remove them?