【问题标题】:php- how to insert form input in an array? [duplicate]php-如何在数组中插入表单输入? [复制]
【发布时间】:2014-11-19 19:26:34
【问题描述】:

我有一个要求以逗号分隔的电话号码的表单,例如,如果用户在“电话”字段中输入以下内容:

9999999999,8888800000,7777788888

然后想将它们存储为一个数组,就像:

$contacts = array ("9999999999","8888800000","7777788888");

我该怎么做?

我试过了:

$contacts = array();
if (is_array(@$_POST['phone']))
{
    foreach($_POST['phone'] as $one)
    {
        $contacts[] = basename($one);
    }
}

【问题讨论】:

标签: php arrays


【解决方案1】:
$myArray = explode(',', '9999999999,8888800000,7777788888');

【讨论】:

    【解决方案2】:

    您必须使用名为 $_POST 的全局变量来获取表单输入,$_POST 侦听 html 元素的名称。 假设我们得到了

    <form method="POST">
      <input type="text" name="phoneNumber1" />
      <input type="text" name="phoneNumber2" />
      <input type="text" name="phoneNumber3" />
    </form>
    

    然后我们就可以像这样获取其中的数据了。

    $contacts = array($_POST['phoneNumber1'], $_POST['phoneNumber2'], $_POST['phoneNumber3']);
    

    【讨论】:

      猜你喜欢
      • 2016-02-04
      • 2016-08-17
      • 1970-01-01
      • 2021-10-05
      • 2014-04-29
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      相关资源
      最近更新 更多