从JavaScript函数调用PHP类方法
问题说明
可能重复:
从javascript调用php函数
我知道php是服务器端和JavaScript是客户端。但我想知道在调用JavaScript函数时如何运行PHP方法。下面是我的代码,我知道错误是但我怎么能执行php方法?
I understand that php is server side and JavaScript is client side. But I would like to know how to run a PHP method when a JavaScript function is called. Below is my code, I know the error is but how can I perform the php method?
<script type="text/javascript">
function toggle()
{
var ele = document.getElementById("addCatTextBox");
var text = document.getElementById("addCatButtonText");
if(ele.style.display == "block") {
ele.style.display = "block";
text.innerHTML = "Save category";
<?php Category::addCategory($inCatName)?>
}
else {
ele.style.display = "none";
text.innerHTML = "Add new category";
}
}
</script>
感谢您的帮助。
正确答案
使用Prototype库(www.prototypejs.org):
Using the Prototype library (www.prototypejs.org):
Javascript:
Javascript:
<script type="text/javascript">
function toggle()
{
var ele = document.getElementById("addCatTextBox");
var text = document.getElementById("addCatButtonText");
if(ele.style.display == "block") {
ele.style.display = "block";
text.innerHTML = "Save category";
var options={
method: 'get',
parameters: 'inCatName=' ele.value,
onSuccess: function(xhr) {
// TODO: Whatever needs to happen on success
alert('it worked');
},
onFailure: function(xhr) {
// TODO: Whatever needs to happen on failure
alert('it failed');
}
};
new Ajax.Request('addCategory.php', options);
}
else {
ele.style.display = "none";
text.innerHTML = "Add new category";
}
}
</script>
addCategory.php:
addCategory.php:
<?php
$inCatName=isset($_REQUEST["inCatName"]) ? $_REQUEST["inCatName"] : null;
Category::addCategory($inCatName);
?>
我们的想法是Javascript向addCategory发送GET(或者可能是POST)请求。幕后的php页面,传递它创建类别所需的任何信息。
The idea is that the Javascript sends a GET (or it could be POST) request to the addCategory.php page behind the scenes, passing it whatever info it needs to create the category.
希望这足以让你前进。我的代码中缺少很多东西 - 您需要验证变量addCategory.php接收并执行任何其他相关的安全检查,然后再将其放在数据库附近。 addCategory.php还需要任何包含文件等,以便它知道你的Category类。最后,addCategory.php应该将某种形式的变量返回到调用它的Javascript代码,以便它知道结果是什么。
Hopefully this is enough to get you going. There's a lot missing from my code - you'll need to validate the variables addCategory.php receives and perform any other pertinent security checks before letting it anywhere near the database. addCategory.php will also require any include files, etc so that it knows about your Category class. Finally, addCategory.php should really return some form of variable back to the Javascript code that called it so that it knows what the outcome was.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfgaab
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22