简体   繁体   English

通过javascript调用php函数

[英]call a php function through javascript

Ok, I am trying to test this jQuery. 好的,我正在尝试测试此jQuery。 I want to run my other php file from ajax. 我想从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>

This is my other php file. 这是我的其他php文件。

<?php
item1 = $_REQUEST['test'];

    echo $item1;


?>

I am sure that the code gets to the ajax function, but then there are nothing happening on my page. 我确定代码可以到达ajax函数,但是页面上什么也没有发生。 There are supposed to be an echo from the other php file. 应该是其他php文件的回声。 The test.php is in Sites/remote, which are the same directory the first file are. test.php在Sites / remote中,这是第一个文件所在的目录。 I have tried url '../test.php' and '../remote/test.php'. 我已经尝试了网址“ ../test.php”和“ ../remote/test.php”。 No differences... 没什么不同

from the code you posted it seems that you have the PHP functions declared, but you never call them. 从发布的代码来看,似乎已经声明了PHP函数,但从未调用过它们。

in your php file you should also have something similar to: 在您的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 ....

you have 2 setSwitch.php , 1 in remote and 1 in same folder 您有2个setSwitch.php,其中1个位于远程目录中,而1个位于同一文件夹中

AND

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

just pass targetId than how to know it off or on..maybe 只是传递targetId而不是如何打开或关闭它。.也许

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

AND

function callPage(url, div){ //need 2 var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM