Jump to content

Form functionality


kwdelre

Recommended Posts

I am a newbie trying to build a quiz system. I have all of the questions and answer choices in a mysql table. My code below selects the question and answers. They appear exactly how I want them to....but I'm stuck on the form functionality. I was trying to use something like this:

 

<input type='radio' name='Q1Choice'  value='$answer1' />

 

I need to be able to insert the choice into my table. I know how to do the insert part but not sure how to take the radio choice as the insert data.

 

<?

//Connect to database
mysql_connect($host, $dbusername, $password);
mysql_select_db($TxQuizdb);


//Retrieve Quiz Questions
$qquery = mysql_query("SELECT * FROM Module_1_Quiz");



while ($m1questions = mysql_fetch_array($qquery)) {
  
   echo "<table width=600 border=1><tr><td>";
   echo $m1questions['Question'];
   echo "</td></tr><br><tr><td>";
   echo $m1questions['Choice1'];
   echo "<br>";
   echo $m1questions['Choice2'];
   echo "<br>";
   echo $m1questions['Choice3'];
   echo "</td></tr></table>";
   
									}

 

thanks for any help.

Link to comment
Share on other sites

Yeah I guess that's all i need, cause in the process file I will have their selection inserted into the table. I had it working before with the code below but that was when i had the quiz info in a php doc.

 

echo '<ol type="a"><br><br>';

shuffle($q1ans);
foreach($q1ans as $answer1) {

echo "<li><input type='radio' name='Q1Choice'  value='$answer1' />$answer1</li><br/>";
}
echo '</ol>';
?>
<br />
<br />

<ol type="a">
<?

//Begin Question #2




shuffle($q2ans);
foreach($q2ans as $answer2) {
echo "<li><input type='radio' name='Q2Choice'  value='$answer2'/>$answer2</li><br/>";
}
?>

Link to comment
Share on other sites

Yeah it did work fine but all of those variables used in it were from an array in a php file. Now I have moved all of the quiz contents to a database. When I run the following code it does not input anything into the fields for their answer choice.

 

//Connect to database
mysql_connect($host, $dbusername, $password);
mysql_select_db($TxQuizdb);


//Retrieve Quiz Questions
$qquery = mysql_query("SELECT * FROM Module_1_Quiz");



while ($m1questions = mysql_fetch_array($qquery)) {
  
   echo "<table width=600 border=1><tr><td>";
   echo $m1questions['Question'];
   echo "</td></tr><br><tr><td>";
   echo "<input type='radio' name='Q1Choice'  value='$answer1' />"
   echo $m1questions['Choice1'];
   echo "<br>";
   echo "<input type='radio' name='Q1Choice'  value='$answer1' />"
   echo $m1questions['Choice2'];
   echo "<br>";
   echo "<input type='radio' name='Q1Choice'  value='$answer1' />"
   echo $m1questions['Choice3'];
   echo "</td></tr></table>";
   
									}

Link to comment
Share on other sites

I think something like this will work:

//Connect to database
mysql_connect($host, $dbusername, $password);
mysql_select_db($TxQuizdb);


//Retrieve Quiz Questions
$qquery = mysql_query("SELECT * FROM Module_1_Quiz");


$quiz = "<table width=600 border=1>";
while ($row = mysqli_fetch_array($qquery)) { 
    $question = $row["question"];
$answer1 = $row["answer_1"];
$answer2 = $row["answer_2"];
$answer3 = $row["answer_3"];

$quiz .= "<tr><td> $question";
$quiz .= "</td></tr><br><tr><td>";
$quiz .= "<input type='radio' name='Q1Choice'  value='$answer1' />";
$quiz .= "$answer1";
$quiz .= "<input type='radio' name='Q1Choice'  value='$answer2' />";
$quiz .= "$answer2";
$quiz .= "<input type='radio' name='Q1Choice'  value='$answer3' />";
$quiz .= "$answer3";
$quiz .= "</td></tr>";
} 
$quiz .= "</table>"

