2014-12-30 49 views
1

我正在使用樹莓派脈衝繼電器(在下面的表單提交)到另一臺設備,如果成功脈衝將打開/關閉一個繼電器。切換的繼電器返回到pi,作爲在加載div內的status.php頁面上監視的輸入。如果我加載下面的頁面,它會正確顯示status.php,但是在按下表單提交按鈕後,它不會重新加載status.php頁面。我嘗試了所有我能想到的,請幫助!PHP/Javascript/Python不能重新加載div

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Alarm Panel</title> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> 
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> 
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
    <script type="text/javascript">// <![CDATA[ 
    $(document).ready(function() { 
        $.ajaxSetup({ cache: false }); 
    setInterval(function() { 
    $('#loading').load('/status.php'); 
    }, 2000); 
    }); 
// ]]> 
</script> 
    <?php 
    if (isset($_POST['PULSE'])) { 
      shell_exec('python /usr/lib/cgi-bin/pulse.py'); 
     } 
    ?> 
</head> 
<body> 
    <div id="currentstatus" data-role="collapsible" data-theme="b" data-content-theme="b" data-collapsed="false"> 
    <h1>Current status</h1> 
    <div id="loading" align="center"> 
     <H3>Loading Status...</H3> 
    </div> 
    </div> 
    <div data-role="collapsible" data-theme="b" data-content-theme="b"> 
    <h4>Change Status</h4> 
    <form method="post"> 
     <input type="submit" name="PULSE" value="PULSE" /> 
    </form> 
    </div> 
    </body> 
    </html> 

status.php <?php $status = shell_exec('python /usr/lib/cgi-bin/status.py'); ?> <h3><? echo $status; ?></h3> <br /> <img src="/img/<? print $status; ?>.jpg" height="250"; width="250";>

+0

你試圖設置窗體的'行動= '?''?這似乎是表格不提交給它的父文件... – Todd

+0

大聲笑。實際上,我認爲這可能是無關緊要的。讓我嘗試一個真正的解決方案... – Todd

+0

什麼是status.php? – OIS

回答

0

你試圖操縱行爲提交?這可能會解決它。

$(document).on('click', '[name="pulse"]', function(e) { 
    e.preventDefault(); 
    $('#loading').load('/status.php'); 
}); 

你也可以嘗試改變你的超時函數......這有點棘手,因爲加載是一個異步函數。

var loadStatusTimeout; 
function loadStatus() { 
    $('#loading').load('/status.php', function() { 
     loadStatusTimeout = setTimeout(loadStatus, 2000); 
    }); 
} 
loadStatusTimeout = setTimeout(loadStatus, 2000); 

也,你不小心將分號到您的img元素:

變化:

<img src="/img/<? print $status; ?>.jpg" height="250"; width="250";> 

到:

<img src="/img/<? print $status; ?>.jpg" height="250" width="250"> 
+0

我嘗試在與document.ready相同的腳本標記中添加它,但在提交後仍然無法正常工作。脈衝的帖子仍然有效,只是不是刷新加載。 –

+0

看看編輯是否有幫助。更多來 – Todd

+0

謝謝我刪除了分號。 我將加載腳本更改爲以下內容,但沒有效果: ''' 我還然後嘗試添加 '回聲 '<腳本類型= 「文本/ JavaScript的」>' 'loadstatus();' ,'';' –