2017-10-16 149 views
-3

我有一個表單,在頁面中我必須驗證是否發佈了值。但是,當我打印郵政陣列時,我得到以外的if條件。 當我嘗試在if條件內使用print_r()時,它不起作用。我使用的代碼如下:提交PHP驗證表單

<?php 
print_r($_POST); 
if (isset($_POST['submit'])) { 
    print_r($_POST); // Not Working 
} 
?> 
<form method="post" action="anotherpage.php"> 
<input type="text" name="course" /> 
<input type="text" name="location" /> 
<input type="submit" name="submit" value="GO"/> 
</form> 

我也試過在窗體中使用隱藏的輸入併發布它。它也沒有工作。

+0

那是因爲你張貼的形式'anotherpage.php ' - 不是到這個頁面。 – shahsani

+0

把'if(isset($ _ POST ['submit'])){...}'代碼放在'anotherpage.php' – shahsani

+0

是否可以在將值發送到另一個頁面之前驗證同一頁面中的表單 – user2634873

回答

0

您的表單張貼到anotherpage.php,而不是相同的頁面。

你可以改變你的<form>張貼到同一頁:

<form method="post"> 
... 
</form> 

或者把你的PHP代碼中anotherpage.php

<?php 
print_r($_POST); 
if (isset($_POST['submit'])) { 
    print_r($_POST); 
} 
?>