PHP array created through html form only stores last element - php

I'm looping a question for the user with different numerical values for $i. They will input a quantity in the html form that is also looped. Upon them clicking submit, I would like to have an array (with the key ranging form [0] onwards) that stores their response to each particular variant of the question. However, with the code I have written, I only manage to store their last input with the key [0] as if it was the first element of the array. All the previous answers seem to be lost when I call print_r. Please, I would really appreciate it if anyone could point out why this is happening or how I could fix it.
<?php
for ($i=2; $i<=10; $i++)
{
print "question $i";
echo"<form action=\"mysqlranked.php\" method=\"post\">
<input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
</form>
<br>";
}
print "
<form>
<input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
</form>";
if (isset($_POST["formSubmit"]))
{
$var = $_POST["pools"];
}
print_r($var);
?>

You had each of your inputs in a new form and the submit button in its own form as well. I fixed it for you:
<?php
echo "<form action=\"mysqlranked.php\" method=\"post\">";
for ($i=2; $i<=10; $i++)
{
print "question $i";
echo"
<input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
<br>";
}
print "
<input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
";
echo "</form>";
if (isset($_POST["formSubmit"]))
{
$var = $_POST["pools"];
}
print_r($var);
?>
Let me know if it works.

You should not have a new form tag for every question.
Try the code below:
<form action="mysqlranked.php" method="post">
<?php
for ($i = 2; $i <= 10; $i++)
{
print "question $i";
?>
<input type="text" name="pools[]" value="0" maxlength="2" size="2">
<?php
}
?>
<input type="submit" name="formSubmit" value="Submit">
</form>
<?php
if (isset($_POST["formSubmit"]))
{
$var = $_POST["pools"];
}
print_r($var);
?>

<?php
for ($i=2; $i<=10; $i++)
{
print "question $i";
echo"<form action=\"mysqlranked.php\" method=\"post\">
<input type=\"text\" name=\"pools[]\" value=\"0\" maxlength=\"2\" size=\"2\">
<br>";
}
print "
<input type=\"submit\" name=\"formSubmit\" value=\"Submit\">
</form>";
if (isset($_POST["formSubmit"]))
{
for($counter=0;$counter<count($_POST["pools"]);$counter++)
{
$var = $_POST['pools'][$counter]
}
}
print_r($var);
?>

you may use array_push($arrayName, $elementToBePushed);

Related

How to store POST values for each form in an array?

Basically the code is supposed to be simple yet it is not working!
First page:
<?php
$i = 1;
$var;
while($i != 10)
{
$var="
<form id='upload' action='test2.php' method='POST'>
<input type='hidden' name='test' value='{$i}'>
<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div> ";
$i = $i+1;
echo $var;
}
?>
Second page:
<?php
echo $_POST['test'];
?>
when I run the code I always get the last value only (9) ... I need to have different value for each button .. can you help me?
You don't need multiple forms or hidden inputs to achieve this. You can just use buttons, and set their values to $i.
echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';
In test2.php, $_POST['test'] will have the $i value of the button you clicked.
Create single form with multiple input elements name as an array to get multiple value's in single input
Try this:
<input type='hidden' name='test[]' value='{$i}'>
Now you will receive an array of test as $_POST['test'] with different values
The problem with the other proposed solution is that you will have 10 forms, but you won't be able to submit all of the items at once. You will only be able to submit the value of one of them.
I believe you're trying to create 10 input elements instead of 10 separate forms:
<?php
$i = 1;
$var;
$var .= "<form id='upload' action='test2.php' method='POST'>"
while($i != 10)
{
$var .= "<input type='hidden' name='test[]' value='{$i}'>"
$i = $i+1;
}
$var .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>
</div>"
echo $var
?>
Here's code that I would suggest instead of what you've got:
<?php
$html = "<form id='upload' action='test2.php' method='POST'>";
for ($i = 1; $i <= 10; $i++){
$html .= "<input type='hidden' name='test[]' value='{$i}'>";
}
$html .= "<input class='buttom' name='album' id='submit' value='Go to album' type='submit'>"
$html .= "</form>";
echo $html
?>
It's a little more work than what Mahesh's answer would require
First, a question: do you really want 10 forms - or do you want one form with 10 questions. Keep in mind that $_POST will only contain values from the form which was submitted (read: one form).
I think you want something like
<form id="upload" action="test2.php" method="POST">
<?php for ($i = 0; $i < 10; $i++) { ?>
<input name="test[]" type="hidden" value="<?=$i?>">
<?php } ?>
<button type="submit">submit</button>
</form>
edit: given your response below, why not use query parameters?
Go to Album <?=$i?>

