下午好!所以,我在这里遇到了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>
答案 0 :(得分:1)
这是因为您的<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
method =&#34; GET&#34; 是方法属性的默认值,它将表单数据附加到名称/值对中的URL:URL?name = value&amp; ;名称=值
然而,
方法=&#34; POST&#34; 将表单数据作为HTTP事务处理发送
和
action =&#34; url&#34; 指定提交表单时发送表单数据的位置。网址可能是绝对的: action =&#34; http://www.example.com/example.php" 或亲戚: action =&#34; example.php& #34;
答案 1 :(得分:0)
表单use the GET
method by default。您必须在method="post"
标记上明确设置<form>
,以便浏览器将请求提交为POST
并填充$_POST
超全局。 method
标记上的<input>
属性没有意义,因为有关向服务器发送数据的详细信息是在整个范围内应用的。