[英]PHP - Variable inside a nested if isset doesn't display
我有這兩個文件
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>";
}
?>
如果刪除第二個isset,將顯示$ age。
如果您意識到,在第二個條件中,我有兩個條件,第一個條件,必須單擊“是”按鈕,第二個條件,必須不單擊“發送”按鈕。
我已經嘗試了很多,但我沒有得到:(
PS我想在同一頁面上顯示它。 沒有其他頁面。 如果這不可能,那么我將在其他頁面中進行介紹。
您確實需要:
所以看起來像:
<?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";
}
?>
你的意思是。 您發送第一個表格。 然后加載另一頁進行確認。 一旦確認您已達到原來的年齡。 對?
這樣嘗試。
<?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!!! :(
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.