i want get the values of form in other php file but i am getting only one input value as the name are same in loop....How to get all the input values with the same name or if i have to change the name tell me how? and how to get it in other php file
<?php
if(isset($_POST['submit'])){
//getting values of number of MCQ questions and true/false questions
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
$number = 1;
echo '<form action="finalquestions.php" method="post">';
//loop to get inputs of mcq as many as the user posted
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$number.'<input type="text" name="question1" /> </br></br>';
echo '<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>';
echo '</br></br>';
$number++;
}
//loop to get inputs of truefalse as many as the user posted
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$number.' <input type="text" name="question2" /> </br></br>';
echo '<input type="radio" name="question2">True</br>
<input type="radio" name="question1">False</br>';
echo '</br></br>';
$number++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>
Related
In the code below only the values from the last two forms are displayed. What could I be doing wrong?
<form action="" method="post">
<input name="fields[edu][name][]" />
<input name="fields[edu][age][]"/>
<br/><br/>
<input name="fields[edu][name][]" />
<input name="fields[edu][age][]"/>
<br/><br/>
<input name="fields[edu][name][]" />
<input name="fields[edu][age][]"/>
<br/><br/>
<input type="submit" name="submit" value="Submit"/>
if(isset($_POST['submit'])){
print_r($_POST['fields']);
foreach($_POST['fields'] as $field){
echo '<br/>';
echo 'Hello your name is : '.$field['name'];
echo '<br/>';
echo 'Hello your age is : '.$field['age'];
}
}
Update: What would be the best way to echo the values from the array? I've tried several methods none works
<form action="" method="post">
<input name="fields[edu0][name][]" />
<input name="fields[edu0][age][]"/>
<br/><br/>
<input name="fields[edu1][name][]" />
<input name="fields[edu1][age][]"/>
<br/><br/>
<input name="fields[edu2][name][]" />
<input name="fields[edu2][age][]"/>
<br/><br/>
<input type="submit" name="submit" value="Submit"/>
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 am working on a php situation that i have. What it should do is like. each product has a unique id value. if the product id is posted throught submit button, A count action should be activited to count the number of time that product id is posted, so increment.
I hope i exposed the situaation clearly. This is the way i thought doing it, but can't get it work:
<?php
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do;
if ($do > 1)
{
$i= $do+1;
echo $i;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/orange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="2" />
<input type="hidden" id="price" name="price" value="25" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="3" />
<input type="hidden" id="prrice" name="price" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
If I assume the value in the hidden input #id is the times users vote the product:
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do++;
}
If I assume it incorrectly I don't understand what you want to do, sincerly.
I have two text boxes and one button. I want when i will type any value in 1st textbox and click on a button the value which i'll type in 1st text box should be the value of 2nd text box. And i was using this code. Can some one help me on this?
<?php
if(isset($_POST['sum']))
{
$v1=$_POST['abc'];
if($v1=="vivek")
{
echo "welcome Mr.".$v1;
}
else
{
echo "Unauthorised User Mr. ".$v1;
}
}
?>
<html>
<body>
<form method="post" action="">
<p>Name:     
<input type="text" name="abc" value=""/>  
</br></br>
Passed Value: <input type="text" name="xyz" value="<?php echo $v1;?>"/>
</p>
<p>
<input type="submit" name="sum" value=" Submit "/>
</p>
</form>
</body>
</html>
try this one.
<form method="post" action="">
<p>Name:     
<input type="text" name="abc" value=""/>  
</br></br>
Passed Value: <input type="text" name="xyz" value="<?php if(isset($_POST['abc'])) { echo $_POST['abc']; } ?>"/>
</p>
<p>
<input type="submit" name="sum" value=" Submit "/>
</p>
</form>
Passed Value:
<input type="text" name="xyz" value="<?php
if(isset($_POST['abc']))
echo htmlentities($_POST['abc'],ENT_QUOTES);
?>"/>
This should do it :)
Try this code.
<script type="text/javascript">
function sub() {
document.getElementById('xyz').value = document.getElementById('abc').value;
}
</script>
<form>
<label>Amonut</label>
<input type="text" name="abc" onkeyup="sub();" id="abc" />
<br /><br />
<label>Discount</label>
<input type="text" name="xyz" id="xyz" />
<input type="submit" name="sum" value=" Submit "/>
</form>
<script type="text/javascript">
function sub() {
document.getElementById('xyz').value = document.getElementById('abc').value;
}
</script>
<form onsubmit="sub();">
<label>Amonut</label>
<input type="text" name="abc" id="abc" />
<br /><br />
<label>Discount</label>
<input type="text" name="xyz" id="xyz" />
<input type="submit" name="sum" value=" Submit "/>
</form>
I think you want this.
Try this code
$v1='';
if(isset($_POST['sum']))
{
if($v1=="vivek")
{
echo "welcome Mr.".$v1;
$v1=$_POST['abc'];
}
else
{
echo "Unauthorised User Mr. ".$v1;
}
}
When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"