If/else with an area for echo's - php

So Im having a bit of a conundrum. Not too sure how I should go about doing the if/else statements with an Echo at the top with the answers, with bullets.
<?php
echo
?>
</p>
<p>
A Chicken
<input type="radio" name="radio" id="radio3" value="no">
<label for="radio3">Choose this answer</label>
</p>
<p dir="ltr">A Ear of Corn.
<input type="radio" name="radio" id="radio2" value="no">
<label for="radio2">Choose this answer</label>
</p>
<p dir="ltr"> A Heart.
<input type="radio" name="radio" id="radio" value="yes"> Choose this answer
</p>
<p dir="ltr">
<input type="button" name="button28" id="button28" value="Yes! That is the answer I chose!">

If your question is basically about how to output certain HTML code based on an if/else clause, then with such a lengthy amount of HTML code as you have posted you would do much better using HEREDOCS checkout the PHP manual here: http://php.net/manual/en/language.types.string.php.
Goodluck!

You can do this on many ways. Here is a small example to get you started working with forms.
<?php
$answer = '';
if(isset($_POST['send']) //check if the form has been sent
&& isset($_POST['answer'])){ //check if an answer is filled in
//You could also use a switch -> http://php.net/manual/en/control-structures.switch.php
if($_POST['answer'] == 'chicken'){ //check if post variable answer value = chicken
$answer = 'your answer is "a chicken"';
}elseif($_POST['answer'] == 'corn'){ //check if post variable answer value= corn
$answer = 'your answer is "A Ear of Corn."';
}elseif($_POST['answer'] == 'heart'){ //check if post variable answer value = heart
$answer = 'your answer is "A Heart"';
}
//echo the variable.
echo $answer;
}
?>
<form method="POST" action="">
<p> A Chicken
<input type="radio" name="answer" id="radio3" value="chicken">
<label for="radio3">Choose this answer</label>
</p>
<p dir="ltr">A Ear of Corn.
<input type="radio" name="answer" id="radio2" value="corn">
<label for="radio2">Choose this answer</label>
</p>
<p dir="ltr">A Heart.
<input type="radio" name="answer" id="radio" value="heart">Choose this answer
</p>
<input type="submit" name="send" id="button28" value="Yes! That is the answer I chose!">
</form>

Related

How to bind radio type inputs to PHP for a quiz

I want to make a quiz test in PHP so that it will instantly show in every page if the answer chosen by the user is correct or not. So my quiz test will be shown as a question per page, please explain and show me how to modify the following code so that I will know what to do in the other pages:
<main>
<div class="big-div">
<p>1. Which is the biggest planet in our Solar System?</p>
<form method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
<label class="container"> D) Neptune
<input type="radio" name="biggestplanet" value="neptune">
<span class="checkmark"></span>
</label>
<input type="submit" value="Send" class="submit">
</form>
<?php
$answer1 = $_POST["getAttribute('biggestplanet')"];
if ($answer1 == "C) Jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
?>
So, briefly, this is my PHP code that I can't figure out how to write it correctly:
<?php
$answer1 = $_POST["getAttribute('biggestplanet')"];
if ($answer1 == "C) Jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
?>
I'm not for the moment interested in how to send the answers to a database, I just want to know how to show to the user if his answers are correct or not.
This is just a quick way to write this, I honestly would use a switch statement when making this into a quiz, but I corrected your errors.
If you wanted them to show the correct or incorrect answer without having to click the submit button than you would need to use Ajax to you would not need to refresh the page.
<main>
<div class="big-div">
<p>1. Which is the biggest planet in our Solar System?</p>
<form action="sotest.php" method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" id="uranus" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
<label class="container"> D) Neptune
<input type="radio" name="biggestplanet" value="neptune">
<span class="checkmark"></span>
</label>
<input type="submit" value="Send" class="submit">
</form>
<?php
if(isset($_POST['submit'])){
$answer1 = $_POST['biggestplanet'];
if ($answer1 == "jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
}
?>
//////////IN A SWITCH////////
<?php
if(isset($_POST['submit'])){ //stops page from submiting on page refresh
$answer1 = $_POST['biggestplanet'];
switch ($answer1) {
case "uranus":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
case "saturn":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
case "jupiter":
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
break;
case "Neptune":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
}
}
?>
I simplyfied it a bit. (easier to understand)
HTML:
<form method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
</form
PHP:
if (isset($_POST['biggestplanet'])) {
$submittedAnswer = $_POST['biggestplanet']; // VALUE of the checked input - identified by NAME of your input
$correctAnswer = 'jupiter';
if ($submittedAnswer === $correctAnswer) {
echo 'Correct!';
} else {
echo 'Sorry, the correct answer is: ' . $correctAnswer;
}
}
If some day you dont know what exactly is submitted. Try a var_dump($_POST); to show submitted data.
The <label> tag is typically bound to an element using the 'for' attribute, which would match the id of the element to which it should be bound. Example:
<label for="uranus">Uranus</label>
<input type="radio" name="biggestplanet" id="uranus" value="Uranus">
<label for="saturn">Saturn</label>
<input type="radio" name="biggestplanet" id="saturn" value="Saturn">
<input type="submit" value="Submit">
And then to grab the value from the input, you'll need to use the 'name' for the input tag:
<form method="POST">
<label for="uranus">Uranus</label>
<input type="radio" name="biggestplanet" id="uranus" value="Uranus">
<label for="saturn">Saturn</label>
<input type="radio" name="biggestplanet" id="saturn" value="Saturn">
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['biggestplanet'])){
if($_POST['biggestplanet'] === 'Saturn'){
$answer1 = $_POST['biggestplanet'];
}elseif($_POST['biggestplanet'] === 'Uranus'){
$answer1 = $_POST['biggestplanet'];
}
echo $answer1;
}
?>

