This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to loop through dynamic form inputs and insert into an array
I have a php script and a form. The php script makes an xml file but what i need is for someone to enter a number and that would set that amount of textboxes that would be for someone to write data for that xml file.
So i need it to write <input type="text" name="a #"> however many times the user enters. Also the name needs to be a number but it counts by one ex:<input type="text" name="1"> <input type="text" name="2">... Thanks
<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
$count = $_POST['quantity'];
for ($i=1; $i<=$count; $i++){
#$s.= "<input type=text name=".$i."><br>";
}
?>
now just echo out $s in your html
This?
<form method="get" action="">
<div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>
<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>
<form method="post" action="">
<?php for ($i = 0; $i < $num_inputs; $i++) : ?>
<div><input type="text" name="inputs[]"/></div>
<?php endfor ?>
</form>
Edit: yes, an array is much better than input_x. Updated my answer.
I think what you want is an array of form fields.
You want something like this:
<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter
echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);
?>
This will print five text boxes:
<input type="text" name="mybox[]" />
Then, when you reference these boxes' values, you do so like thus:
<?php
foreach ($_POST['mybox'] as $i) {
echo $i;
}
?>
That is, by using "mybox[]" as the name of each input field, you create an array of textboxes, which you can then iterate through.
Related
Need a little help with my coding. I'm working on a project in which it will accept 5 different numbers and insert them into a PHP array. The codes I have tried are written below. Both of them would make all the 5 contents of my array the same number, appreciate a little help please? Never dealt with array in PHP yet.
<form action="activity_1.php" method="post">
<input type="text" name="number">
<input type="submit" value="Submit" name="submit">
</form>
So far these are the ones I've tried:
$no = array();
for($i=1; $i<=5; $i++){
array_push($no, $_POST['number']);
}
Also
$no = array();
for($i=1; $i<=5; $i++){
//$no[$i]= $_POST['number'];
}
You may use one session variable to store the posted data.
So slightly amend your code into the following :
<?php session_start(); ?>
<form action="#" method="post">
<input type="text" name="number">
<input type="submit" value="Submit" name="submit">
</form>
<?php
if ( !isset($_SESSION["no"]) ) {
$_SESSION["no"]=array();
}
if (isset($_POST["number"])){
array_push($_SESSION["no"], $_POST['number']);
}
for($i=0; $i<5; $i++){
if (isset($_SESSION["no"][$i])) {
if ($_SESSION["no"][$i]!="") {
echo " Number you entered was: " .$_SESSION["no"][$i] . "<br>";
}
}
}
?>
In my opinion the code can be further amended so that
a) you should have a button to "clear" all the previous entered number;
b) I don't know whether it is necessary, but you may wish to add a function to check whether a newly entered number is the same as one of the past numbers entered, and if they are the same, do not put it into the array.
This question already has answers here:
How to get a form input array into a PHP array
(9 answers)
Closed 7 years ago.
I have an HTML/PHP form that lists multiple inputs in which users can change values. I have a PHP while loop create the fields like so:
...
$result = mysql_query($query);
while (list($a,$b,$c) = mysql_fetch_row($result))
{
echo '<tr>';
echo '<td>$a</td>';
echo '<td><input name="b" type="text" value="'.$b.'"</input></td>';
echo '<td><input name="c" type="text" value="'.$c.'"</input></td>';
echo '</tr>';
}
...
For this example I can have multiple lines of a,b,c and want to get all of the values when I submit the form via POST. Only $b and $c are input values that can be changed. Do I create variables $a,$b,$c as arrays, and if so, how do I set that up so that all of the values will be stored?
submit your inputs with name like b[] & c[]
HTML
<input type="hidden" name="a[]" value="a_value" />
<input type="text" name="b[]" value="b_value" />
<input type="text" name="c[]" value="c_value" />
PHP
<?php print_r($_POST['a']); ?>
<?php print_r($_POST['b']); ?>
<?php print_r($_POST['c']); ?>
Attempt 1: This shows what I want to achieve. The same array but different key and value pair.When printed, it list out all the keys and values.
<?php
$rental[]=array('day'=>1,'rate'=>10);
$rental[]=array('day'=>2,'rate'=>20);
$rental[]=array('day'=>3,'rate'=>30);
$rental[]=array('day'=>4,'rate'=>40);
print_r($rental);
?>
Attempt 2: Now, I'm using a single form to submit multiple times.I expect the key and values to be stored inside an array each time the form is submitted. But what happens is, the last value pair overwrites the previous one.Therefore only one pair is shown. So far, I can store them with the help of session. But I just wonder if there's a way to achieve it via php array on the same page?
<?php
$rental[]=array('day'=>$_POST['days'],'rate'=>$_POST['rental']);
print_r($rental);
?>
<form action="#" method="post">
Number of Days: <input type="text" name="days" value=""><br/>
Rental rate: <input type="text" name="rental" value=""><br/>
<input type="submit" name="add_rental_rate" value="Add Rental Rate">
</form>
You can use session to achieve what you want, see this code:
<?php
session_start();
if(isset($_POST['days']) && isset($_POST['rental'])){
$_SESSION['info'][] = array($_POST['days'] => $_POST['rental']);
}
if(isset($_SESSION['info'])) {
for($i = 0; $i < count($_SESSION['info']); $i++) {
foreach($_SESSION['info'][$i] as $days => $rental){
echo '<p>' . $days . '<br>';
echo $rental . '</p>';
}
}
}
?>
<form action="#" method="post">
Number of Days: <input type="text" name="days" value=""><br/>
Rental rate: <input type="text" name="rental" value=""><br/>
<input type="submit" name="add_rental_rate" value="Add Rental Rate">
</form>
You should read some docs about session:
http://php.net/manual/en/intro.session.php
This title might not describe my question too well but I was unsure how to name this post... Anyways, I have a form that has dynamically generated input boxes that pulls the last 4 years with the following:
<?php
$current_date = new DateTime();
for ($i = 1; $i <= 4; $i++) {
$current_date->modify('-1 year');
$date_string = $current_date->format('Y')
?>
<fieldset name="gross_sales">
<input type="number" name="year_brand_gross[<?php echo $date_string; ?>]" placeholder="Gross Sales for <?php echo $date_string; ?>">
</fieldset>
<?php
} // end while
?>
And once the user clicks submit the form data is processed via my process.php file that contains the following:
$year_brand_gross[1] = $_POST['year_brand_gross'][1];
$year_brand_gross[2] = $_POST['year_brand_gross'][2];
$year_brand_gross[3] = $_POST['year_brand_gross'][3];
$year_brand_gross[4] = $_POST['year_brand_gross'][4];
Now I'm pretty sure the above part is not right. So this is my question... How would I get the info from these inputs into my email that's sent since their created by an array and not "actually" there. Here's a stripped down version of my html email that's sent which I'm pretty sure is also wrong since the above code is incorrect:
<table>
<tr>
<td>Gross Sales:</td>
</tr>
<tr>
<td>{$year_brand_gross[1]}</td>
<td>{$year_brand_gross[2]}</td>
<td>{$year_brand_gross[3]}</td>
<td>{$year_brand_gross[4]}</td>
</tr>
</table>
Any help is greatly appreciated!
Your form would actually look like
<input type="number" name="year_brand_gross[2012]" ... />
<input type="number" name="year_brand_gross[2011]" ... />
<input type="number" name="year_brand_gross[2010]" ... />
etc...
That means you need to use
$_POST['year_brand_gross'][2012]
$_POST['year_brand_gross'][2011]
$_POST['year_brand_gross'][2010]
etc...
on the server.
foreach($_POST['year_brand_gross'] AS $yeah => $value) {
// use $year and $value variables to do whatever
// this code will execute once for each values in $_POST['year_brand_gross'].
// note: print_r($_POST);
// $_POST is an array, same for $_GET and so on
}
so i have this code fragment here..
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question<?php echo $i; ?>' rows=3 cols=90></textarea></p>
<input type="radio" name="answer<?php echo $i; ?>" value="True"> True
<input type='radio' name="answer<?php echo $i; ?>" value="False"> False<br><br><br>
<?php
}
}
... i am making a quiz maker in php...
the first thing to do is to set up the desired number of questions, so the value entered will go on the $numTF variable. Depending on the entered value, the textarea part will be printed. and there will be different names for each text area. AND THE CODE ABOVE IS WHERE U PRINT THE FORMS AFTER U ENTER THE DESIRED VALUE.
The next thing is to save that in a database. since the name of each textarea will be based on a variable value($i) that is used in a loop (name="answer") , HOW CAN I USE IT IN $_POST??? Like, would i do it like this?? ($_POST['question']).
HOW CAN I SAVE THESE QUESTIONS IN A DATABASE??
PLEASE HELP ME ....
I WOULD BE SO MUCH MUCH MUCH GRATEFUL FOR A LIL HELP.
<?
var_dump($_POST);
?>
<form method="post">
<?
$numTF=4;
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question[<?php echo $i; ?>]' rows=3 cols=90></textarea></p>
<input type="radio" name="answer[<?php echo $i; ?>]" value="True"> True
<input type='radio' name="answer[<?php echo $i; ?>]" value="False"> False<br><br><br>
<?php
}
}
?>
<input type="submit" name="submit" value="submit"/>
</form>
Use $_POST['question'][1] // To get first question
Use $_POST['answer'][1] // To get first answer
Use loop to get all question and answers
I agree with Sachin as far as using name='question[]'. To answer question a little more as far as storing it in a database. Personally I would use a JSON array.
$store_answers = json_encode($_POST['answer']);
$store_questions = json_encode($_POST['question']);
Then just store $store_string in a TEXT field in your database. Then when you pull it back out of the database you can simple use:
$answers = json_decode($store_answers);
$questions = json_decode($store_questions);
Then you can loop through using a foreach like so:
foreach($questions as $key=>$question) {
echo "Question $key = {$answers[$key]} <br />";
}
This will display the results for each question.