Chrisa_dr Posted February 2, 2009 Share Posted February 2, 2009 Hello, Im trying to make a test page using php and mysql. The test is automaticaly made from a mysql table. What i want to do is name each form part with a variable and then get the name of the variable and pass it on to another page (or the same that doesn't matter) so that i can grade the test submitted. The code i used is: <? $username="root"; $password="****"; $database="****"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); //the name of the table from the previus page $table=$_POST['table']; $query="SELECT * FROM $table"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); echo "<b><center>$table</center></b><br><br>"; //the variables i used $j =1; $i=0; while ($i < $num) { //the fields of my database $id=mysql_result($result,$i,"id"); $type=mysql_result($result,$i,"type"); $question=mysql_result($result,$i,"question"); $answ1=mysql_result($result,$i,"answ1"); $answ2=mysql_result($result,$i,"answ2"); $answ3=mysql_result($result,$i,"answ3"); $answ4=mysql_result($result,$i,"answ4"); $sosto=mysql_result($result,$i,"sosto"); //the variables that would add and print the final score $sum=0; $count=0; // hidden variables echo"<input type='hidden' name='id' value='$id' />"; echo"<input type='hidden' name='question' value='$question' />"; echo"<input type='hidden' name='answ1' value='$answ1' />"; echo"<input type='hidden' name='answ2' value='$answ2' />"; echo"<input type='hidden' name='answ3' value='$answ3' />"; echo"<input type='hidden' name='answ4' value='$answ4' />"; echo"<input type='hidden' name='sosto' value='$sosto' />"; echo"<input type='hidden' name='table' value='$table' />"; echo "<form action='pick5.php' method='post' >"; if($type=="radio"){ echo "$j. $question<br>"; echo "<input type='radio' name='$j' value='$j'>right $j<br>"; echo "<input type='radio' name='$j' value='$j'>wrong $j<br><hr>"; echo"<input type='hidden' name='$j' value='$j' />"; $j++; } else if($type=="fill"){ echo "$j. $question<br>"; echo "<input type='text' name='$j' size='2'> $answ1<br>"; echo "<input type='text' name='$j' size='2'> $answ2<br>"; echo "<input type='text' name='$j' size='2'> $answ3<br>"; echo "<input type='text' name='$j' size='2'> $answ4<hr><br>"; echo"<input type='hidden' name='q$j' value='$j' />"; $j++; } else if($type=="select"){ echo "$j. $question<br>"; echo "<select name='$j'>"; echo "<option value='$j'> $answ1</option>"; echo "<option value='$j'> $answ2</option>"; echo "<option value='$j'> $answ3</option>"; echo "<option value='$j'> $answ4</option>"; echo "</select><hr><br>"; echo"<input type='hidden' name='q$j' value='$j' />"; $j++; } $i++; } echo "<input type='submit' name='Submit' value='Submit'>"; ?> //pick5.php <? $username="root"; $password="****"; $database="****"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $table=$_POST['table']; $query="SELECT * FROM $table"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); $id=mysql_result($result,$i,"id"); $type=mysql_result($result,$i,"type"); $question=mysql_result($result,$i,"question"); $answ1=mysql_result($result,$i,"answ1"); $answ2=mysql_result($result,$i,"answ2"); $answ3=mysql_result($result,$i,"answ3"); $answ4=mysql_result($result,$i,"answ4"); $sosto=mysql_result($result,$i,"sosto"); $j=$_POST['$j']; $sum=0; $count=0; echo "<b><center>$table</center></b><br><br>"; echo"<input type='hidden' name='$j' value='$j' />"; //$j=1; //as the name of my form elements is $j i used POST to get these variables and assigned them to a new variable called $x as shown below for ($j=1;$j<$num;$j++){ $x=$_POST['$j']; //$sosto is the right answ from my datadase and $sum is a variable that i want to use so that i can grade the test. echo "sosto is $sosto<br>"; echo"$j<br>"; echo "$x<hr><br>"; if($x==$sosto){ $sum=$sum+100; $count=$count+1; echo "sum is $sum<br>"; } else{ $sum=$sum+0; $count=$count+1; echo "sum is $sum<br>";} } ?> But this returns only the $j from the previus page . The if($x==$sosto) doesn't seem to work Link to comment https://forums.phpfreaks.com/topic/143497-variables-and-radio-buttons-php/ Share on other sites More sharing options...
ialsoagree Posted February 2, 2009 Share Posted February 2, 2009 Not 100% sure this is the problem but have you defined an HTML form tag with action and method? Link to comment https://forums.phpfreaks.com/topic/143497-variables-and-radio-buttons-php/#findComment-752866 Share on other sites More sharing options...
Chrisa_dr Posted February 3, 2009 Author Share Posted February 3, 2009 How sould i define it? But i'm pretty sure that this is not the problem. Can you help me with the variables? Link to comment https://forums.phpfreaks.com/topic/143497-variables-and-radio-buttons-php/#findComment-753349 Share on other sites More sharing options...
Snart Posted February 3, 2009 Share Posted February 3, 2009 Try with $x=$_POST[$j]; instead of $x=$_POST['$j']; Link to comment https://forums.phpfreaks.com/topic/143497-variables-and-radio-buttons-php/#findComment-753353 Share on other sites More sharing options...
Chrisa_dr Posted February 3, 2009 Author Share Posted February 3, 2009 I tried that but the problem is that $x=$_POST[$j] returns the number of $j. I also tried doing <input type='radio' name='$j' value='right'> but whene i call $x it doesnt return right but 1,2,... Link to comment https://forums.phpfreaks.com/topic/143497-variables-and-radio-buttons-php/#findComment-753358 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.