简体   繁体   English

PHP:如何与两个单独的表单输入共享一个下拉菜单(一个文本区域,一个文件上传)

[英]PHP: how can I share one drop-down menu with two separate form inputs (one textarea, one file upload)

I currently have two html forms. 我目前有两种HTML表单。 They both POST to process.php: one posts text entered into a textarea, the other posts an uploaded file. 他们都发布到process.php:一个发布文本到textarea,另一个发布上传的文件。 The value of a hidden input called 'act' is either 'paste' or 'upload' respectively, and this is used by process.php to distinguish the two forms. 隐藏的称为“ act”的输入的值分别为“ paste”或“ upload”,process.php使用此值来区分这两种形式。 The current code (below) works fine. 当前代码(如下)工作正常。

<!--Box to paste list of queries-->
<form enctype="multipart/form-data" action="process.php" method="POST" onSubmit="return showPleaseWait()">
    <?php //This hidden input value 'paste' is used by process.php to distinguish the two forms ?>
    <input type="hidden" name="act" value="paste"/>
    <b><font color="#1F88A7">Paste in a list (one query per line):<br/></b><br/>
    <textarea id="text" cols="15" rows="6" name="ID_list" style='background-color:#ffffff; border:solid 1px #1F88A7'></textarea>
    <input id="butt" name="sub" type="submit" value="Submit" /><br />
</form>

<br />

<!--Box to upload a file -->
<form enctype="multipart/form-data" action="process.php" method="POST" onSubmit="return showPleaseWait()">
    <?php //This hidden input value 'upload' is used by process.php to distinguish the two forms ?>
    <input type="hidden" name="act" value="upload"/>
    <b><font color="#1F88A7">Or upload (plain text, one query per line):</font> </b><br />
    <input id="butt" name="uploadedfile" type="file" style='background-color:#ffffff; border:solid 1px #1F88A7'/><input type="submit" value="Upload File" />
</form>

In process.php I have this: 在process.php我有这个:

if (isset($_POST['act'])){    
    if ($_POST['act'] == 'upload'){
        // ...process one way
        echo "file uploaded";
    }else if($_POST['act'] == 'paste'){
        // ...process another way
        echo "text uploaded";
    }
}

What I would like to do now is to add a single dropdown menu that will post with whichever of these two forms are submitted. 我现在想做的是添加一个下拉菜单,该菜单将以提交这两种形式中的任何一种形式发布。 I can paste the code below into both of the two forms, but then I have this dropdown menu displayed twice (once for each form). 我可以将下面的代码粘贴到这两种形式中,但是此下拉菜单显示了两次(每种形式一次)。 I would like to display this dropdown menu only once at the top of the page, and its value to be posted with whichever of the two forms ('paste' or 'upload') is submitted. 我只想在页面顶部显示一次此下拉菜单,并使用两种形式(“粘贴”或“上传”)中的任何一种来提交其值。

<form action="process.php" method="post">
    <b><font color="#1F88A7">Select your query type:</font> </b><br />
    <select name="ID_type">
    <option value=""></option>
    <option value="type1">type1</option>
    <option value="type2">type2</option>
    <option value="type3">type3</option>
    </option>
    </select>
</form>

The goal is to process different types of query differently by including if statements into process.php, such as: 目标是通过将if语句包含到process.php中来不同地处理不同类型的查询,例如:

if ($_POST['ID_type'] == 'type1'){}

The problem seems simple, and maybe I can't solve it because of my lack of familiarity with html (apologies if there is anything horrible in the html above), but I have not been able to find a solution. 这个问题似乎很简单,也许由于我对html缺乏了解(如果上面的html中有什么可怕的内容而道歉),我可能无法解决,但是我找不到解决方法。 Many many thanks for any help. 非常感谢您的帮助。

If you don't want to get into any javascript workarounds with your existing two form structure, then you should have everything surrounded by just one form. 如果您不希望使用现有的两种表单结构来解决任何javascript变通方法,那么您应该将所有内容都仅用一种表单包围。 Maybe both "act" and "query type" should be dropdowns at the top of the form. 也许“ act”和“ query type”都应该是表格顶部的下拉菜单。 Then, depending on if act is set to paste or upload, you look at either the textarea or file input - as they will both be included if it is just one form. 然后,根据是将行为设置为粘贴还是上载,您将查看文本区域或文件输入-因为如果只是一种形式,则它们都将包括在内。

