2013-09-28 40 views
1

場景:如何使用PHP使用JavaScript?

我創建使用IF ELSE在當用戶點擊BOX A和盒子B ......然後,假設箱A已經滿了,用戶點擊「保存按鈕」後,確認消息會過濾顯示器說...... 「你選擇的盒子已滿,你想保存其他項目嗎?」

這裏是我的代碼:

<?php 
//else if...... 
    else if($box== "BOX A" && $row[item] == "100") 
    { 

     echo "<script type='text/javascript'>"; 
     echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')"; 
     echo "if (r==true){"; 
     echo "alert('You pressed OK!')}"; 
     echo "else{"; 
     echo "alert('You pressed Cancel!')}"; 
     echo "</script>"; 

    } 
?> 

我有問題的代碼,當我嘗試使用echo "enter here"; 對其進行跟蹤其隨時停止程序閱讀這個代碼後

echo "<script type='text/javascript'>"; 

然後它會跳到這裏:

echo "</script>"; 

結果:

它沒有顯示彈出消息...所以我需要的是顯示彈出消息,我不知道爲什麼,爲什麼它不顯示......沒有按鈕涉及顯示確認信息......如果用戶想要繼續保存,如果其中一個盒子已滿,如果不是他/她將按下取消,則它將提醒用戶...

注意: 我只是PHP和Javascript的初學者..

在此先感謝您。

+2

您需要從基本的PHP教程開始。 –

+1

刪除'<?php'和'?>',因爲您已經在使用PHP – GroovyCarrot

+0

@GroovyCarrot ...對不起,我已經修改了我的代碼...我只是錯過了複製它...... – user2826499

回答

7

這是因爲你開始內的另一個

else if($box== "BOX A" && $row[item] == "100") 
{ 
    <?php 
    echo "<script type='text/javascript'>"; 
    echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')"; 
    echo "if (r==true){"; 
    echo "alert('You pressed OK!')}"; 
    echo "else{"; 
    echo "alert('You pressed Cancel!')}"; 
    echo "</script>"; 
    ?> 
} 

<?/<?php標籤應該是

else if($box== "BOX A" && $row[item] == "100") 
{ 
    echo "<script type='text/javascript'>"; 
    echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')"; 
    echo "if (r==true){"; 
    echo "alert('You pressed OK!')}"; 
    echo "else{"; 
    echo "alert('You pressed Cancel!')}"; 
    echo "</script>"; 
} 

但我個人認爲

else if($box== "BOX A" && $row[item] == "100") 
    { 
?> 
<script> 
var r=confirm('The Box you chosen is already full. Do you want to save the other items?')"; 
if (r==true) { 
    alert('You pressed OK!'); 
} else { 
    alert('You pressed Cancel!'); 
} 
</script> 
<? 
    } 

是更清潔,更具有可讀性。保持JavaScript不在PHP中,如果可以避免,不要將它回顯出來。在6個月內進行調查可能很難。

+0

@davidkonrad ...嗨對不起..我剛剛修改我的代碼...對於晚編輯抱歉... – user2826499

+0

@davidkonrad ...我已經嘗試過,但我有一個錯誤...「解析錯誤:語法錯誤,意外的文件結束「當我試圖做同樣的事情時...... – user2826499

+0

@davidkonrad ......已經在工作......非常感謝你 – user2826499

0
$str = <<<MY_MARKER 
<script type="text/javascript"> 
document.write("Hello World!"); 
</script> 
MY_MARKER; 
echo $str; 
2

或者你可以做這樣的事情。

<?php 
    //some PHP code 
    if($box== "BOX A" && $row[item]== 100) 
    { 
    ?> 

     <script type='text/javascript'> 
     var r=confirm('The Box you chosen is already full. Do you want to save the other items?'); 
     if (r==true){ 
     alert('You pressed OK!')} 
     else{ 
     alert('You pressed Cancel!')} 
     </script> 

    <?php 
    } 
    ?> 
+1

是的,很好的inmo – davidkonrad

0

你的問題基本上是因爲當你echo'ing您的JavaScript你忘記了;角色仍然需要繼續JS線:

<?php 
//else if...... 
    else if($box== "BOX A" && $row[item] == "100") 
    { 

     echo "<script type='text/javascript'> 
       var r = confirm('The Box you chosen is already full. Do you want to save the other items?'); // <- this was missing 
       if (r == true){ 
        alert('You pressed OK!'); 
       } 
       else{ 
        alert('You pressed Cancel!'); 
       } 
       </script>"; 

    } 
?> 

小動作我用的是PHP回聲允許新的行不破壞,所以你可以隨時回聲一塊代碼,並查看它更容易

2

我假設你已經把php標籤和一條if語句之前,這條線

else if($box== "BOX A" && $row[item] == "100") 

事實是,你不需要打開另一個php標籤。您已經在使用PHP編寫代碼,因此您不需要再指定它,因爲它會導致代碼失敗。你可以寫這樣的東西來代替。

else if($box == "BOX A" && $row[item] == "100") 
{ ?> 
    <script type='text/javascript'> 
    var r=confirm('The Box you chosen is already full. Do you want to save the other items?'); 
    if (r==true) 
     alert('You pressed OK!'); 
    else 
     alert('You pressed Cancel!'); 
    </script> 
    <?php 
}