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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题