There are ways to improve UX with this idea through javascript, for example to validate if the textarea contains data if act=paste or if there is a file in the file upload if act=upload before allowing the post, but you could also do this all server side if you don't want to get into JS. 有多种方法可以通过javascript改善这种想法,例如,在允许发布之前,如果act = paste验证文本区域是否包含数据,或者如果act = upload验证文件上传中是否有文件,但是您也可以这样做所有服务器端,如果您不想使用JS。

You are making it more complex than it should be, unless you really have a reason for this I would just them in the same form if I were you. 您正在使它变得比原本应该的复杂,除非您确实有理由这样做,否则如果我是您,我将以相同的形式使用它们。 Then put logic in the PHP since they are both call the same process.php script. 然后将逻辑放入PHP,因为它们都调用相同的process.php脚本。

<form enctype="multipart/form-data"
   action="process.php" method="POST" onSubmit="return showPleaseWait()">  
<b><font color="#1F88A7">Select your query type:</font> </b><br />
<select name="ID_type">
<option value=""></option>
<option value="type1">type1</option>
<option value="type2">type2</option>
<option value="type3">type3</option>
</option>
</select>
<!-- Paste -->
<b><font color="#1F88A7">Paste in a list (one query per line):</b><br/>
<textarea id="text" cols="15" rows="6" name="ID_list" 
      style='background-color:#ffffff; border:solid 1px #1F88A7'></textarea>
<!-- upload -->
<b><font color="#1F88A7">Or upload (plain text, one query per line):</font></b>

<input name="uploadedfile" type="file" 
       style='background-color:#ffffff; border:solid 1px #1F88A7'/>   
<input id="butt" name="sub" type="submit" value="Submit" /><br />
</form>

I wouldnt even use hiddenfield and stuff: 我什至不使用hiddenfield和东西:

if (isset($_POST)){    
    if (is_uploaded_file($_FILES['yourfile']['uploadedfile'])){
        // ...process one way
        echo "file uploaded";
    }else if(isset($_POST['ID_list'])){
        // ...process another way
        echo "text uploaded";
    }

    if ($_POST['ID_type'] == 'type1'){
        //do whatever
    }
}

Now if you don't want the user to process both , then I will focus on redesigning my Interface to just allow one type of process( maybe using a radio button or something like that) 现在,如果您不希望用户同时处理两者,那么我将集中精力重新设计我的界面,使其仅允许一种类型的过程(也许使用单选按钮或类似的方法)

暂无
暂无

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

相关问题 如何从一个下拉菜单的两个表中获取数据 - How to get data from two tables for one drop-down menu PHP 使用一种形式将两种不同的文件类型与两种不同的输入上传到两个不同的目录中 - PHP Upload Two Different File Types Using One Form With Two Different Inputs Into Two Different Directories 我如何链接两个下拉菜单框,我可以选择一个菜单,它应该在其他下拉框中显示我的列表? - how can i link two drop down menu box where i can select one menu and it should show my list in other drop down box? 在PHP中将下拉选择列表的内容从一种形式传递到另一种形式 - Pass contents of drop-down select list from one form to another in PHP 创建一个下拉列表链,该列表基于前一个输入,形成php中的数据库 - Create a Chain of drop-down list that base on the input of the previous one form the database in php PHP-如何以3种输入形式上传3张图片,以一种形式生成PHP - PHP - how to upload 3 images with 3 inputs in one form php 如果下拉菜单中的子页面已打开,我如何提供菜单项 id=&quot;active&quot;? PHP - How can I give menu item id="active", if its subpage in a drop-down menu is open? PHP 如何通过表单处理(PHP)访问下拉菜单中的值? - How can the value from a drop-down menu be accessed on form processing (PHP)? 如何处理下拉菜单( <select> )菜单中的.php文件? - How to process a drop-down (<select>) menu in a .php file? 如果选项之一为0,如何在选择下拉列表中检查变量 - how to check for a variable in select drop-down if one of the option is 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM