在不离开当前页面的情况下从 Javascript 调用 PHP 脚本
我在不离开当前 HTML 页面的情况下从 Javascript 调用 PHP 脚本时遇到了一些问题(如果可能的话).我知道可以使用 AJAX,但可以单独使用 Javascript 吗?
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?
上下文:-
我希望我的页面使用 Javascript 执行一个简短的动画(使用 onclick
),然后立即调用 PHP 脚本将数据插入 MySQL 数据库 - 所有这些都无需离开页面,因此不会抑制动画.
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.
我可以做的动画部分以及将数据插入数据库等,但是我如何在那个 Javascript 动画函数的末尾调用 PHP 脚本?
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 是异步 Javascript 和 XML,它是一种 Javascript 技术,允许您向服务器发送请求(就像您的浏览器在输入 URL 时所做的那样)并在 javascript 字符串中获得响应,而不是在页面中呈现它.
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.
问题是不同的浏览器实现 AJAX 的方式不一样,所以我建议使用 jQuery 进行抽象.
The problem is different browsers do not implement AJAX the same way, So I suggest using jQuery for abstraction.
用 jQuery 做这个:
do this with jQuery:
<script>
$.get("backend.php",{param:value},callbackFunction);
callbackFunction(data)
{
alert(data);
}
</script>
相关文章