I have a form with 3 selections, and I want the form to replace the selected text with the name entered when submitted. So if the user enters their name as Josh, and picks 'King', I want the form to update itself and instead of having King it would say Josh which would no longer be a choice, and then have the other two characters below for someone else to pick from. This is a PHP document, but the data is in a HMTL form of course.
I was reading something about hidden forms, but was not sure how to implement them here. Does anyone have any suggestions?
<form method="get" action="">
Your Name: <input type="text" name="name" />
<br/>
<input type="radio" name="character" value="King"> King<br>
<input type="radio" name="character" value="Queen"> Queen<br>
<input type="radio" name="character" value="Prince"> Prince <br>
<input type="submit" value="Submit" />
Are you looking to do something like this?
You can do it by using php conditions and hidden fields.
Save the file with the name "form.php" then run it and tell me if it does the trick ;)
<form method="post" action="form.php">
Your Name: <input type="text" name="name" />
<br/>
<?php if((isset ($_POST['name-1']) || isset($_POST['name'])) && (isset ($_POST['character-1']) && $_POST['character-1']=='King'))
{
$name=isset($_POST['name-1'])?$_POST['name-1']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-1" value="'.$name.'"/><input type="hidden" name="character-1" value="'. $_POST['character-1'].'"/>';
}
else {?>
<input type="radio" name="character-1" value="King"> King<br>
<?php } ?>
<?php if((isset ($_POST['name-2']) || isset($_POST['name'])) && (isset ($_POST['character-2']) && $_POST['character-2']=='Queen'))
{
$name=isset($_POST['name-2'])?$_POST['name-2']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-2" value="'.$name.'"/><input type="hidden" name="character-2" value="'. $_POST['character-2'].'"/>';
}
else {?>
<input type="radio" name="character-2" value="Queen"> Queen<br>
<?php } ?>
<?php if((isset ($_POST['name-3']) || isset($_POST['name'])) && (isset ($_POST['character-3']) && $_POST['character-3']=='Prince'))
{
$name=isset($_POST['name-3'])?$_POST['name-3']:$_POST['name'];
echo $name.'<br><input type="hidden" name="name-3" value="'.$name.'"/><input type="hidden" name="character-3" value="'. $_POST['character-3'].'"/>';
}
else {?>
<input type="radio" name="character-3" value="Prince"> Prince <br>
<?php } ?>
<input type="submit" value="Submit" />
</form>
Related
I would like to store an user input from a textbox into an external php array by clicking a button. Also, there is a dependency from two radio buttons.
This is my current attempt:
At first, I include the external php array:
<?php
include ('../array.php');
?>
The input form:
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
Insert the value into the array:
<?php
if (isset($_POST['send'])){
if (isset ($_POST['name'])){
if ($_POST['name']=='Type1'){
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
}
?>
But the values don't "stack", meaning that the array isn't growing with each button click.
Can anyone help please? :D
Thanks in advance!
If you want to stack values, you need to store them somewhere to keep values between each requests to server.
Values can be stored in session (for current user only) or in any database supported by PHP.
Here an example with session :
index.php :
<?php
include ('./array.php');
?>
<form action="" method="post">
<input type="text" name="textfield" value="">
<input type="radio" name="name" value="Type1">Type1<br>
<input type="radio" name="name" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
array.php :
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['send']) && isset($_POST['name']) && $_POST['name']=='Type1') {
$_SESSION['persistentValues'][] = $_POST['textfield'];
}
print_r($_SESSION['persistentValues']);
?>
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
in the file php put this
<?php
if (isset($_POST['send']) && $_POST['textfield'] && $_POST['name']){
if ($_POST['name']=='Type1'){
array = require('./array.php');
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
?>
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>";
}
}
?>
I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>
I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />
I have this form:
<form action="" method="post">
<label for="color">Color:</label>
<input type="text" style="width: 195px;" name="color" id="color" value="<?php echo $item; ?>">
<input value="Save" type="submit" />
</form>
And the following PHP to capture contents:
if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0 ) {
$thisarray[] = $_POST['color'];
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
}
How can I add a new field to the Form and then add that to the array to be saved?
You can add new fields to the form if you alter the HTML code. There are many different form elements, see if there is something you need here: http://www.w3schools.com/html/html_forms.asp
You can access the value of each element with $_POST['key'], so to add it to the array you would have to write $thisarray[] = $_POST['key']. Note: Replace key with the actual name of the form element.
Whole Example:
HTML:
<form action="" method="post">
<label for="color">Color:</label>
<input type="text" style="width: 195px;" name="color" id="color" value="<?php echo $item; ?>">
<input name="the_new_element" />
<input value="Save" type="submit" />
</form>
PHP:
if (!empty($_POST['color']) && preg_match('/^#[a-fA-f0-9]{6}/', $_POST['color']) != 0) {
$thisarray[] = $_POST['color'];
$thisarray[] = $_POST['the_new_element'];
SetProfileField('usercss', $USER->id, implode(',',$thisarray));
}
I named the field the_new_element, change this to whatever you want. Of course you should also sanitzie the contents.