通过javascript调用php函数

通过javascript调用php函数,php,javascript,ajax,Php,Javascript,Ajax,好的,我正在尝试测试这个jQuery。我想从ajax运行我的另一个php文件 <script type="text/javascript"> var switchOn = function() { $.ajax({ url: '../remote/test.php', type:'POST', dataType:'text', data: {test: 'Hello there!'}, success: function(data) {

好的,我正在尝试测试这个jQuery。我想从ajax运行我的另一个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>

var switchOn=函数(){
$.ajax({
url:“../remote/test.php”,
类型:'POST',
数据类型:'text',
数据:{test:'你好!'},
成功:功能(数据){
文件。写入(数据);
}
});
}
//按钮功能
函数changeState1()
{
if(window.document.myform.switch1[0]。选中){
window.document.myform.switch1[1]。选中=true;
document.myform.changeStateButton1.value=“打开”;
开关();
}否则{
window.document.myform.switch1[0]。选中=true;
document.myform.changeStateButton1.value=“关闭”;
开关();
}
}
函数changeState2()
{
if(window.document.myform.switch2[0]。选中){
window.document.myform.switch2[1]。选中=true;
document.myform.changeStateButton2.value=“打开”;
}否则{
window.document.myform.switch2[0]。选中=true;
document.myform.changeStateButton2.value=“关闭”;
}
}
函数changeState3()
{
if(window.document.myform.switch3[0]。选中){
window.document.myform.switch3[1]。选中=true;
document.myform.changeStateButton3.value=“打开”;
}否则{
window.document.myform.switch3[0]。选中=true;
document.myform.changeStateButton3.value=“关闭”;
}
}
函数changeState4()
{
if(window.document.myform.switch4[0]。选中){
window.document.myform.switch4[1]。选中=true;
document.myform.changeStateButton4.value=“打开”;
}否则{
window.document.myform.switch4[0]。选中=true;
document.myform.changeStateButton4.value=“关闭”;
}
}
开/关

开关1

开关2

开关3

开关4

这是我的另一个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
        }
    }
}
您有2个setSwitch.php,1个在remote中,1个在同一文件夹中

只需传递targetId,而不是如何知道它的开关..也许

switch='+targetSwitch,document.getElementById(targetId)+'&acrion=off'


“但它不工作”是一个无用的问题/错误描述。请具体说明发生了什么以及您预期会发生什么。一些提示:你有什么错误吗?在JavaScript还是PHP方面?是否发送了Ajax请求?您是否收到响应?在测试此代码时使用的浏览器是什么?
exec('tdtool-n'.escapeshellarg($switch))@FelixKling同意。请通过解释您的问题来节省我们一些时间,而不是期望我们也解决这个问题。我没有错误,似乎我的php文件从未被调用过
switch='+targetSwitch,document.getElementById(targetId)
switch='+targetSwitch,document.getElementById(targetId)+'&acrion=off'
function callPage(url, div){ //need 2 var