2017-03-05 83 views
0

下午好!所以,我在這裏遇到了我的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

形式use the GET method by default。您必須在<form>標記上明確設置method="post"以便瀏覽器將請求作爲POST提交併填充超全球$_POST<input>標籤上的method屬性沒有意義,因爲有關將數據發送到服務器的詳細信息在整個表單中應用。

+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

方法=「GET」是方法屬性的默認值,它追加表單數據在名稱/值對的網址:URL名=值&名=值

然而,

方法? =「POST」的形式發送數據作爲HTTP交易後

而且,

動作=「URL」指定在提交表單時向何處發送表單數據。該URL可以是絕對的:行動= 「http://www.example.com/example.php」或相對:行動= 「使用example.php」

相關問題