Click here to Skip to main content
15,900,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have question's array`
$questions = [
	["id" => 1,"Question" => "What is the capital city of Armenia?", "Answer" => ["Yerevan", "Moscow", "New York"]],
	["id" => 2,"Question" => "Who was the best footballer in 2017?", "Answer" => ["Messi", "Ronaldo", "Neymar"]],
	["id" => 3,"Question" => "4 * 5 = ?", "Answer" => ["9", "45", "20"]],
	["id" => 4,"Question" => "When has created PHP?", "Answer" => ["1998", "1994", "2000"]],
	["id" => 5,"Question" => "Who is the president of Russia?", "Answer" => ["Lavrov", "Putin", "Medvedev"]]
];


I'm adding it on the body dynamically. The questions in h3 tag, the answers in select, and also i have Check button, after click on Check button it must return the count of true answers.

http://prntscr.com/kpy53o[^]

What I have tried:

// quiz.php
<?php 

if($_SERVER["REQUEST_METHOD"] == "POST"){
	$name = $_POST["make"];
        print $name; // in this case it prints only the last select's value 
}
else{
	header("location:index.php");
}


 ?>



<form action="quiz.php" method="post">
		<?php 
		foreach($questions as $value){ 
			?>

			<div id="d1">
				<fieldset>
					<legend id="h1"><?= "Question"." ".$value["id"].":"?></legend>
					<h3 class="h3"><?= $value["Question"]?></h3>
					<select name="make" class="btn btn-outline-info">
						<?php 
						for($i = 0; $i < count($value["Answer"]); $i++){ ?>
							<option><?= $value["Answer"][$i]?></option>
						<?php	}

						?></select></fieldset>
					</div>


					<?php 
				}

				?> 
				<button id="btn1" class="btn btn-success">Check</button>
			</form>


How can i get values from every option? How can i check every condition?

// $count = 0;
if(option1 == "Yerevan"){
count++;
}
if(option2 == "Ronaldo"){
count++;
}

and so on. How to get option1, option2....?
Posted
Updated 3-Sep-18 1:19am
v5
Comments
Mohibur Rashid 3-Sep-18 5:29am    
Where is your $value["Question"]? Its not declared anywhere
Suren97 3-Sep-18 5:36am    
It's in h3 tag
Suren97 3-Sep-18 5:36am    
look before select tag
Mohibur Rashid 3-Sep-18 5:55am    
did you try $_POST["make"]?
Suren97 3-Sep-18 6:00am    
Yes, i tried to keep it in variable, then print it, like this` $name = $_POST["make"]; print $name...It must print all selected values right? But it prints only the last select's value

Your problem is not being able to differentiate answers from different questions.
1. Assign a unique value to the name attribute for the select of each question, why not use the question id? i.e.
<select name="<?= $value['id'] ?>" class="btn btn-outline-info">

2. You have not added a value attribute to each select option:
<option value="<?= $value['Answer'][$i] ?>"><?= $value["Answer"][$i]?></option>

To check the answer upon submission, e.g.
if($_POST["1"]=="Yerevan"){
   count++;
}
// and so on...

good luck!
 
Share this answer
 
v2
Comments
Suren97 3-Sep-18 7:21am    
Thank you very much :)
Mohibur Rashid 3-Sep-18 7:26am    
You beat me by 7 mins, typing on cellphone is not easy
Now I understand. Your problem is you are generating 5 select box with the same name make. And HTML sending all of them, but when you access through post you get the last one. Same effect as when you try to set different value to a same variable.

Solution, name each select object as make[]
And you will get $_POST["make"] as array. You can be more generic by assigning a separate name for all, and put them in the same array as $questions. And through loop assign each select box an uniq name. In case of accessing, you will have to use the same array.
 
Share this answer
 
Comments
Suren97 3-Sep-18 7:21am    
Thank you also :)
Mohibur Rashid 3-Sep-18 7:27am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900