通过javascript调用php函数
问题描述:
好吧,我正在测试这个jQuery。我想从ajax运行我的另一个php文件。通过javascript调用php函数
<script type="text/javascript">
var switchOn = function() {
$.ajax({
url: '../remote/test.php',
type:'POST',
dataType:'text',
data: {test: 'Hello there!'},
success: function(data) {
document.write(data);
}
});
}
//Button functions
function changeState1()
{
if(window.document.myform.switch1[0].checked){
window.document.myform.switch1[1].checked = true;
document.myform.changeStateButton1.value = "Turn On";
switchOn();
}else{
window.document.myform.switch1[0].checked = true;
document.myform.changeStateButton1.value = "Turn Off";
switchOn();
}
}
function changeState2()
{
if(window.document.myform.switch2[0].checked){
window.document.myform.switch2[1].checked = true;
document.myform.changeStateButton2.value = "Turn On";
}else{
window.document.myform.switch2[0].checked = true;
document.myform.changeStateButton2.value = "Turn Off";
}
}
function changeState3()
{
if(window.document.myform.switch3[0].checked){
window.document.myform.switch3[1].checked = true;
document.myform.changeStateButton3.value = "Turn On";
}else{
window.document.myform.switch3[0].checked = true;
document.myform.changeStateButton3.value = "Turn Off";
}
}
function changeState4()
{
if(window.document.myform.switch4[0].checked){
window.document.myform.switch4[1].checked = true;
document.myform.changeStateButton4.value = "Turn On";
}else{
window.document.myform.switch4[0].checked = true;
document.myform.changeStateButton4.value = "Turn Off";
}
}
</script>
<form name="myform" action="index.php?p=remotecontrol" method="POST">
<b>On/Off</b>
<br>
Switch 1
<br>
<input type="radio" name="switch1" onClick="window.document.myform.switch.value = 'On'">
<input type="radio" name="switch1" onClick="window.document.myform.switch.value = 'Off'">
<input type="button" id="changeStateButton1" name="changeStateButton1" value="Turn On" onClick="changeState1()">
<br>
Switch 2
<br>
<input type="radio" name="switch2" onClick="window.document.myform.switch.value = 'On'">
<input type="radio" name="switch2" onClick="window.document.myform.switch.value = 'Off'">
<input type="button" id="changeStateButton2" name="changeStateButton2" value="Turn On" onClick="changeState2()">
<br>
Switch 3
<br>
<input type="radio" name="switch3" onClick="window.document.myform.switch.value = 'On'">
<input type="radio" name="switch3" onClick="window.document.myform.switch.value = 'Off'">
<input type="button" id="changeStateButton3" name="changeStateButton3" value="Turn On" onClick="changeState3()">
<br>
Switch 4
<br>
<input type="radio" name="switch4" onClick="window.document.myform.switch.value = 'On'">
<input type="radio" name="switch4" onClick="window.document.myform.switch.value = 'Off'">
<input type="button" id="changeStateButton4" name="changeStateButton4" value="Turn On" onClick="changeState4()">
<br>
</form>
这是我的其他php文件。
<?php
item1 = $_REQUEST['test'];
echo $item1;
?>
我确信代码到达ajax函数,但是在我的页面上没有任何事情发生。应该有来自另一个PHP文件的回声。 test.php位于Sites/remote,它们是第一个文件所在的目录。我试过url'../test.php'和'../remote/test.php'。没有区别...
答
从你发布的代码看来,你已经声明了PHP函数,但你永远不会打电话给他们。
在你的PHP文件你也应该有类似的东西:
if (isset($_GET['switch']))
{
$switch=$_GET['switch'];
if ("something" == $switch)
{
setSwitchOn($switch);
}
else
{
if ("something else" == $switch)
{
setSwitchOff($switch)
}
else
{
// some other code
}
}
}
答
callPage('setSwitch.php?switch='+targetSwitch ....
callPage('remote/setSwitch.php?switch='+tar ....
你有2 setSwitch.php,1远程和1相同的文件夹
和
switch='+targetSwitch,document.getElementById(targetId)
只是通过targetId而不是如何知道它关闭或打开..可能
switch='+targetSwitch,document.getElementById(targetId)+'&acrion=off'
和
function callPage(url, div){ //need 2 var
* “但它不工作” *是** **无用的问题/错误描述。请具体说明会发生什么以及您希望发生什么。一些提示:你有什么错误吗?在JavaScript或PHP方面?是否发送了Ajax请求?你有回应吗? – 2013-05-02 09:13:16
测试此代码时,您使用的浏览器是什么? – SaidbakR 2013-05-02 09:14:02
'exec('tdtool -n'。escapeshellarg($ switch));' – 2013-05-02 09:15:46