How to display correct answer in different color than the rest?

I am working on a quiz building application that will let a user enter a quiz question with four possible answers. They must select which answer will be correct with a radio button. I then need to display the answers in a random order with the correct one being a different color than the rest. Currently I able to display the answers in a random order, but I don't know how to make the answer that was selected with a radio button a different color. Here is what it looks like...
And this is the result page...
As you can see, the results are displayed in random order, which is what I want. How can I change the color of "Albany" on the result page to show that it is the correct answer?
Here is my form code....
<form style="text-align:center" action="QuestionReview.php" method="POST">
<br>
<label class="instructions" for="question" >Enter your question here</label><br>
<textarea name="question" rows="5" cols="40"></textarea>
<br><br>
<p class="instructions">
Please provide four answers to your question and select the
correct one.
</p>
<input type="radio" name="selection" value="answerA">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerB">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerC">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerD">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="submit" value="Submit Entry">
</form>
And here is the result page.....
<?php
// Retrieve the question and answers from the HTML form
$question = $_POST['question'];
$answers = $_POST['answer'];
$selection = $_POST['selection'];
shuffle($answers);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Entry Review</title>
<style>
.instructions {
color: #696D6E;
}
</style>
</head>
<body>
<h1 class="instructions">Entry Review</h1>
<p><em>You entered the following question:</em></p>
<p><strong><?php echo $question; ?></strong></p><br>
<p><em>These are the answers you provided:</em>
<p>
<strong>
<?php
foreach($answers as $value) {
echo $value . '<br>';
}
?>
</strong>
</p>
</body>
</html>
You could wrap each answer in <div>-s and add style to them only if they were selected.
It is a good idea to change input field names in your form from selection and answer[] to answer[selected] and answer[body] respectively. This allows you to group together the body and the correctness indicator of an answer. Now you can modify your foreach in the following way:
<?php
foreach($answers as $answer){
// Print div with style, if this answer is correct
$answer['selected'] ? print '<div style="color:green;">' : print '<div>';
echo $answer['body']
echo '</div>'
}
?>
Form
<input type="radio" name="answer[0][selected]" value="true">
<input type="text" name="answer[0][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[1][selected]" value="true">
<input type="text" name="answer[1][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[2][selected]" value="true">
<input type="text" name="answer[2][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[3][selected]" value="true">
<input type="text" name="answer[3][body]" style="width:400px">
<br><br>
PHP Code
<?php
// Retrieve the question and answers from the HTML form
$question = $_POST['question'];
$answers = $_POST['answer'];
shuffle($answers);
?>
// . . .
<?php
foreach($answers as $answer){
// Print div with style, if this answer is correct
$answer['selected'] ? print '<div style="color:green;">' : print '<div>';
echo $answer['body']
echo '</div>'
}
?>
P.S
Make sure to escape the user passed data before you use it!
You can change your input fields as below:
<input type="radio" name="selection" value="answer0">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer1">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer2">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer3">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="submit" value="Submit Entry">
Then you can modify your PHP code(foreach part) as below:
<?php
foreach($answers as $key=>$value) {
if($selection == "answer$key"){
echo "<span style='color:green'>".$value . "</span><br>";
}else{
echo $value."<br>";
}
}
?>

radio buttons value if statement

