Jump to content

htmlspecialchar issue


zed420

Recommended Posts

Hi All

I wonder if you can help me, I'm fairly new to PHP,  I'm trying create a quiz for my website I did well so far till it came to use of html tags. I've managed to display them by using 'htmlspecialchar' but now the query wont count those questions that have tags as their options, for example

Question Choose the correct HTML tag to make a text italic
Opt1 <Italic>
Opt2 <i>
Opt3 <ii>

When you click on Opt2 this should be counted as one of your right answer but it DOES NOT. but on same question if you don't use tags in options (Opt) it will work. This is how I am finding the results;

$query = "SELECT * FROM php_tb ORDER BY id";
$result = mysql_query($query)
or die ("Couldn't execute query 2."); 

if (!$_POST['submit']) 
{ 
echo "<form method=post action=$PHP_SELF>"; 
while ($row = mysql_fetch_array($result)){ 
$id= $_POST["id"];
$question= $_POST["question"];
$opt1= $_POST["opt1"];
$opt2= $_POST["opt2"];
$opt3= $_POST["opt3"];
$answer= $_POST["answer"];

} 
} 

elseif ($_POST['submit']) 
{ 
$score = 0; 
$total = mysql_num_rows($result); 
while ($row = mysql_fetch_array($result)){ 
$answer= htmlspecialchars($answer); 
$answer = $row[answer]; 
$z = "q$row[id]"; 
$z = trim($z);
if ($_POST[$z] == $answer) 
{ 
$score++; 
} 
} 

echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>"; 
if ($score == $total) {
echo "Congratulations! You got all the question right!";
echo "<p>Well done $name, with a score of $score, 
</p>"; 

 

Thanks

Zed

Link to comment
https://forums.phpfreaks.com/topic/108590-htmlspecialchar-issue/
Share on other sites

Thanks for replying Jabop I've tried like this it still wont work;

$query = "SELECT * FROM php_tb ORDER BY id";
$result = mysql_query($query)
       or die ("Couldn't execute query 2.");	

if (!$_POST['submit']) 
{ 
echo "<form method=post action=$PHP_SELF>";    

	while ($row = mysql_fetch_array($result)){  
  
		$id= $_POST["id"];
		$question= $_POST["question"];
              $question= htmlspecialchar($question);      
		$opt1= $_POST["opt1"];
              $opt1= htmlspecialchar($opt1);
		$opt2= $_POST["opt2"];
              $opt2= htmlspecialchar($opt2);
		$opt3= $_POST["opt3"];
              $opt3= htmlspecialchar($opt3);
		$answer= $_POST["answer"];
              $answer= htmlspecialchar($answer);

     } 
} 

elseif ($_POST['submit']) 
{      
$score = 0;    
$total = mysql_num_rows($result); 
  	while ($row = mysql_fetch_array($result)){   
$answer= htmlspecialchars($answer);                             
		$answer = $row[answer];            
		$z = "q$row[id]";                    
		$z = trim($z);
		if ($_POST[$z] == $answer) 
	{                
	$score++;                 
	}          
}  

      
echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>";	
if   ($score == $total) {
echo "Congratulations! You got all the question right!";
echo "<p>Well done $name, with a score of $score, 
               </p>";

 

Any more suggestions...

Thanks Wolphie

I've just relised with your code the query is counting the question with tags on but the only problem now is its not displaying them. e.g

Question blah blah blah
Opt1
Opt2
OPt3

There is nothing infront of Options(Opt). The data is deff going into databse but without < and >

 

Help needed

Zed

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.