Jump to content

Recommended Posts

Guys:  Suppose I have the following script:

<?php
  ...
  echo '<a href="" onclick="deleteInfo('.$id.');">X</a>';
  ...
?>

// In JavaScript
function deleteInfo (recordID) {
  var response = confirm("Are you sure you want to delete this record");
  if (response)
     how to call PHP from here so that I can delete this record ?  Thanks.
}

Link to comment
https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/
Share on other sites

AlexWD, thanks for your suggestion.  I do believe Ajax is the answer.  Unfortunately, I'm in a situation that I have to use a non-Ajax solution although I know Ajax.  We are still using PHP 5.1.x which does not have Ajax support.  Is there any non-Ajax solution to either call PHP from JavaScript or delete a record, with id = recordID, from database directly ?  Thanks.

AlexWD, thanks for your suggestion.  I do believe Ajax is the answer.  Unfortunately, I'm in a situation that I have to use a non-Ajax solution although I know Ajax.  We are still using PHP 5.1.x which does not have Ajax support.  Is there any non-Ajax solution to either call PHP from JavaScript or delete a record, with id = recordID, from database directly ?  Thanks.

 

The version of PHP doesn't affect whether or not you can use AJAX (to the best of my knowledge). As long as you have JavaScript enabled, it will work.

The version of PHP doesn't affect whether or not you can use AJAX (to the best of my knowledge). As long as you have JavaScript enabled, it will work.

 

Is it true ?

 

Assuming that it is, is there not a non-Ajax approach as an alternative solution for this ?

Here's the thing - if you have to use JavaScript, then AJAX is an option (I can't imagine why it wouldn't be?). If you can't use AJAX, then use JavaScript to redirect the user to a new page that contains PHP code for deleting the record:

 

window.location = 'http://www.somenewurl.com/somescript.php?id={SOMEID}';

 

The version of PHP doesn't affect whether or not you can use AJAX (to the best of my knowledge). As long as you have JavaScript enabled, it will work.

 

Is it true ?

 

Assuming that it is, is there not a non-Ajax approach as an alternative solution for this ?

Browsers can only make HTTP requests to web servers. Using AJAX just means that the HTTP request is asynchronous (i.e. in the background) to what the browser is displaying in the foreground.

 

So, you cannot directly call a php function from a browser. You must request a URL that causes the web server to process a php page and the php code on that page determines what action to take.

kratsg, you are coming down the path that I went through!  The thing is, if I do:

 

function deleteInfo(recordID) {
  var response = confirm("Are you sure you want to delete this record?");
  if (response)
    window.location.href = "index.php?action=delete&id=" + recordID;
}

 

It will NOT re-direct the url.  I tried it many times this morning.  This is what I experienced, if you make the line window.location.href = ... the very first line inside the deleteInfo(), yes, it will re-direct the url.  But, the fact that this line is nested inside the if statement, it somehow fails to re-direct.  Try it for yourself.  That's why I've been so frustrated!

So, you cannot directly call a php function from a browser. You must request a URL that causes the web server to process a php page and the php code on that page determines what action to take.

 

OK, so how do I re-direct the URL then ?  I tried

 

window.location.href = "index.php?action=delete&id=" + recordID

 

but it failed to re-direct!  Thanks.

There's an even easier way!

 

<a href="yoururl.php?id=ID" onclick="return confirm('Are you sure you want to delete this record?');">X Record</a>

 

kratsg, you are coming down the path that I went through!  The thing is, if I do:

 

function deleteInfo(recordID) {
  var response = confirm("Are you sure you want to delete this record?");
  if (response)
    window.location.href = "index.php?action=delete&id=" + recordID;
}

 

It will NOT re-direct the url.  I tried it many times this morning.  This is what I experienced, if you make the line window.location.href = ... the very first line inside the deleteInfo(), yes, it will re-direct the url.  But, the fact that this line is nested inside the if statement, it somehow fails to re-direct.  Try it for yourself.  That's why I've been so frustrated!

There's an even easier way!

 

<a href="yoururl.php?id=ID" onclick="return confirm('Are you sure you want to delete this record?');">X Record</a>

 

kratsg, I think this code will fail!  I like your idea in attaching the question to the onclick attribute but, think about it, no matter you click "OK" or "Cancel" in response to the question, it will go ahead and do the deletion.

 

 

Not at all! I used a "return confirm". If I have a link like so:

<a href="someurl.php" onclick="return false;">Link</a>

 

It won't work. Try it. if I change it to 'true', it works:

 

<a href="someurl.php" onclick="return true;">Link</a>

 

kratsg, I think this code will fail!  I like your idea in attaching the question to the onclick attribute but, think about it, no matter you click "OK" or "Cancel" in response to the question, it will go ahead and do the deletion.

You could always try:

 

<?php
if(isset($_GET['cmd']) && $_GET['cmd'] == 'delete') {
echo 'I\'m running the delete code for ' . $_GET['id'] . '!';
}
?>
<a href="?id=10&cmd=delete" onclick="return confirm('Are you sure you want to delete?');">Run the delete function</a>

 

This will reload the page, where the function is ready to run the delete for you.  Not as elegant as Ajax, but it will work.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.