I have several groups of radio buttons that I want to use in an IF statement (or if you have a better solution)
Users will come to the site, select the buttons, then select submit. After submitting, I want the user to see instantly if they should "refer patient" or "don't refer patient".
I am not sure of a couple of things:
How do I make the "submit" button cause the input to be calculated (meaning, the user gets the instant response)
Since there are several combinations of inputs that can create a "refer" or "don't refer" response, can I add multiple conditions to the IF statement? Also, how can I include radio buttons in the statement - do I just use the "value" of the button. I only learned the very basic method of using numbers..
Below is my code so far. I tried to start the IF statement with values. Not sure if doing it right.
Any help is greatly appreciated!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Indications for Mohs</title>
<?php
$Patient_status='unchecked';
$Primary_status='unchecked';
$Type_status='unchecked';
$BCCT_status='unchecked';
$SCCT_status='unchecked';
$Size_status='unchecked';
$Area_status='unchecked';
if (isset($_POST['Submit'])) {
$selected_radio=$_POST['REFER'];
if (selected_radio == "Healthy" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H" or "Immunocompromised" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H")
?>
</head>
<body>
<form name="Indications" action="" method="POST">
<p><h2><strong><u>Indications for Mohs Surgery</u></strong></h2>
</p>
<strong>Patient </strong>
<div alighn="center"><br>
<input type="radio" name="Patient" value="Healthy">Healthy<br>
<input type="radio" name="Patient" value="Immunocompromised">Immunocompromised<br>
<input type="radio" name="Patient" value="Genetic">Genetic Syndrome<br>
<hr>
<strong>Primary vs Recurrent</strong>
<div alighn="center"><br>
<input type="radio" name="Primary" value="Primary">Primary<br>
<input type="radio" name="Primary" value="Recurrent">Recurrent<br>
<hr>
<strong>Type</strong>
<div alighn="center"><br>
<input type="radio" name="Type" value="BCC">BCC<br>
<input type="radio" name="Type" value="SCC">SCC<br>
<input type="radio" name="Type" value="LM">LM or MIS<br>
<hr>
<strong>BCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="BCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="BCCT" value="Nodular">Nodular<br>
<input type="radio" name="BCCT" value="Superficial">Superficial<br>
<hr>
<strong>SCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="SCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="SCCT" value="Nonaggressive">Nonaggressive<br>
<input type="radio" name="SCCT" value="Verrucous">Verrucous<br>
<input type="radio" name="SCCT" value="KA">KA - type SCC<br>
<input type="radio" name="SCCT" value="Bowen">In situ SCC/Bowen<br>
<input type="radio" name="SCCT" value="AK">AK<br>
<hr>
<strong>Size (cm)</strong>
<div alighn="center"><br>
<input type="radio" name="Size" value="0.5"><0.6<br>
<input type="radio" name="Size" value="0.6-1">0.6-1<br>
<input type="radio" name="Size" value="1.1-2">1.1-2<br>
<input type="radio" name="Size" value="2">>2<br>
<hr>
<strong>Area</strong>
<div alighn="center"><br>
<input type="radio" name="Area" value="H">H<br>
<input type="radio" name="Area" value="M">M<br>
<input type="radio" name="Area" value="L">L<br>
<hr>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
<p><strong><u>Definitions</u>:</strong><br>
Nonaggressive SCC: <2mm depth without other defining features, Clark level ≤III<br>
Area H: 'Mask Areas' of face (central face, eyelids, eyebrows, nose, lips [cutaneous/mucosal/vermillion], chin, ear, and periauricular skin/sulci, temple), genitalia (including perineal and perianal), hands, feet, nail units, ankles, nipples/areola<br>
Area M: Cheeks, forehead, scalp, neck, jawline, pretibial surface<br>
Area L: Trunk and extremities (excluding pretibial surface, hands, feet, nail units and ankles)</p>
</div>
</form>
</body>
</html>
If you want to have the submit display a response instantly you should use JavaScript as this does not require a form submission/call to a server. You can use the onsubmit event.
Regarding checking for if a radio button is checked, use the .checked property of an element:
document.getElementById('elem').checked //true or false
Almost everything is wrong with that code.
Let's start...
You don't have a REFER element in your form, so $_POST['REFER'] is never set. To access radio button values, you need to access with their relevant name as the index key to the $_POST[] array. E.g. $_POST['Patient'], $_POST['Primary'] ...etc. Those will give you the value of the radio button selected within that group.
Secondly, your conditional statements are wrong in the if statement. To compare conditional statements, you have to specifically compare the variable with different values every time. You'd have to say
if ($selected_radio == "Healthy" && $selected_radio == "Patient") {
// code goes here
}
And also, to check which radio button was selected, you need to access $_POST['<Radio_group_name>'] and this will give you the value of the radio button selected for that group. e.g.
$_POST['Patient']
would give Healthy if user selected that one for the group.

sending html form radio box results by email?

can someone please help i am trying to email the results of 4 radio boxes from my form by email. i receive the email fine but am not getting the results sent through.
can someone show me what im doing wrong?
html form:
<html>
<head>
<title>Site Feedback</title>
</head>
<body>
<form name="myform" action="send_feedback.php" method="POST">
<div class="wrapper_feedback" align="left">
<p>Website Design:</p>
<input type="radio" name="design" value="design1">
1
<input type="radio" name="design" value="design2">
2
<input type="radio" name="design" value="design3" checked>
3
<input type="radio" name="design" value="design4">
4
<input type="radio" name="design" value="design5">
5
</p>
<hr>
<p>Ease of Use:</p>
<input type="radio" name="easeuse" value="ease1">
1
<input type="radio" name="easeuse" value="ease2">
2
<input type="radio" name="easeuse" value="ease3" checked>
3
<input type="radio" name="easeuse" value="ease4">
4
<input type="radio" name="easeuse" value="ease5">
5
</p>
<hr>
<p>Fit for Purpose:</p>
<input type="radio" name="purpose" value="purpose1">
1
<input type="radio" name="purpose" value="purpose2">
2
<input type="radio" name="purpose" value="purpose3" checked>
3
<input type="radio" name="purpose" value="purpose4">
4
<input type="radio" name="purpose" value="purpose5">
5
</p>
<hr>
<p>Layout:</p>
<input type="radio" name="layout" value="layout1">
1
<input type="radio" name="layout" value="layout2">
2
<input type="radio" name="layout" value="layout3" checked>
3
<input type="radio" name="layout" value="layout4">
4
<input type="radio" name="layout" value="layout5">
5
</p>
<input type="submit" action="submit" value="submit" name="submit">
<br>
</div>
</form>
</body>
</html>
php:
<?php ob_start(); ?>
<?php
/* Set e-mail recipient */
$myemail = "info#mydomain.com";
$subject = "site Feedback";
/* Let's prepare the message for the e-mail */
$message = "somesite.com Feedback
$design
$easeuse
$purpose
$layout
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
$_SESSION['feedback']="<div class=\"infobox-index\"><strong>Thank You</strong> - We appreciate you taking the time to tell us what you think.</div>";
header("Location: {$_SERVER['HTTP_REFERER']}");
exit();
?>
<?php ob_end_flush() ?>
Your code is relying on Register Globals which is deprecated and discouraged. You should access the data with:
$_POST['design']
Not:
$design
Instead of these variables:
$design
$easeuse
$purpose
$layout
use this:
$_POST['design']
$_POST['easeuse']
$_POST['purpose']
$_POST['layout']
You need to access the POST variables sent from the form using the following syntax:
$_POST['design']
$_POST['easeuse']
$_POST['purpose']
$_POST['layout']

PHP form that shows extra content based on user's response

I'm looking for a way to have a simple form (yes and no radio button questions) where if the user answers yes to all of the questions and hits submit, a hidden link to a file is made visible. I'm no good at creating my own PHP yet...any suggestions?
<form name="myform" action="http://www.mydomain.com/myformhandler.php" method="POST">
<div align="center"><br>
<p>Question nubmer 1...</p><input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No" checked> No<br>
<hr>
<p>Question nubmer 2...</p><input type="radio" name="group2" value="Yes"> Yes<br>
<input type="radio" name="group2" value="No"> No<br>
</div>
</form>
This would be visible if the answer to both questions is yes...
<div>
grab the file here
</div>
<?php
if (isset($_POST['group1']) && isset($_POST['group2'])) {
if ($_POST['group1']=='Yes' && $_POST['group2']=='Water') print '<div>grab the file here</div>';
}
?>
You basically just need to validate the values of the form that is being submitted.
$group1 = $_POST['group1'];
$group2 = $_POST['group2'];
if ($group1 == 'Yes' AND $group2 == 'Yes') echo 'My hidden data'[
You're interested in the GET/POST global arrays, in this case POST:
<form name="myform" action="" method="POST">
<div align="center"><br>
<p>Question nubmer 1...</p>
<input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No" checked> No<br>
<hr>
<p>Question nubmer 2...</p>
<input type="radio" name="group2" value="Water"> Yes<br>
<input type="radio" name="group2" value="Beer"> No<br>
<input type="submit"/>
</div>
</form>
<?php
if ($_POST['group1'] == 'Yes' && $_POST['group2'] == 'Water') {
echo '<div>grab the file here</div>';
}
?>
Try it: http://jfcoder.com/test/grabfile.php
$group1 = $_POST('group1);
$group2 = $_POST('group2);
Then just use '==' (Equal or Set as) to check if they are the same:
if($group1 == 'Yes' && $group2 == 'Yes') print grab the file here';
The code above will print the file. The reason is that the values of the operands are equal.
I'm way late to this party but I thought the OP would like to know you could also do this in JavaScript without posting back to the page:
$('form[name="myform"]').submit(function(event){
event.preventDefault();
if($('input[name="group1"]').val() == "Yes" && $('input[name="group2"]').val() == "Water"){
$('#linkToPDF').show();
}
});
You can see it working here:
http://jsfiddle.net/ajp4r/

Categories