2014-06-06 71 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>