2013-05-02 123 views
1

好吧,我正在測試這個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'。沒有區別...

+6

* 「但它不工作」 *是** **無用的問題/錯誤描述。請具體說明會發生什麼以及您希望發生什麼。一些提示:你有什麼錯誤嗎?在JavaScript或PHP方面?是否發送了Ajax請求?你有迴應嗎? – 2013-05-02 09:13:16

+0

測試此代碼時,您使用的瀏覽器是什麼? – SaidbakR 2013-05-02 09:14:02

+0

'exec('tdtool -n'。escapeshellarg($ switch));' – 2013-05-02 09:15:46

回答

0

從你發佈的代碼看來,你已經聲明瞭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 
     } 
    } 
} 
0
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