duanchi8836 2014-02-06 17:17
浏览 69
已采纳

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

I currently have two html forms. They both POST to process.php: one posts text entered into a textarea, the other posts an uploaded file. 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. 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:

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 ($_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. Many many thanks for any help.

  • 写回答

2条回答 默认 最新

  • douludi8413 2014-02-06 17:28
    关注

    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.

    <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:

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?