2014-06-06 72 views
0

我在一個頁面上有兩個表單,每個表單都有一個提交按鈕。多個表單和提交按鈕,但只有一個被提交?

我需要每個按鈕只提交其分配給的表單。

有誰知道我該怎麼做?

下面的代碼:

<body> 

    <?php 

     include ("test.txt") 

    ?> 

    <br> <br> 
    <form name = "contactForm" action = "php/text_write.php" method = "post"> 

     <textarea rows = "10" cols = "50" name = "text_input">Replace this text with what you want to print on screen</textarea> <br> <br> 
     <input type= "submit" name = "write_text" value = "Edit Text!"> 

    </form> 


</body> 

<body> 

    <form action="php/upload/upload_file.php" method="post" enctype="multipart/form-data"> 


    <label for="file">Filename:</label> 


    <input type="file" name="file" id="file"><br><br> 


    <input type="submit" name="upload" value="Submit"> 

</body> 

+0

我很確定這種形式默認情況下。或許第二種形式的名字缺乏對你的欺騙?在Jitendra的評論之後,爲什麼你的HTML中有兩個body標籤? – Fluffeh

+0

您的第二個表單不是很接近''標籤。 :) –

+0

缺少關閉窗體標籤''爲第二個窗體 –

回答

1

更改第二個形式這

<body> 

<form action="php/upload/upload_file.php" method="post" enctype="multipart/form-data"> 


<label for="file">Filename:</label> 


<input type="file" name="file" id="file"><br><br> 


<input type="submit" name="upload" value="Submit"> 

</form> 

</body> 
0

您將無法在純HTML中實現您想要的功能。原因是當用戶點擊提交按鈕時瀏覽器必須準備HTTP POST請求。此請求的內容由按下提交按鈕的表單字段定義。然後瀏覽器需要接受HTTP響應,破壞進程中另一個表單的內容。

但是,您可以使用JavaScript來做你所需要的。您可以使用表單提交方法,但不允許您的瀏覽器遠離您的頁面。這種方式從特定形式的HTTP POST將到達網絡服務器。但是,網絡服務器響應將被忽略。技術上會做你想做的,但絕對是糟糕的用戶體驗。因此,我不打算深入研究如何實現它的技術細節,並且您應該對所有表單中的所有元素使用帶有單個提交按鈕的單一表單。

另一種解決方法,並有多種形式(我個人不喜歡這樣的解決方案)的方式是使用框架,其中每個框架有自己的形式和提交按鈕。框架將爲表單提供獨立性。但請注意,這些日子框架並不受歡迎。

0

你不能有一個提交按鈕來控制html中的兩個表單,它是每個表單的提交按鈕。

因此,您的表單的代碼應該更改爲;

第一類:

<?php 

    include ("test.txt") 

?> 

<br> <br> 
<form name = "contactForm" action = "php/text_write.php" method = "post"> 

    <textarea rows = "10" cols = "50" name = "text_input">Replace this text with what you want to print on screen</textarea> <br> <br> 
    <input type= "submit" name = "write_text" value = "Edit Text!"> 

</form> 

你的第二個表格

<form action="php/upload/upload_file.php" method="post" enctype="multipart/form-data"> 


    <label for="file">Filename:</label> 


    <input type="file" name="file" id="file"><br><br> 


    <input type="submit" name="upload" value="Submit"> 

</form>