Getting values from generated inputs

Hello i have one input that generates " text inputs and one submit input" and what is my problem i cant get the value that is written into the generated inputs by user ... here is my code:
<form method="post" action="">
<input type="text" name="generator"/>
<input type="submit" name="generatingsubmit"/>
</form>
<?php
if(isset($_POST['generator'])){
$generator = $_POST['generator'];
echo "<form method='post' action=''>";
for($i = 0; $i < $generator; $i++){
echo "<input type='text' name='" . $i ."'/>";
}
echo "<input type='submit' name='submit'/>";
echo "</form>";
}
echo $_POST[$i];
?>
I made index.php with the code:
<form method="post" action="index.php?action=post">
<input type="text" name="generator"/>
<input type="submit" name="generatingsubmit"/>
</form>
<?php
if (isset($_GET['action']) && $_GET['action']=='post') {
if(isset($_POST['generator'])){
$generator = $_POST['generator'];
echo "<form method='post' action='index.php?action=get_value'>";
for($i = 0; $i < $generator; $i++){
echo "<input type='text' name='somename[]'/><br />";
}
echo "<input type='submit' name='submit' />";
echo "</form>";
}
}
if (isset($_GET['action']) && $_GET['action']=='get_value') {
$somename=$_POST['somename'];
foreach( $somename as $n ) {
print $n;
}
}
?>
All works fine, inputs are generated then values of inputs are received.. All in one index.php file

Store textbox values in php array

