2012-11-08 103 views
0

我有這樣兩個文件PHP - 變量裏面嵌套如果isset不顯示

a.php只會

<? 

echo " 
    <form action = 'B.php' method = 'post'> 
      Age: <input type = 'text' name = 'age'> 
      <input type = 'submit' name = 'send' value = 'send'> 
     </form> 
"; 

?> 

B.php

<? 

    $age = $_REQUEST ['age']; 

    if (isset($_POST['send'])){ 
     echo "Are you sure you wanna send this age?"; 
     echo " 
      <form action = 'B.php' method = 'post'> 
      <input type = 'submit' name = 'age2' value = 'YES'> 
      "; 

       if (isset($_POST['age2']) && !isset($_POST['send'])){ 
        echo "Your final age is".$age; //It doesn't display!!! :(
       } 
      echo "</form>"; 
    } 
?> 

如果我刪除了第二個ifset, $年齡將被顯示。

如果你意識到,在第二個isset中我有兩個條件,第一個,必須點擊YES按鈕,第二個,發送按鈕不能被點擊。

我已經嘗試了許多這和我沒有得到這個:(

PS我想在同一個頁面中顯示它。沒有其他頁面。 如果這是不可能的這一點,那麼我會在其他頁面。

+0

凡在窗體的AGE2歲? –

+0

age2是確認年齡 – seRgiOOOOOO

+0

的第二個按鈕,對不起,我的錯誤,它不是prueba-2,它是B,已編輯 – seRgiOOOOOO

回答

0

你的意思是,你送的第一種形式,然後加載其他頁面進行確認。一旦你確認你的原始時代,對嗎?

試試這個樣子。

<?php 
    $age = $_POST['age']; 
    if (isset($_POST['send'])): 
?> 
    Are you sure you want to send this age? 
    <form action = 'b.php' method = 'post'> 
    <input type = 'hidden' name = 'age' value = '<?php echo $age; ?>'> 
    <input type = 'submit' name = 'age2' value = 'YES'> 

<?php 
    endif; 
    // This wont show up if the first form is sent 
    if (isset($_POST['age2'])){ 
     echo "Your final age is ".$_POST['age']; //It does display!!! :(
    } 

?> 
+0

它不顯示年齡:(,我剛剛試過,沒有:( – seRgiOOOOOO

+0

你最後一次編輯不起作用:(回聲「你的最後一個年齡是」。$ _ POST ['age'];) – seRgiOOOOOO

+0

嗯給我一秒來檢查代碼,你應該檢查馬里奧的答案,它的方式更加乾淨:) –

3

您的確需要:

  • 拆分出第二個if。這不可能都是事實。按鈕1被按下,或者不按鈕。
  • 對以前的變量進行<input type=hidden>
  • 瞭解字符串和heredoc語法。
  • 修復你的可怕的indendation。

所以它看起來像:

<?php 

    $age = $_REQUEST['age']; 

    if (isset($_POST['send'])) { 

     echo <<<END 
      Are you sure you wanna send this age? 
      <form action='B.php' method='POST'> 
       <input type='submit' name='age2' value='YES'> 
       <input type=hidden name=age value='$age'> 
      </form> 
END; 

    } 

    if (isset($_POST['age2'])) { 
     echo "Your final age is $age"; 
    } 

?> 
+0

它但是在我的課堂上,我們仍然沒有使用heredoc,我對此沒有任何想法,還有其他的選擇嗎?如果不可能,非常感謝! – seRgiOOOOOO

+0

您可以使用雙引號字符串,但如果在公共網站上使用這個函數,還要注意轉義輸出('htmlspecialchars()')或過濾輸入(這裏是'intval()')。 – mario