0

Good afternoon! So, I'm running into a few problems with my PHP code, here. I'm not exactly certain why, but upon pressing the submit button, no information is sent to $_POST. Any reasoning as to why this is?

The data sent into the html document is written into a php multidimensional associative array.

My code is posted below.

<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>

2 Answers2

1

That would be because your <form> tag doesn't have any attributes in it. It should've been:

 <form action="php_script_to_process_the_form.php" method="POST">

     ... Form elements ...

     <input class="btn btn-info" type="submit" value="submit">

 </form>

Refer tag on W3Schools for a list of all available attributes it could take: https://www.w3schools.com/tags/tag_form.asp

method="GET" is the default value for the method attribute, and it Appends the form-data to the URL in name/value pairs: URL?name=value&name=value

Whereas,

method="POST" Sends the form-data as an HTTP post transaction

And,

action="url" Specifies where to send the form-data when a form is submitted. The url may be absolute: action="http://www.example.com/example.php" or relative: action="example.php"

Ashil John
  • 7,362
  • 4
  • 19
  • 34
0

Forms use the GET method by default. You must explicitly set method="post" on your <form> tag for browsers to submit the request as POST and populate the $_POST superglobal. The method attribute on <input> tags is meaningless, as details about sending data to the server are applied form-wide.

Bytewave
  • 597
  • 1
  • 8
  • 20
  • Oh my goodness, you're right! I knew that. Something so simple... One of those things you just overlook, even after hours of troubleshooting. – Michael Harty Mar 05 '17 at 02:51