为什么我的$ _POST返回一个空数组?
问题描述:
下午好!所以,我在这里遇到了我的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>
答
形式use the GET
method by default。您必须在<form>
标记上明确设置method="post"
以便浏览器将请求作为POST
提交并填充超全球$_POST
。 <input>
标签上的method
属性没有意义,因为有关将数据发送到服务器的详细信息在整个表单中应用。
答
这将是因为您的<form>
标记在其中没有任何属性。它应该已经:
<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
方法=“GET”是方法属性的默认值,它追加表单数据在名称/值对的网址:URL名=值&名=值
然而,
方法? =“POST”的形式发送数据作为HTTP交易后
而且,
动作=“URL”指定在提交表单时向何处发送表单数据。该URL可以是绝对的:行动= “http://www.example.com/example.php”或相对:行动= “使用example.php”
我的天哪,你说得对!我知道。一些非常简单的... 即使经过数小时的故障排除,您也可以忽略其中一件事。 –