简体   繁体   English

在较大的表单中上传文件(一页上有两个表单),我如何一次提交两组数据(文件和表单)? (PHP)

[英]Uploading files within a larger form (two forms on one page), how can I submit both sets of data (file and form) at once? (PHP)

I have a form such that a list of questions with inputs is followed by an invitation to upload files if they have them.我有一个表格,其中包含输入的问题列表,然后是邀请上传文件(如果有的话)。

I have avoided nesting forms by popping up the form containing the upload.我通过弹出包含上传的表单来避免嵌套表单。 The difficulty is that if they press submit on the Uploader form, it will process the upload on the destination page, and likewise if they hit submit on the rest of the form, the uploads are not submitted.困难在于,如果他们在 Uploader 表单上按下提交,它将处理目标页面上的上传,同样,如果他们在表单的其余部分点击提交,则不会提交上传。

How can I do both at once?我怎样才能同时做到这两点?

My code:我的代码:

<form id="questions" action="page2.php" method="POST">
    Question 1 <input name="q1"/>
    Question 2 <input name="q2" />  
    Have any files to add? <button id="upload-button">Upload</button>
    <input type="submit" value="Submit Answers" />
</form>


<div class="hidden">
    <form id="fileupload" enctype="multipart/form-data" action="uploader.php" method="POST">
        <input id="fileupload" type="file" name="files[]"  multiple>
        <input  type="submit" value="Upload Files" />
    </form>
</div>

jQuery will pop up the hidden div if someone presses the button to add files.如果有人按下按钮添加文件,jQuery 将弹出隐藏的 div。

I don't have a problem with them being posted to the same place, but I tried using javascript to use the click event for each button to submit both forms and it did not work.我没有将它们发布到同一个地方的问题,但是我尝试使用 javascript 来使用每个按钮的 click 事件来提交两个表单,但它不起作用。 (Reading stack overflow later explained that only the last submission gets processed in this way) (读栈溢出后来解释说只有最后一次提交才会这样处理)

Any ideas?有任何想法吗? Thanks谢谢

Very simply, structure your HTML like this, and process the logic from both page2.php and uploader.php in one shot (ie include the file-upload logic in page2.php).很简单,像这样构建您的 HTML,并一次性处理 page2.php 和 uploader.php 中的逻辑(即在 page2.php 中包含文件上传逻辑)。 If you show us the contents of both of those files, I can show you how to combine them.如果您向我们展示这两个文件的内容,我可以向您展示如何组合它们。

Also added labels with links to the associated inputs, and swapped the submit input for a button element.还添加了带有关联输入链接的标签,并将提交输入交换为按钮元素。

<form id="questions" enctype="multipart/form-data" action="page2.php" method="POST">
    <label for="q1">Question 1</label>
    <input id="q1" name="q1" type="text" />

    <label for="q2">Question 2</label> 
    <input id="q2" name="q2" type="text" />  

    <label for="fileupload">Upload a File<label>
    <input id="fileupload" type="file" name="files[]"  multiple />

    <button type="submit">Submit</button>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个 HTML 表单中的两个提交按钮,用于上传或删除带有 PHP 的文件 - Two submit buttons in one HTML form, for uploading or deleting a file with PHP 如何将一个HTML表单提交到多个php文件? - How can I submit one HTML form to multiple php files? 我如何在一个页面中使用2个表单,以及在单击第一个提交按钮时如何以第二个表单显示某些内容 - How i can use 2 forms in one page and when click in first submit button show something in second form 我可以在 Cordova 中提交 PHP 表单吗? - Can I submit a PHP form within Cordova? Dropzone:一次提交表单数据和dropzone - Dropzone: Submit both form data and dropzone at once 当您完成一个表单时,创建了两个不同的PHP Laravel联系表单,它们都同时提交和发送 - Created two different PHP Laravel contact forms both submit and send at the same time when you complete one form 我可以使用Dropzone提交表格而无需上传任何文件吗? - Can I submit a form with Dropzone without uploading any files? 如何一步提交一个PHP表单帖子并重定向到确认页面? - How can I submit a PHP form post and redirect to a confirmation page in one step? PHP:我可以使用AJAX在表单内提交表单吗? - PHP: Can I submit a form within a form using AJAX? 一页php中有两个表单,但只想提交一个 - Two form in one page php, but want to submit only one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM