admin管理员组文章数量:1415664
OK, not really sure if this is possible but thought I'd ask anyway :)
I have a HTML page which contains some information and a link. The link calls a JavaScript confirm dialog box. If the user selects OK then I want to call a function in a PHP file, but in the background, i.e I don't want to change page, reload or anything.
Is this possible?
Thanks
T
OK, not really sure if this is possible but thought I'd ask anyway :)
I have a HTML page which contains some information and a link. The link calls a JavaScript confirm dialog box. If the user selects OK then I want to call a function in a PHP file, but in the background, i.e I don't want to change page, reload or anything.
Is this possible?
Thanks
T
Share Improve this question asked May 18, 2012 at 16:38 user913059user913059 5891 gold badge4 silver badges19 bronze badges2 Answers
Reset to default 6Use AJAX. The following uses jQuery:
if(confirm('Are you sure?')) {
$.ajax({
url: '/path/to/file.php',
data: 'url=encoded&query=string', // Can also be an object
success: function(output) {
// This function is called after the PHP file pletes.
// The variable passed in is a string containing all output of
// your PHP script (i.e. echo / print )
}
});
}
For more information, check out jQuery's documentation for AJAX. If you cannot use jQuery, there are many tutorials on using native JavaScript to make AJAX Requests.
If you are returning a large amount of data back to your success function from PHP, it would be worthwhile for you to return the data from PHP as a JSON encoded array, which can be safely parsed client-side into a JavaScript object.
$('.LinkClass').click(function(){
if( confirm('Is it OK?') ) {
$.ajax({
url: 'action.php',
type: 'POST',
data: 'data=value', // Can also be an object
success: function(data) {
// Do Nothing
},
error: function(){
alert('Error');
}
});
}
});
本文标签: Use JavaScript confirm dialog to make a PHP call in the backgroundStack Overflow
版权声明:本文标题:Use JavaScript confirm dialog to make a PHP call in the background - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745196601a2647170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论