2016-07-20 104 views
1

我有下面的代碼可以工作,但速度很慢,有沒有什麼建議可以改善週轉。PHP var to Javascript

怎樣纔可以有它僅接受數字1到10,並拒絕阿爾法

「H ** P://mydomai.any/handler.php MSG 1 = 10」 OK

「H ** p://mydomai.any/handler.php?msg1 = 14「不行

」h ** p://mydomai.any/handler.php?msg1 = HELLO「no ok(now it accept所有)

handler.php

<?php 
header("HTTP/1.1 200 OK"); 
if (isset($_REQUEST['msg1'])) { 
    $msg1 = $_REQUEST['msg1']; 
    ?> 
    <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script> 
    <script type="text/javascript" src="http://signage.me/demo/sendCommand.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      sendCommand("galaxy.signage.me", "username", "password", "13", "new1", (msg1 = <?php echo (json_encode($msg1)); ?>)); 
     }); 
    </script> 
    <?php 
} 
?> 
+1

檢查http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript也許有幫助 – Mostafa

+0

@Mostafa選擇技術來傳遞數據(JSON)是從該帖子。現在的問題是爲什麼它需要很長的時間來執行 – Chris

回答

0

is_numeric - 查找變量是數字還是數字字符串。

<?php 
header("HTTP/1.1 200 OK"); 
if (isset($_REQUEST['msg1']) && is_numeric($_REQUEST['msg1']) && $_REQUEST['msg1'] >=1 && $_REQUEST['msg1'] <=10) { 
    $msg1 = $_REQUEST['msg1']; 
    ?> 
    <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script> 
    <script type="text/javascript" src="http://signage.me/demo/sendCommand.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() 
     { 
      sendCommand("galaxy.signage.me", "username", "password", "13", "new1", (msg1 = <?php echo (json_encode($msg1)); ?>)); 
     }); 
    </script> 
    <?php 
} 
?> 
+0

謝謝阿米特,任何想法爲什麼它延遲? – Chris