[英]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.