2015-03-31 48 views
0

我們可以用不同的頁面使用$ _POST 2次嗎? 這個例子..

action.php的

<form action="next.php" method="post"> 
<input name="test" value="test" /> 
<button type="submit" name="test">submit</button> 
</form> 

next.php

<?php 
$test=$_POST['test']; 
?> 
<form action="next1.php" method="post"> 
<input name="test1" value="<?php echo $test; ?>" /> 
<button type="submit" name="test1">submit</button> 
</form> 

next1.php

<?php 
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ? 
?> 
+0

你想要在next1.php文件中打印兩個文本字段值? – RaMeSh 2015-03-31 08:39:50

+0

**危險**:此代碼[易受XSS影響](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS))。用戶輸入需要在插入HTML文檔之前進行轉義! – Quentin 2015-03-31 08:56:30

+0

@Michael Halomoan Sihoml,是的,你可以在代碼中使用$ _POST兩次。更多細節請參閱我的答案。 – RaMeSh 2015-03-31 09:18:41

回答

1

next.phpaction.php你需要改變你的輸入型等作爲

<input type="text" name="test" value="test" /> 
<input type="text" name="test1" value="<?php echo $test; ?>" /> 

Wi薄next.phpaction.php的你缺少類型attr輸入標籤

,你有submitinput相同name attributes next.php需要內刪除或更改從值分別爲

<input type="submit" value="submit"/> 
+0

類型屬性也沒有在action.php中給出,爲什麼它的工作? – 2015-04-02 06:07:23

+0

@JithinJose你能指出我在哪裏 – 2015-04-02 06:08:32

+0

in action.php 2015-04-02 06:09:57

-1

您需要再次以第二種形式發送POST值,例如在隱藏字段中,否則該值不會被髮送。

此外,您的提交按鈕不應與您想要內容的輸入字段具有相同的名稱。因此,改變你的提交按鈕的名稱,例如:

action.php的

<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form> 

next.php

<form action="next1.php" method="post"> 
<input name="test1" value="<?php echo $test; ?>" /> 
<button type="submit" name="submit">submit</button> 
</form> 
+0

對不起朋友,我不明白你的答案。你可以用代碼解釋/編輯我的代碼嗎? – 2015-03-31 08:30:49

+0

當然,我會更清楚地解釋一下。 next.php中的提交按鈕名稱爲'test1',並且沒有值。它在輸入字段後面列出了測試值所在的位置。由於提交按鈕和測試值具有相同的名稱,但提交按鈕在末尾列出並且沒有值,因此您將不會收到任何值,如$ _POST [ 'TEST1']。爲了「修復」它,像我在答案中那樣重命名提交按鈕。 – manniL 2015-03-31 08:31:19

0

下面的代碼我試過我的本地服務器,它成功執行。

action.php的: -

<form action="next.php" method="POST"> 
<input type="text" name="test"/> 
<input type="submit" value="submit"/> 
</form> 

next.php: -

<?php 
$test = $_POST['test']; 
echo $test; 
?> 
<form action="next1.php" method="POST"> 
<input name="test1" value="<?php echo $test; ?>" /> 
<input type="submit" value="submit"/> 
</form> 

next1.php: -

<?php 

$test2=$_POST['test1']; 

echo "------".$test2; 
?> 

希望這將^ h elp.I認爲這已經達到您的要求。

0

如果你想在一個文件中做到這一點,例如索引。php那麼這裏是代碼

<?php 
if(isset($_POST['test'])){ 
    if(isset($_POST['test_text'])){ 
     echo 'Output from NEXT: '.$_POST['test_text']; 
    } 
} 
elseif(isset($_POST['test1'])){ 
    if(isset($_POST['test_text1'])){ 
     echo 'Output from NEXT1: '.$_POST['test_text1']; 
    } 
} 
?> 
<table style="width:100%;"> 
    <tr > 
     <td style="width:50%;"> 
      <form action="" method="post" name="next"> 
      <input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" /> 
      <button type="submit" name="test">submit test1</button> 
      </form> 
     </td> 
     <td style="width:50%;"> 
      <form action="" method="post" name="next1"> 
      <input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" /> 
      <button type="submit" name="test1">submit test1</button> 
      </form> 
     </td> 
    </tr> 
</table> 

我希望這會有所幫助。

相關問題