在不离开当前页面的情况下从Javascript调用PHP脚本 [英] Calling PHP scripts from Javascript without leaving current page
问题描述
我无法在不离开当前HTML页面的情况下从Javascript调用PHP脚本时遇到一些麻烦(如果可能的话)。我明白这是可能的使用AJAX,虽然它可能使用Javascript单独吗?
上下文:
I希望我的页面使用Javascript(使用onclick)执行一个简短的动画,然后立即调用一个PHP脚本将数据插入到MySQL数据库中 - 所有这些都不会离开页面,因此它不会禁止动画。
我可以做的动画部分以及将数据插入到数据库等等,但是如何在JavaScript动画函数结束时调用PHP脚本?
任何指针,代码片段等将不胜感激! ^ _ ^
如果此问题已被询问,请致歉。
< AJAX是异步JavaScript和XML,
它是一种Javascript技术,它允许您向服务器发送请求(如您的浏览器在输入URL时所做的那样),并以JavaScript字符串形式响应,而不是呈现它在页面中。
问题是不同的浏览器没有以同样的方式实现AJAX,所以我建议使用jQuery进行抽象。
使用jQuery做到这一点:
< script>
$ .get(backend.php,{param:value},callbackFunction);
callbackFunction(data)
{
alert(data);
}
< / script>
I'm having some trouble calling PHP scripts from Javascript without leaving the current HTML page (if it is at all possible). I understand it is possible using AJAX, although is it possible using Javascript alone?
Context:-
I want my page to perform a short animation using Javascript (using onclick), then immediately call a PHP script to insert data into a MySQL database - all without leaving the page so it does not inhibit the animation.
The animation part I can do and the inserting the data into the database, etc. but how can I call a PHP script at the end of that Javascript animation function?
Any pointers, code fragments, etc. would be greatly appreciated! ^_^
Apologies if this question has been asked previous.
AJAX is Asynchronous Javascript And XML, Its a Javascript technology that allows you to send a request to the server (as your browser does when you enter a URL) and have the response in a javascript string instead of rendering it in the page.
The problem is different browsers do not implement AJAX the same way, So I suggest using jQuery for abstraction.
do this with jQuery:
<script>
$.get("backend.php",{param:value},callbackFunction);
callbackFunction(data)
{
alert(data);
}
</script>
这篇关于在不离开当前页面的情况下从Javascript调用PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!