Execute php file with javascript function
816
I want to execute php file in javascript function. Code is as shown below:
<a id="cancel" href="./?&_action=list" onclick="javascript:clearRecord();return rcm.command('list','',this,event)">Cancel</a>
<script language="javascript" type="text/javascript">
var u1='michael';
function clearRecord() {
$(function() {
location.href = 'clear.php?h='+u1;
});
}
</script>
But when I click to cancel button, clear.php not executed. How I should come out from this?
Answer
Solution:
full & working answer to your question:
Answer
Solution:
if you want to just execute some php and leave current page to the one generated by this php script. then you got it almost right. I do not see that you are using jquery - so skip this "$(function(){})" part, and i don't see what u1 is added for but this will work:
and this will do the same:
BUT if you want only to run "clear.php" and then reload current page. One way of doing it can be putting at the end of your "clear.php" file something like:
(it will work only if clear.php does't write anything in response).
But you can do it other ways:
- using AJAX - call clear.php with jQuery.get() and call location.reload() on success;
- using IFRAME - set iframe's location to clear.php and then call window.location.reload();
- using IMG - set img.src to clear.php ...
...and possibly many other ways :)