Nate Nate - 7 months ago 10
SQL Question

Trying to POST a value that is set by a variable, how do I access it with $_POST?

I have some names stored in a database and this is what I use to access them and submit them through a form.

<form action="takeQuiz.php" method="post" id="takeQuiz">
<?php $singleSQL = mysql_query("SELECT * FROM examtable");
while($row = mysql_fetch_array($singleSQL)){
$examName = $row['name'];
echo '<p><input type="submit" value="'.$examName.'"/>';
} ?>
</form>


Then on the takeQuiz.php I want to set a variable with the value of the submit

$examName = $_POST['.$examName.'];

$otherSQL = mysql_query("SELECT * FROM $examName");

Answer

It is strange to build a form with buttons only. However you must add name attribute on these buttons (it is bad practice to have non-unique names into single form):

<form action="takeQuiz.php" method="post" id="takeQuiz">
    <?php $singleSQL = mysql_query("SELECT * FROM examtable");
        while($row = mysql_fetch_array($singleSQL)){
            $examName = $row['name'];
            echo '<input type="submit" name="submit" value="'.$examName.'"/>';
        } ?>
</form>

Then in your takeQuiz.php you can get it as:

$examName = $_POST['submit'];
$otherSQL = mysql_query("SELECT * FROM $examName");

And yes, stop using mysql_* functions, but use mysqli_* or PDO.