I want to get the values that belong to the answers of a test.
There could be any number of questions.
A question has the HTML:
<form method='post' action='calificar.php'>
<div class='pruebaAlumno'>
<h5>Pregunta 3</h5>
<h3>¿Que factores definen la capacidad de una persona?</h3>
<input type='checkbox' name='respuestaAlumno[]'
value='Voluntad'> Voluntad <br>
<input type='checkbox' name='respuestaAlumno[]'
value='Cultura'> Cultura <br>
<input type='checkbox' name='respuestaAlumno[]'
value='Hábitos'> Hábitos <br>
<input type='checkbox' name='respuestaAlumno[]'
value='Habilidad'> Habilidad <br>
<input type='checkbox' name='respuestaAlumno[]'
value='Perfil'> Perfil <br>
<input type='checkbox' name='respuestaAlumno[]'
value='Dones'> Dones <br>
<input type='hidden' name='idPregunta' value='41'>
<input type='hidden' name='tipo' value='C'>
</fieldset>
</div> <div class='calificar'>
<input type='submit' name='calificar' value='enviar' >
</div>
</form>
I want to get
Answer (respuestaAlumno)
questionID (idPregunta)
type (tipo)
from each question.
Then I want to save the data in an array.
A test can have as many questions as the teacher wants. The form has one submit button.
I´ve tried to get the data in different ways. Using JS DOM. Using POST.
I´ve found some PHP DOM tutorials but them all teach how to modify an static HTML.
Please help me to get the submitted data from each fieldset.
Thanks in advance.
You could format your form names like this.
First question
name="respuestaAlumno[0][]"
name="respuestaAlumno[0][]"
...
name="idPregunta[0]"
name="tipo[0]"
Second question
name="respuestaAlumno[1][]"
name="respuestaAlumno[1][]"
...
name="idPregunta[1]"
name="tipo[1]"
You would end up with three arrays in your $_POST.
Try this
<?php
if(isset($_POST['calificar'])
{
$respuestaAlumno=$_POST['respuestaAlumno'];
//you need respuestaAlumno array to single string
echo $data_to_string=implode(",",$respuestaAlumno);
$count_of_respuestaAlumno=count($_POST['respuestaAlumno']);
for($i=0;$i<=$count_of_respuestaAlumno-1;$i++)
{
echo $data=$respuestaAlumno[$i]."/n";
}
//question id
echo $idPregunta=$_POST['idPregunta'];
echo $tipo=$_POST['tipo'];
//do something
}
Related
Here's my form
<form id="place-bid" action="placebid.php?placeBidProductId='.$row['product_id'].'" method="post" >
Your Bid:<br>
<input type="text" placeholder="$$$" name="money">
<input type="Submit" class="button_bid" name="submit" value="Place Bid">
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
</form>
and here is my placebid.php
<?php
// $product_id=$_GET['placeBidProductId'];
$product_id=$_POST['row_id'];
echo " Id Produs : ".$product_id;
?>
My problem: I can't get the value from $row['product_id'], it will only echo the string "$row['product_id'] .$product_id;"
I'm guessing that before you are outputting that form you are doing some sort of query of a database. If that is the case then you likely want to change this line
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
to
<input type=hidden id='rowid' value="<?php echo row['product_id'];>" name='row_id'>
Use print_r($_POST) for all form post value on form action page i.e. placebid.php. it will show you all value in form. Then make you business logic accordingly.
I'm making a Quiz. And with each question I'm showing the possible answers( "True" or "False") with a While loop in PHP:
echo "<form method='post' action='quizCheck.php'>";
while(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer' value='true' />
</label>
<label>False
<input type='radio' name='answer' value='false' />
</label>
</div>";
}
echo "</form>";
Let's say there are 10 questions and I select "True" on 6 questions.
What code do I have to put in quizCheck.php so it can count the number of "True" answers and store it in a variable?
You will need to do two things, first you need a submit button in the form:
<button type="submit" value="Submit">Submit</button>
Then you will also need the names of the radio inputs to be unique so in the while loop (which you really should just change to a for loop) do:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answer{$x}' value='true' />
</label>
<label>False
<input type='radio' name='answer{$x}' value='false' />
</label>
</div>";
}
When the form is submitted, then in quizCheck.php you just check $_POST[answer0] through $_POST[answer9] to see which are true and increment a counter.
If you want the answers in a single array then do this:
for(x=0;x<=10; x++){
echo "<div class='buttons'>
<label>True
<input type='radio' name='answers[$x]' value='true' />
</label>
<label>False
<input type='radio' name='answers[$x]' value='false' />
</label>
</div>";
}
When this form is submitted, then in quizCheck.php you just get something like $answers = $_POST[answers] and then go through answers[0] to answers[9] for example
so I have a form. The form consists of 10 lines by default. It goes like this:
<form method="post" action="actionhere">
<?php
for($i=0; $i<10;$i++) {
?>
<div class='clone_me'>
<span>Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' name='tx_<?php echo $i;?>'/>
</div>
<?php } ?>
<input type='submit' name='submit' value='Submit'/>
</form>
So inside the form, we will have 10 rows of checkbox+textbox.
What I'm trying to make is, I want to place a button to add new row (the checkbox+textbox). Now, problem is, I need the $i value (since it's form the for loop). Is that possible that when we click the add row button, the value of $i that we set inside for loop be incremented by 1 on each click? I know we can clone the div using jquery, but how about the $i value?
I think you are doing it in wrong way you do not need $i value inside name attribute you have to use array for it for example
<form method="post" action="test.php">
<div class='clone_me'>
<span>Line 1</span>
<input type='checkbox' name='ck[]'/><!--this field should menditory-->
<input type='text' name='tx[]'/>
<span>Line 2</span>
<input type='checkbox' name='ck[]'/><!--this field should menditory-->
<input type='text' name='tx[]'/>
</div>
<input type='submit' name='submit' value='Submit'/>
</form>
Now implement this code in actionhere.php
<?php
$cks = $_POST['ck'];
$txs = $_POST['tx'];
foreach($cks as $key => $ck) {
echo $ck."<br>";
echo $txs[$key]."<br>";
}
?>
Well, basically no. Your PHP script is already over when the html has been generated. So you can't rely anymore on PHP. But you don't need to make it explicitly appear in your html.
You should count the rows using jquery :
var i = $('form').find('.clone_me').length
and then add a new row using javascript again :
$('form .clone_me:last').clone().insertAfter('form .clone_me:last');
<input type='hidden' name='counter' id='counter'/>
<input type='checkbox' name='chk' id='chk'/>
<?php
$counter=$_POST['counter'];
for($i=0;$i<=$counter;$i++)
{
$chk=$_POST['chk'.$i];
// Your Insert Code Here
}
?>
may be this can be but have some limit
if you have no problem with page reload the you can do it is:-
by this way your page will reload and the value in textbox and checkbox will gone:---:)
every time new page generate and send by server to client browser
<?php
if(isset($_POST['submit'])){
$ends = $_POST['ttl_rows'];
}else{
$ends = 10;
}
?>
<form method="post" action="#">
<?php
for($i=1 ; $i<$ends;$i++) {
?>
<div class='clone_me'>
<span>Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' name='tx_<?php echo $i;?>'/>
</div>
<?php } ?>
<input type='hidden' name='ttl_rows' value='<?php echo ($i+1); ?>'/>
<input type='submit' name='submit' value='Submit'/>
</form>
I have 3 textfields and only one submit button to send all the data of the textfield at once. But how do I send the data of all three textfields at once in php?
<form>
for($x=0;$x<3;$x++){
<input type="text" name="name">
}
<input type="submit" name="submit">
</form>
Now I have a three fields inside a for loop and I have to extract data from all of them using single submit button.So how can I do that?
Using the 'name' attribute on an input allows you to do this for example
<form action='submit.php' method='post'>
<input type='text' name='one'></input>
<input type='text' name='two'></input>
<input type='text' name='three'></input>
<input type='submit' name='submit' value='Submit!' />
</form>
and in your PHP you would do something like this
<?php
if(isset($_POST['submit'])){
$inputOne = $_POST['one'];
$inputTwo = $_POST['two'];
$inputThree = $_POST['three'];
//Do whatever you want with them
}
?>
There are better ways of doing this, but this is probably the simplest to understand
If you want all the inputs to have the same name do this
<input type='text' name='textinput[]'></input>
Use that instead and loop through all of the inputs like so
<?php
foreach($_POST['textinput'] as $input){
//do something with $input
}
?>
You put them all inside the same <form> and make sure they have different values for the name attributes (or that the values end in []).
I believe what you are looking for is this Note the [ ] behind the name field
<form>
for($x=0;$x<3;$x++) {
<input type="text" name="name[]" />
}
<input type="submit" name="submit" />
</form>
Then to retrieve the values
$names = $_POST['name'];
foreach( $names as $name ) {
print $name;
}
I've been working on a small project lately that involves question and answer. So basically it's a quiz/test, whatever you call this page. I made a database as per requirement for all the questions and answers storage. So yea, the output is coming from a database. Now, I need to output 5 questions at a time in a page so used pagination to do it.
$outputList = '';
while($row = mysql_fetch_array($sql2)){
$question = $row['questions'];
$opta = $row['optA'];
$optb = $row['optB'];
$optc = $row['optC'];
$optd = $row['optD'];
$id = $row['ctrlNo'];
$outputList .="
<p>$id. $question</p>
<div>
<input type='radio' name='question-$id-' id='question-$id-answers-A' value='A' />
<label for='question-$id-answers-A'>A) $opta </label>
</div>
<div>
<input type='radio' name='question-$id-' id='question-$id-answers-B' value='B' />
<label for='question-$id-answers-B'>B) $optb </label>
</div>
<div>
<input type='radio' name='question-$id-' id='question-$id-answers-C' value='C' />
<label for='question-$id-answers-C'>C) $optc </label>
</div>
<div>
<input type='radio' name='question-$id-' id='question-$id-answers-D' value='D' />
<label for='question-$id-answers-D'>D) $optd </label>
</div>
";
}
Okay, so I have 4 radio buttons for the choices of each question and they are all working perfectly. What I can't figure out is collecting their values during pagination. I mean, when I'm on page 1 and submit my answers it's working, but when I go to page 2 and submit my answers it doesn't pass the values collected from page 1. Now I'm running out of ideas how to do it. Can anyone guide me as to what I can possibly do to gather all the values of the selected radio button first from page 1 to the current page of the user and pass it to my php page, which verifies the answers? Here's the code in my html.
<form id="form1" name="form1" action="testpage5.php" method="POST" >
<?php print "$outputList"; ?>
<p><div style="margin-left:58px; margin-right:58px; padding:6px; background- color:#FFF; ">
<?php echo $paginationDisplay; ?>
</div></p>
<input type="submit" value="Submit Quiz"></input>
</form>`
testpage5.php is the page where I verify the answers comparing value of $_POST to the correct answer from the database. I used $_POST to collect the values of the radio buttons.
Have a look at $_SESSION. It's a super global array which stores values of a browser session.
An alternative is creating hidden form elements which contain the values of the previously submitted pages.