variables and radio buttons php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

variables and radio buttons php

Post by Chrisa_dr »

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:

Code: Select all

 
<?
$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>";
 
[b]//the variables i used[/b]
$j =1;
$i=0;
while ($i < $num) {
 
[b]//the fields of my database[/b]
 
$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");
[b]//the variables that would add and print the final score[/b]
$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='pick4.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'>";
?>
 
What i dont know is how to call the variables so that i can proccess the form. Please help me...
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: variables and radio buttons php

Post by jaoudestudios »

Use $_POST to get the values sent in the form.

You are putting the results in the page? This is a bad idea as anyone could do a view source and see the results! Hidden inputs are still written into the page code, just not displayed on the screen.
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

Re: variables and radio buttons php

Post by Chrisa_dr »

OK but how can i use POST to call the variables this is my problem
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: variables and radio buttons php

Post by jaoudestudios »

Chrisa_dr wrote:OK but how can i use POST to call the variables this is my problem
Here are some good examples...http://www.w3schools.com/php/php_post.asp
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

Re: variables and radio buttons php

Post by Chrisa_dr »

Yes boy my radio buttons name is $j how can i call it in the next page. What i thought would work is the following:

Code: Select all

<?
$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;
 
//[b]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[/b] 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: variables and radio buttons php

Post by papa »

$j=$_POST['j'];
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

Re: variables and radio buttons php

Post by Chrisa_dr »

I tried that it returns nothing.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: variables and radio buttons php

Post by jaoudestudios »

Chrisa_dr wrote:Yes boy
8O

Your php is wrong! To start with look at line 38!?! BOY!
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

Re: variables and radio buttons php

Post by Chrisa_dr »

To start with i wanted to write BUT ok sorry about that. line 38 is $x=$_POST['$j']; i tried the same thing using $j=$_POST['$j']; but it returned a time overflow message.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: variables and radio buttons php

Post by papa »

You overwrite $j here:
for ($j=1;$j<$num;$j++){

And ['$j'] won't work either way.
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

Re: variables and radio buttons php

Post by Chrisa_dr »

I realised that that doesn't work but i cant think of a way to make this hole thing work. Do you have any idea of how to make this script work? Even a hole new idea would be appriciated.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: variables and radio buttons php

Post by papa »

What does $_POST['j'] contain ?
Chrisa_dr
Forum Newbie
Posts: 7
Joined: Fri Jan 30, 2009 4:48 am

Re: variables and radio buttons php

Post by Chrisa_dr »

The hole idea is to make a test out of a mysql table. I did that the test is created and now i want to grade the test. So i thought that if i could name the radio buttons and the other elements of the form with a variable ($j) because the number of questions varies. My main problem is how to get the inputs of the test as the name of my form elements is a variable.
Post Reply