【问题标题】:a problrm when i turn a html form inputs into php array当我将 html 表单输入转换为 php 数组时出现问题
【发布时间】:2017-10-29 03:12:57
【问题描述】:

我的html文件包含以下表格

<form action="action.php" method="get">
    First name:<br>
    <input type="text" name="name[]" /><br>
    Second name:<br>
    <input type="text" name="name[]" /><br>
    Third name:<br>
    <input type="text" name="name[]" /><br>
    Forth name:<br>
    <input type="text" name="name[]" /><br>
    <input type="submit" value="submit">
</form>

我希望输出是输入的一个随机值,所以我的 action.php 看起来像

<?php

    $output = $_POST['name'];

    $key = array_rand($output);
    echo $output[$key];

?>

但这不起作用并给了我以下内容

注意:未定义索引:第 8 行 C:\xampp\htdocs\myfiles\action.php 中的名称

警告:array_rand() 期望参数 1 为数组,在第 10 行的 C:\xampp\htdocs\myfiles\action.php 中给出 null

有人可以帮忙吗?

【问题讨论】:

  • 你的方法是“GET”,为什么要使用“$_POST”?
  • form方法改为post:method="post"
  • @RyanAW 你是对的,我的错,谢谢
  • @JosanIracheta 谢谢

标签: php html arrays forms random


【解决方案1】:

使用get方法提交表单

所以您应该使用 $_GET 来检索发送的数据,如下所示:

<?php
     $output = $_GET['name'];
     $key = array_rand($output);
     echo $output[$key];
?>

【讨论】:

    猜你喜欢
    • 2015-08-29
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2012-06-22
    • 2021-06-03
    相关资源
    最近更新 更多