drgwsx8405 2013-05-02 09:11
浏览 74

通过javascript调用php函数

Ok, I am trying to test this jQuery. I want to run my other php file from ajax.

<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
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. There are supposed to be an echo from the other php file. The test.php is in Sites/remote, which are the same directory the first file are. I have tried url '../test.php' and '../remote/test.php'. No differences...

  • 写回答

2条回答 默认 最新

  • dongzhuoxie1244 2013-05-02 09:19
    关注

    from the code you posted it seems that you have the PHP functions declared, but you never call them.

    in your php file you should also have something similar to:

    if (isset($_GET['switch']))
    {
        $switch=$_GET['switch'];
        if ( "something" == $switch )
        {
            setSwitchOn($switch);
        }
        else
        {
            if ( "something else" == $switch )
            {
                setSwitchOff($switch)
            }
            else
            {
                // some other code
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