1

I would like to run a php function from a javascript code, to be more specific I got a button that delete a record from the database. The function that does that named

delete_post($id)

Here is what I tried:

<input type="submit" name="delete" value="delete" 
onClick="if(confirm('Are you sure?')) {<?php delete_post($row['id']);?>}">

When I click the button, there is no alert box. The funny thing is if I don't call a function inside the php code and I do something else such as echo the alert does pop out but the php code doesn't executed.

So, how can I do that? How can I run a php code inside my javascript onClick code.

4
  • 4
    PHP runs of the server, JS on the client. Your JS would need to talk to the server using AJAX for it to delete a record (unless you post a form that then loads the page again). Please consider security - row id alone is not secure. Commented Dec 1, 2013 at 20:56
  • the best way to go to avoid security flaws would be JS/ajax...split your code and have a seperate JS and PHP file...if you need help doing this, let me know...will whoop you up ans answer Commented Dec 1, 2013 at 21:36
  • @VyrenMedia I do need help, never used ajax before. Commented Dec 3, 2013 at 12:46
  • @ImriPersiado I added and answer check it out. Commented Dec 4, 2013 at 7:28

3 Answers 3

2

You can't. PHP is supposed to be run before the page loads, thus giving it the name Pre-Hypertext Protocol. If you want to run PHP after a page loads via JavaScript, the best approach would be linking to a new page that runs the PHP, then returning the user back.

file1.php:

...
<input type="submit" name="delete" value="delete" onClick="if(confirm('Are you sure?')) document.location.href='file2.php';">
...

file2.php:

<!doctype html>
<html>
<head>
<?php
delete_post($row['id']);
?>
<meta http-equiv="refresh" content="0; url=file1.php" />
</head>
<body>
<p>You will be redirected soon; please wait. If you are not automatically redirected, <a href="file1.php">click here</a>.</p>
</body>
</html>

Assuming you would have multiple IDs, you can keep them all onto one redirect page:

if(confirm('Are you sure?')) document.location='file2.php?id=2'; // file1.php
delete_post($row[$_GET["id"]]); // file2.php

But do not put PHP code directly into the query string, or your site could be susceptible to PHP injection

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer, but I have to point out that PHP now stands for "PHP: Hypertext Processor" (and used to stand for "Personal Home Page" from the name PHP Tools).
btw there is a big secure flaw in that way.. everyone will be able to load exmaple.com/fil2.php?id=x and they can change the x to every id they want to delete.
@11684 You can get data from AJAX, but since JavaScript runs client-side, you cannot edit the XML.
The PHP invoked by the AJAX call can do that though. @User7210510310410210511810133
1

You can't RUN php code in Javascript , but you can INVOKE it through JS/Ajax. For good practice split your php and JS , for example create a page that takes an ID and deletes it's row (i'm guessing your using REST) and invoke it through JS.

Cleaner , effective , and more secure

Comments

1

From your question, i would suggest you give jquery a try.

link to Jquery on your page's head section,

here is your js function

function deleteRow(id)
{
 var url='path/to/page.php';
 $("#loading_text").html('Performing Action Please Wait...');
 $.POST(url,{ row_id: id } ,function(data){  $("#loading_text").html(data) }); 
}

This should do it for you.

Since its a delete, am using $.post Let me know if you find any more issues

here is a link to jQuery hosted by google CDN //ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js

This is how your form should look like

<form>
<label for="Number"></label>
<input type="text" name="some_name" id="some_id" value="foo bar">
<input type="submit" name="delete" value="delete" 
onClick="javascript: deleteRow(the_id_of_the_row);">
</form>
<br>
<div id="loader_text"></div>

now your php page that does the delete could look like this

<?php
 $row_id = some_sanitisation_function($_POST['row_id']) //so as to clean and check user input
 delete_post($row_id);
 echo "Row deleted Successfully"; //success or failure message
?>

This should do it for you.

2 Comments

@ImriPersiado i expected you to handle the database connection. i dont know how ur tables are setup or how your connection is done...from your question, i saw a php function delete_post($id) i believe you had all that covered in the function....all you would do is include the file that has the function or have you not defined your delete_post($row_id) function yet?..if that is the case, let me know and i will guide you on how to go about it.
no problemo...hope your issue is sorted out now..@ImriPersiado

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.