Getting post values of multiple lines in a form [duplicate] - php

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']); ?>

Related

How to store a PHP variable in a HTML attribute? [duplicate]

This question already has answers here:
PHP Text Field value shows incomplete data, only 1 word
(5 answers)
Closed 11 months ago.
I want to create a page which allows a user to select colours (among the listed ones, via checkboxes). When the user clicks on the submit button, all the selected colours should get displayed with their corresponding colours as their font colour.
Here is my code:
<?php
if(!isset($_POST['button'])) {
?>
Colours<br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=post>
<input type="checkbox" name="colours[]" value="Red">Red<br>
<input type="checkbox" name="colours[]" value="Blue">Blue<br>
<input type="checkbox" name="colours[]" value="Green">Green<br>
<input type="checkbox" name="colours[]" value="Yellow">Yellow<br>
<input type="checkbox" name="colours[]" value="Pink">Pink<br>
<input type="submit" name="button" value="Show"> </form>
<?php
} else {
if (isset($_POST['colours'])) {
echo "Colours selected are:<br><UL type=circle>";
foreach($_POST['colours'] as $color)
echo "<LI><font color='echo $color'; >$color</font>";
echo"</UL>";
} else {
echo "No colour selected";
}
}
?>
There is a bug somewhere, the font colors are not as expected.
I want it like this image below:
Simply write
echo "<li><font color='$color'> $color </font></li>"; // complete li tag
other mistakes:-
a) form method attrubute should be method='post'
b) UL type=circle should be ul type='circle'
When appending a string to variable or another string, you must concatenate the values with a . (dot). for example:
echo("<li><font color=".$color.">".$color."</font>");
Try this;
echo "Colours selected are:<br><UL type=circle>";
foreach($_POST['colours'] as $color) {
echo " <LI><font color='".$color."'>'".$color."'</font></LI>";
}
echo"</UL>";

Multiple checkbox to POST to another page [duplicate]

This question already has answers here:
Get $_POST from multiple checkboxes
(5 answers)
Closed 7 years ago.
I have a list of checkbox (I need to give an id to each), then to make POST to another page
I have a while loop that which brings me one checkbox for each item in the database, I need to make each checkbox to make the POST, on the other page one check or X displays
Edit page.php
while ($fila = mysql_fetch_array($rs)) {
echo "<td > <input type='checkbox' /> </td>}
AnotherPage.php
I guess I have to give an id to each checkbox to make the post and to get on another page.
This seems to be a duplicate of this question: Get $_POST from multiple checkboxes
See the checked answer there.
The main thing in PHP based applications is that a form element with a name with name[] (square brackets at the end) will result in an array in the receiving PHP script. That allows you to have all the values the user selected.
There must be something like this
<form action="AnotherPage.php" method="post">
<input type="checkbox" name="check1" value="1">
</form>
And then in php
if (!empty($_POST['check1'])) {
// make your logic
}
<?php
while ($fila = mysql_fetch_array($rs)) {
<td> <input type='checkbox' name='CheckBoxName[]' value="<?echo $fila['GiveValueHere'];?>"/> </td>
}?>
SomePage.php (Submit Page)
<?
$totalCheckBoxChecked=sizeof($_POST['CheckBoxName']);
for($i=0;$i<$totalCheckBoxChecked;$i++)
{
echo $checkBoxValue=$CheckBoxName[$i];
echo "<br>";
}
?>

hidden array returns OK but text array is empty html to PHP

Form:
<form method="POST" action="edit_work.php">
<input type="hidden" name="wid[]" size="1" value="<?php echo "$wid1" ?>" >
<input type="text" name="course[]" size="15" value="<?php echo "$course1" ?>" >
PHP:
extract($_POST);
for($i=0;$i<$count;$i++) {
echo $wid[$i];
echo $course[$i];
}
gives the wid values OK but not the text entered for the course names.
I have been through all forums for 2 days now. Any help? Thanks.
If you want your PHP to retrieve your data from the form, can't you name your text input "course", then get it inside your PHP with $_POST['course'] ?
What is your $count ?
Using brackets with your name attribute inside your input tag may be dangerous.
If you're using a list of inputs maybe you can define a text format like name="course#" where # is your index and then access it form your $_POST variable using $_POST['course'.$index]
You don't need to extract($_POST) in that case.

How to submit the values of checkbox? [duplicate]

This question already has answers here:
How to pass an array of checked/unchecked checkbox values to PHP email generator?
(5 answers)
Closed 9 years ago.
On my form I have this part :
<input type="checkbox" name="city" value="Nicosia" class="choosecity">Nicosia<br>
<input type="checkbox" name="city" value="Limassol" class="choosecity">Limassol<br>
<input type="checkbox" name="city" value="Larnaca" class="choosecity">Larnaca<br>
and on the results page where I use the mail function, I want to get thechecked cities.
I used this without result:
foreach($_POST['city'] as $checkbox){
echo $checkbox . ' ';
}
What am I missing here?
Use name="city[]". Otherwise you will only be able to submit one city. You may also want to use
$cities = isset($_POST['city']) ? $_POST['city'] : array();
foreach ($cities as $city)
You need to name your inputs as an array name="city[]"
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]" you will get an array when you do this:
$educationValues = $_POST['education']; // Returns an array
print_r($educationValues); // Shows you all the values in the array
So for example:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]"></p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]"></p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]"></p>
Will give you all entered values inside of the $_POST['education'] array.
In JavaScript, it is more efficient to get the element by id...
document.getElementById("education1");
The id doesn't have to match the name:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1"></p>
You just just have to add this [] to input name this will create an array starting with [0]. The result will look so:
array(
[0] => 'Nicosia',
[1] => 'Limassol',
[2] => 'Larnaca',
)
The HTML:
<input type="checkbox" name="city[]" value="Nicosia" class="choosecity" />Nicosia<br>
<input type="checkbox" name="city[]" value="Limassol" class="choosecity" />Limassol<br>
<input type="checkbox" name="city[]" value="Larnaca" class="choosecity" />Larnaca<br>
The PHP:
if( isset($_POST[city]) && is_array($_POST[city]) ){
foreach($_POST[city] as $checkbox){
echo $checkbox . ' ';
}
}

Using a function a dynamic amount of times [duplicate]

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.

Categories