echo $quiz;

 

The value in the square brackets next to $row is the name of each column with the question/possible answers in.

' .= ' means append data to the string, so it just adds it to the end.

I've got the <table> starting tag outside of the loop, as this will put all of the information into the one table, instead of making multiple tables.

 

I haven't tested the code above, so there is most definitely errors, just play around with it until you can get it to work, but that is the basic idea that you want.

 

Denno

Link to comment
Share on other sites

I feel dumb... why can't I get the following to work?

 


$quiz .= "<input type='radio' name='Q$iChoice'  value='$answer1' />";

 

I am just using $i = 1 at the beginning of the loop and then $i=$i++ at the end. This way Q1Choice, Q2Choice, etc can be identified later.

 

Thanks!

Link to comment
Share on other sites

$quiz .= "<input type='radio' name='Q" . "$i" . "Choice'  value='$answer1' />";

 

You can try that, but the use of double quotes means that you can place variables into strings without having to escape and all that..

 

In what way doesn't it work? Does it give an error or anything?

 

Denno

Link to comment
Share on other sites

Well before trying to use $i it was only letting me select 1 radio for both questions being tested. So I figured it was the radio input name. Plus I would need the names to change anyway. When I put in the code with $i it still would only let me select one radio for both questions. It still does it with your adjustments.

 

Here's where I'm at:

 

mysql_connect($host, $dbusername, $password);
mysql_select_db($TxQuizdb);


//Retrieve Quiz Questions
$qquery = mysql_query("SELECT * FROM Module_1_Quiz");


$quiz = "<table width=600 border=1>";
while ($m1questions = mysql_fetch_array($qquery)) { 
    

$question = $m1questions["Question"];
$answer1 = $m1questions["Choice1"];
$answer2 = $m1questions["Choice2"];
$answer3 = $m1questions["Choice3"];

$i = 1;


$quiz .= "<tr><td> $question";
$quiz .= "</td></tr><br><tr><td>";
$quiz .= "<input type='radio' name='Q" . "$i" . "Choice'  value='$answer1' />";
$quiz .= "$answer1";
$quiz .= "<input type='radio' name='Q" . "$i" . "Choice'  value='$answer2' />";
$quiz .= "$answer2";
$quiz .= "<input type='radio' name='Q" . "$i" . "Choice'  value='$answer3' />";
$quiz .= "$answer3";
$quiz .= "</td></tr>";

$i = $i++;
}

$quiz .= "</table>";



echo $quiz;

Link to comment
Share on other sites

Alright I've got it to work. Here is the code that I'm using:

<?php

//connect to database
require_once "connect_to_mysql.php";

$sqlCommand = "SELECT * FROM quiz"; 
$query = mysql_query($sqlCommand,$myConnection) or die (mysql_error()); 

$quiz = "<table width=600 border=1>";
$i = 0;
while ($row = mysql_fetch_array($query)) { 
$i++;
    $question = $row["question"];
$answer1 = $row["answer1"];
$answer2 = $row["answer2"];
$answer3 = $row["answer3"];

$quiz .= "<tr><td> $question";
$quiz .= "</td></tr><br><tr><td>";
$quiz .= "<input type='radio' name='Q $i Choice'  value='$answer1' />";
$quiz .= "$answer1";
$quiz .= "<input type='radio' name='Q $i Choice'  value='$answer2' />";
$quiz .= "$answer2";
$quiz .= "<input type='radio' name='Q $i Choice'  value='$answer3' />";
$quiz .= "$answer3";
$quiz .= "</td></tr>";
} 
$quiz .= "</table>";
mysql_free_result($query);

?>

<!doctype html>
<html>
<head>
<title>Test Page</title>

<style type="text/css">

</style>

</head>
<body>

<?php

echo $quiz;

?>

</body>
</html>

 

Give that a whirl and see what you come up with :).

 

Denno

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.