doushi5752 2015-03-01 21:22
浏览 51
已采纳

如何从一个变量中的多个表单搜索字段中捕获数据

I have an html search form with multiple fields like select, textarea and text inputs. I need the values from all the fields to be stored into $input in the php search form but I'm not sure how to store them into one variable called $input on the php page.

<form name="search" method="post" action="http://example.com/search/">
Seach for: <input type="text" name="find" /> <input type="submit"  
name="search" value="Search" />
Search for movies by Type
<select name="find">
<option value="Sci-Fi" selected>Sci-Fi</option>
<option value="Comedy">Comedy</option>
<option value="Drama">Drama</option>
</select>
</form>

//on search.php need to 'find' to be data from several form inputs
$input = $_POST['find'];
  • 写回答

3条回答 默认 最新

  • douzi1350 2015-03-01 21:33
    关注

    If you give multiple inputs the same name, $_POST['find'] will just contain the input from one of them (the last one, I think). You need to either give them different names, or you can end the name with []. When you do the latter, PHP will put all the inputs into an array.

    <form name="search" method="post" action="http://example.com/search/">
    Seach for: <input type="text" name="find[]" /> <input type="submit" name="search" value="Search" />
    Search for movies by Type
    <select name="find[]">
        <option value="Sci-Fi" selected>Sci-Fi</option>
        <option value="Comedy">Comedy</option>
        <option value="Drama">Drama</option>
    </select>
    </form>
    

    With this, you can do:

    $input = implode(' ', array_filter($_POST['find']));
    

    array_filter will remove any empty values, then the rest will be concatenated together with space between them.

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

报告相同问题?

悬赏问题

  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题