tmyonline Posted June 16, 2010 Share Posted June 16, 2010 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. } Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/ Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 Look into AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072748 Share on other sites More sharing options...
tmyonline Posted June 16, 2010 Author Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072752 Share on other sites More sharing options...
kratsg Posted June 16, 2010 Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072754 Share on other sites More sharing options...
tmyonline Posted June 16, 2010 Author Share Posted June 16, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072758 Share on other sites More sharing options...
kratsg Posted June 16, 2010 Share Posted June 16, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072760 Share on other sites More sharing options...
haku Posted June 16, 2010 Share Posted June 16, 2010 The non-ajax solution would be to set the HREF of your <a> tag to be a PHP script that will delete the data in question. You can then add a javascript confirmation to the link. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072762 Share on other sites More sharing options...
PFMaBiSmAd Posted June 16, 2010 Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072763 Share on other sites More sharing options...
tmyonline Posted June 16, 2010 Author Share Posted June 16, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072765 Share on other sites More sharing options...
tmyonline Posted June 16, 2010 Author Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072769 Share on other sites More sharing options...
kratsg Posted June 16, 2010 Share Posted June 16, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072775 Share on other sites More sharing options...
tmyonline Posted June 16, 2010 Author Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072779 Share on other sites More sharing options...
kratsg Posted June 16, 2010 Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072780 Share on other sites More sharing options...
tmyonline Posted June 16, 2010 Author Share Posted June 16, 2010 OK kratsg, I'm going to try it first thing in the morning when I'm at work. It does look like a very handy solution, clean and short. If it works, I cannot thank enough! Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072781 Share on other sites More sharing options...
jcbones Posted June 16, 2010 Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204907-how-to-call-php-from-javascript/#findComment-1072789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.