I have two PHP pages named index.php and result.php. I want to perform some operations in index.php page and hold some values to those variables. Then I want to pass them to result.php page.
Will I use action="result.php" in form or will I pass them with header('location: result.php')?
If you suggest header() then where will I put the header() code?
I've searched but didn't get any specific result.
Two php pages
result.php page
<?php
if (isset($_POST['form1'])) {
$right_ans = $_POST[$right_ans_count];
$wrong_ans = $_POST[$wrong_ans_count];
$not_answered = $_POST[$unanswered];
echo "Right Answer: ". $right_ans;
echo "Wrong Answer: ". $wrong_ans;
echo "Not Answered: ". $not_answered;
}
?>
index.php page
<?php
ob_start();
?>
<?php
if(isset($_REQUEST['form1'])) {
$success_message = "<div class='success'>Congratulations! Your answer is right.</div>";
$error_message = "<div class='error'>Sorry! Your answer is wrong.</div>";
$right_ans_count = 0;
$wrong_ans_count = 0;
$unanswered = 0;
$answer1 = $_POST['question1'];
$answer2 = $_POST['question2'];
$answer3 = $_POST['question3'];
$answer4 = $_POST['question4'];
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome to Online Model Test</title>
<style>
body {
background-color: white;
}
.success {
color: green;
}
.error {
color: red;
}
table tr td h2 {
color: cornflowerblue;
}
table tr td h1 {
color: green;
}
</style>
</head>
<body>
<form action="" method="post">
<table>
<tr>
<td><h2> What is our country name? </h2></td>
</tr>
<tr>
<td>
<input type="radio" name="question1" value="Australia">Australia
<input type="radio" name="question1" value="Pakistan">Pakistan
</td>
</tr>
<tr>
<td>
<input type="radio" name="question1" value="Bangladesh">Bangladesh
<input type="radio" name="question1" value="Singapore">Singapore
</td>
</tr>
<td><?php if($answer1=="Bangladesh") {
echo $success_message;
$right_ans_count++;
}
else if($answer1=="") {
$unanswered++;
}
else {
echo $error_message;
$wrong_ans_count++;
}
?>
</td>
<tr>
<td><h2> What is our national flower? </h2></td>
</tr>
<tr>
<td>
<input type="radio" name="question2" value="Belly">Belly
<input type="radio" name="question2" value="Rose">Rose
</td>
</tr>
<tr>
<td>
<input type="radio" name="question2" value="Lily">Lily
<input type="radio" name="question2" value="Gadha">Gadha
</td>
</tr>
<td><?php if($answer2=="Lily") {
echo $success_message;
$right_ans_count++;
}
else if($answer2=="") {
$unanswered++;
}
else {
echo $error_message;
$wrong_ans_count++;
}
?>
</td>
<tr>
<td><h2> What is your national fruit? </h2></td>
</tr>
<tr>
<td>
<input type="radio" name="question3" value="Mango">Mango
<input type="radio" name="question3" value="Lichi">Lichi
</td>
</tr>
<tr>
<td>
<input type="radio" name="question3" value="Orange">Orange
<input type="radio" name="question3" value="Jac-Fruit">Jac-Fruit
</td>
</tr>
<td><?php if($answer3=="Jac-Fruit") {
echo $success_message;
$right_ans_count++;
}
else if($answer3=="") {
$unanswered++;
}
else {
echo $error_message;
$wrong_ans_count++;
}
?>
</td>
<tr>
<td><h2> Who is our national Poet? </h2></td>
</tr>
<tr>
<td>
<input type="radio" name="question4" value="Kazi Nazrul Islam">Kazi Nazrul Islam
<input type="radio" name="question4" value="Rejaul Islam">Rejaul Islam
</td>
</tr>
<tr>
<td>
<input type="radio" name="question4" value="Robi Thagure">Robi Thagure
<input type="radio" name="question4" value="Begum Rokeya">Begum Rokeya
</td>
</tr>
<td><?php if($answer4=="Kazi Nazrul Islam") {
echo $success_message;
$right_ans_count++;
}
else if($answer4=="") {
$unanswered++;
}
else {
echo $error_message;
$wrong_ans_count++;
}
?>
</td>
<tr>
<td>
<input type="submit" name="form1" value="Submit All">
</td>
</tr>
<tr>
<td>
<h1>Result</h1>
<?php if(isset($_REQUEST['form1'])) echo "Correct Answer: ". $right_ans_count.
" Wrong Answer: ". $wrong_ans_count. " Unanswered: ". $unanswered; ?>
</td>
</tr>
</table>
</form>
</body>
</html>
simple solution in your html use the form action
<form action="/result.php" method="post">
so when you submit the form you will get all the post values in our result.php file result.php
<?php
if (isset($_POST['form1'])) {
$answer1 = $_POST['question1'];
$answer2 = $_POST['question2'];
$answer3 = $_POST['question3'];
$answer4 = $_POST['question4'];
if($answer1=="Bangladesh") {
echo $success_message;
$right_ans_count++;
}
else if($answer1=="") {
$unanswered++;
}
else {
echo $error_message;
$wrong_ans_count++;
}
..
..
}
?>
in your result.php file you can check the answer is correct or wrong their.