So basically I have the user enter a number on my first screen.
Here is test1.php which generates the number of text boxes that the user had previously entered. That number is $input
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='Names'>";
}
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
Then my test2.php should print all the values entered, but it only prints out the last value entered from test1.php. For example is $input is 4, only the text entered in the 4th text box prints, which is understandable as I don't know how to print all values.
$names=$_POST['Names'];
foreach($number as $num){
echo $num;
}
Is the problem with the name I gave to the textboxes, or something else?
Any help is much appreciated.
Just create a name grouping attribute, so that you'll get an array of inputs instead of just one:
<input type='text' name='Names[]'>
// ^ this is important
Sidenote:
I don't know if this is a typo, but this should be $names instead of $number:
$names = $_POST['Names'];
foreach($names as $num){
echo $num . '<br/>';
}
Sample Demo
Your problem is that you give the same name to all of your input (in your test1.php) so when you try to restore them on your test2.php, your $_POST['Names'] just takes the last input with this name.
A solution is to give a different name to all of your input
In yout first file use it :
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='".$i."'>";
}
echo "<input type='hidden' name='input' value='".$input."'>";
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
And in your 2nd file :
for($i=1; $i<=$_POST['input']; $i++){
echo $_POST['$i'];
}
<form method="post" name="myform">
<input type="text" name="array[]" Value="101"/>
<input type="text" name="array[]" Value="102"/>
<input type="text" name="array[]" Value="103"/>
<input type="text" name="array[]" Value="104"/>
<input type="submit" name="submit" Value="submit"/>
</form>
if(isset($_POST['submit'])){
foreach($_POST['array'] as $myarray) {
echo $myarray.'<br>';
}
OUTPUT
101
102
103
104

Create input field dynamically and need to send value

I am not writing my exact code here but I am giving the basic idea.
<form action="thispageagain.php">
for($i=0; $i<$n; $i++){
echo "<input type=\"submit\" value=\"view post\" />";
}
</form>
Now when someone click on view post i have to send post id for that post. How can I do this. I have to use post method here.
you need to send the post id right... if i did not get your question wrong then you can do it as follow.. i assume that $i represents your post id that you want to send.
<?php
if(isset($_POST['formsubmitted']) && $_POST['formsubmitted'] === 'Y'){
$postId = array_keys($_POST['btnviewPost']);
print_r( $postId);
}
?>
<form action="" method="post">
<input type="hidden" name="formsubmitted" value="Y" />
<?php for($i=0;$i<$n;$i++){?>
<input type="submit" value="view post" name="btnviewPost[<?php echo $i?>]" />
<?php }?>
</form>
Each submit input field needs to be in its own form.
for ($i = 0; $i < $n; $i++) {
echo "<form action="thispageagain.php">";
echo "<input type=\"submit\" value=\"view post\" />";
echo "</form>";
}

online quiz using php and mysql(radio button)

please help, i'm developing an online quiz application. All the questions and answers will be selected from the database. Where i'm having probkem with is to get the values from the radio button whether checked or not. bellow is the code that generate the questions and answers from database.
if (!isset($_POST['submit'])) {
echo "<form method=post action='#'>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$opt5 = $row["ans5"];
$answer = $row["ans"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td><td>$opt4 <input type=radio name=q$id value=\"$opt4\"></td><td>$opt5 <input type=radio name=q$id value=\"$opt5\">q$id</td></tr>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
the name of the radio button is
<input type='radio' name='q$id' value='$opt4' />
How do i get the value of the checked radio button?
or is my PHP code wrong?
what i needed is to output what is selected if a radio button is checked.
if(isset($_POST['submit']))
{
$value = $_POST[''];//the value of the radio button, i don't know what to put here
$n = count($value);
for($i=0; $i < $n; $i++)
{
echo $value[$i];
}
}
Try:
if(isset($_POST['submit']))
{
$value = $_POST;//the value of the radio button, i don't know what to put here
$n = count($value);
for($i=0; $i < $n; $i++)
{
echo $value[$i];
}
}
$_POST[''] was wrong. You needed to use just $_POST.
You could also just use foreach here:
if(isset($_POST['submit']))
{
$values = $_POST;//the value of the radio button, i don't know what to put here
foreach($values as $value)
{
echo $value;
}
}
$_POST is just a array containing whatever your form submited. If you had a name textfield you would use:
echo $_POST['name'];
to echo it.
Try
var_dump("<PRE>", $_POST);
And you will see exactly how your form is being organised. Link to documentation
thanks for your response. i was able to come out with this code and it works but i don't know if the structure or the way i solved it is proper or good enough.
<?php
if (!isset($_POST['submit'])) {
echo "<form method=post action='#'>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$opt5 = $row["ans5"];
$answer = $row["ans"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td><input type=radio name=$id value=\"$id $opt1\">$opt1 </td><td><input type=radio name=$id value=\"$id $opt2\">$opt2 </td><td><input type=radio name=$id value=\"$id $opt3\">$opt3 </td><td><input type=radio name=$id value=\"$id $opt4\">$opt4 </td><td><input type=radio name=$id value=\"$id $opt5\">$opt5 <br>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
?>
what i did was to include the id of the question in the value of the radio button
<input type=radio name=$id value=\"$id $opt1\">
i collect the values of the radio button using $_POST as suggested, i then explode the value into two arrays so the first array will contain the question id and the second will contain the actual value of the radio button (answer).
<?php
elseif (isset($_POST['submit']))
{
var_dump("<PRE>", $_POST);
$sd = $_POST;
foreach($sd as $sd)
{
$tok = explode(" ", $sd, 2);
$w = mysql_query("select * from `questions` WHERE `id` = '$tok[0]'")or die(mysql_error());
if(mysql_num_rows($w) == 1)
{
$sf = mysql_fetch_object($w);
echo $tok[1].".......".$sf->ans."<br>";
}
}
}
?>
it works perfectly but will it give efficient result?

Categories