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

报告相同问题?

悬赏问题

  • ¥15 wpf程序使用过程中异常奔溃
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少
  • ¥15 按颜色进行点云分割-python
  • ¥15 Matlab如何实现汽车变道切入场景的批量仿真
  • ¥15 无刷电机要通过什么才能做各种动作
  • ¥15 VS2010通过OnCtlColor函数改变radio控件字体的颜色为白色,但是程序运行后发现不起作用,怎么办?
  • ¥15 Qt中实现子线程和管理线程类之间实时通信
  • ¥15 cacls 命令如何解除锁定文件夹?
  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向