【问题标题】:Why is my $_POST coming back with an empty array?为什么我的 $_POST 返回一个空数组?
【发布时间】:2017-07-25 00:49:33
【问题描述】:

下午好!所以,我的 PHP 代码在这里遇到了一些问题。 我不确定为什么,但是在按下提交按钮后,没有信息发送到 $_POST。有什么理由解释这是为什么?

发送到html文档中的数据写入php多维关联数组。

我的代码贴在下面。

<html>
<?php
$pageId = "Quiz";
$questions = array(
    array('question' => 'How do you install Apache2 on Debian?',
        'answer' => 'sudo apt-get install apache2',
        'choices' => array('1' => 'apt-get update', '2' => 'sudo apt-get install apache2', '3' => "sudo apt-get install apache", '4' => 'apt-get install apache2',),
    ),
    array('question' => 'What command enables ufw?',
        'answer' => 'sudo ufw enable',
        'choices' => array('1' => 'sudo ufw allow', '2' => 'sudo ufw enable 80', '3' => 'ufw allow', '4' => 'sudo ufw enable',),
    ),
    array('question' => 'What ports do you keep open to ensure your web content can be driven?',
        'answer' => '80 and 443',
        'choices' => array('1' => '80 and 443', '2' => '88 and 441','3' => "80 and 4443", '4' => '90 and 433',),
    ),
    array('question' => 'What OS was this tutorial tailored for?',
        'answer' => 'Debian',
        'choices' => array('1' => 'Debian', '2' => 'Ubuntu','3' => 'CentOS', '4' => 'FreeBSD',),
    ),
    array('question' => 'What are some of the benefits to setting up your own web server?',
        'answer' => 'choice 1 data',
        'choices' => array('1' => 'choice 1 data', '2' => 'choice 2 data',),
    ),
);
include 'includes/header.html.php';
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>

<div class="container" id="theBestStuff">
    <main>
        <form>
            <ol>
<?php foreach ($questions as $q => $question) : ?>
                <li><?= $question['question']?></li>
<?php foreach ($question['choices'] as $c => $choice) : ?>
                <label><input type="radio" name="question<?= $q ?>" value="<?= $choice ?>"><?= $choice ?></label>
<?php endforeach; ?>
<?php endforeach; ?>
            </ol>
            <input class="btn btn-info" action="" method="post" type="submit" value="submit">
        </form>
    </main>
</div>

<?php
include 'includes/footer.html.php';
?>
</html>

【问题讨论】:

    标签: php html arrays post multidimensional-array


    【解决方案1】:

    那是因为您的&lt;form&gt; 标签中没有任何属性。应该是:

     <form action="php_script_to_process_the_form.php" method="POST">
    
         ... Form elements ...
    
         <input class="btn btn-info" type="submit" value="submit">
    
     </form>
    

    请参阅 W3Schools 上的标签以获取所有可用属性的列表:https://www.w3schools.com/tags/tag_form.asp

    method="GET" 是方法属性的默认值,它将表单数据以名称/值对的形式附加到 URL:URL?name=value&name=value

    然而,

    method="POST" 将表单数据作为 HTTP 发布事务发送

    还有,

    action="url" 指定提交表单时将表单数据发送到何处。 url 可以是绝对的:action="http://www.example.com/example.php" 或相对的:action="example.php"

    【讨论】:

      【解决方案2】:

      表格use the GET method by default。您必须在 &lt;form&gt; 标记上显式设置 method="post",以便浏览器以 POST 提交请求并填充 $_POST 超全局。 &lt;input&gt; 标签上的 method 属性毫无意义,因为有关向服务器发送数据的详细信息在表单范围内应用。

      【讨论】:

      • 天哪,你是对的!我知道。如此简单的事情……即使经过数小时的故障排除,您也会忽略其中的一件事。
      猜你喜欢
      • 2022-01-09
      • 2019-12-04
      • 2015-05-25
      • 2012-07-31
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 2020-08-30
      • 2015-11-24
      相关资源
      最近更新 更多