I have multiple inputs in a file like this:
<form action="card_generate.php">
<input type="text" name="tZero">
<input type="text" name="tOne">
<input type="text" name="tTwo">
<input type="text" name="tThree">
</form>
in the file card_generate.php this have to go in an array i have wrote:
$tabs = array($_POST["tZero"], $_POST["tOne"], $_POST["tTwo"], $_POST["tThree"]);
Is there a way I can put these values in an array through a loop or something instead of putting each value in an array one by one, there can be more values than four values.
Use input name array,
<form action="card_generate.php">
<input type="text" name="t[]">
<input type="text" name="t[]">
<input type="text" name="t[]">
<input type="text" name="t[]">
</form>
And, you will get in post,
print_r($_POST['t']);
You can also use a foreach loop as shown in the example:
HTML
<form method="POST">
<input type="text" name="tZero">
<input type="text" name="tOne">
<input type="text" name="tTwo">
<input type="text" name="tThree">
<input type="submit" name="">
</form>
PHP
<?php
if ( isset($_POST) ) {
foreach ($_POST as $key => $value) {
echo "Name: $key, value: $value";
echo "<br>";
}
}
?>
RESULT
Name: tZero, value: first
Name: tOne, value: hi
Name: tTwo, value: firthfds
Name: tThree, value: fourth value
P.S. Don't forget method="POST" in your <form>.
You can, using array_push: http://php.net/manual/de/function.array-push.php
You can than just loop like this:
for($i = 0; $i < 10; $i++) {
if(isset($_POST["t" . $i])) {
array_push($array, $_POST["t" . $i]);
}
}
Related
Hi friends am trying to pass the input type as array.
Here is my code..
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo sizeof($videoid);
?>
}
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
But am unable to read them for example I want to pass hello,how,are,you,why
but am unable to read them after when I pass them
Here's how I would do it. You only need one texbox. No need for multiple textbox. Try this:
<?php
if(isset($_POST['submit_tags'])){
$tagsList=$_POST['tags_list'];
$videoids = explode(",", $tagsList);
echo sizeof($videoids);
}
?>
Also you can remove the [] from your textbox name name="tags_list[]" to name="tags_list"
1) To send a input value as array to PHP, you need to set one value per input
<input type="text" name="tags_list[]" value="hello">
<input type="text" name="tags_list[]" value="world">
Now the PHP can understand $_POST['tag_list'] as array.
2) You can change your approach and split the string.
<input type="text" name="tags_list" value="hello, world">
And transform the $_POST['tag_list'] string into an array. Example:
$tag_list = explode(', ', $_POST['tag_list']);
Or you can use preg_split function to add more intelligence to your string transformation.
Regards,
To read(convert string) from array you can use implode
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo implode(",",$videoid);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
Sravya try to understand the concept first, name as an array in a html element is used when you want to make more than one html element as same type so that you can get its value by iterating the array like:
<input type="text" name="tags_list[]"> -> its value is one
<input type="text" name="tags_list[]"> -> its value is two
<input type="text" name="tags_list[]"> -> its value is three
You can print its values like:
print_r($tags_list);
Otherwise use single html element.
try this.
$variableAry=explode(",",$variable); //you have array now
foreach($variableAry as $var)
{
echo $var. "<br/>";
}
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
$tag_list = explode(', ', $videoid);
print_r($tag_list);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
How i can take the data from test[] on action.php file?
<form action="action.php" method="post">
<input type="text" name="test[]"><br />
<input type="text" name="test[]"><br />
<input type="submit" value="invia" />
i try this but don't work. How can I print an array in separate data?
echo $_POST["test"];
echo $_POST["test"];
You have to use the index to get the specific items:
echo $_POST["test"][0];
echo $_POST["test"][1];
Or output the array:
print_r($_POST["test"]);
Or loop over it:
foreach($_POST["test"] as $item) {
echo $item;
}
A page is posting an array to me like this:
<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />...
An array.
I can loop through it like this easy enough
foreach ( $_POST['fields'] as $key => $field ) {
echo $key." ".$field['value'] ;
}
Result of above:
first_name jim
email_address 1
address_postal_code 45254
But what I really need to do is reference just the zip (45254) out of the array, maybe like:
echo $_POST['fields']['zip_code']; //or
echo $_POST['fields']['zip_code']['value'];
result: 45254
Is this possible?
Update
<input type="text" name="fields[zip_code][value]" value="45254" />
to be
<input type="text" name="fields[zip_code]" value="45254" />
Edit: I wasn't aware you can't modify the html, that wasn't specified in the original question.
The only thing you can really do is do:
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
However at that point, you might as well just assign $_POST['fields']['zip_code']['value'] to a variable and use that.
If you can't update the html of the form, all you can do is manipulate the data after it's been assigned to the $_POST superglobal like it's any other array
Edit 2: Adding a complete snippet to try:
If you do, what do you get?:
<?php
foreach ( $_POST['fields'] as $key => $field ) {
echo $key." ".$field['value'] ."<br />";
}
echo $_POST['fields']['zip_code']['value'] . "<br />";
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
echo $_POST['fields']['zip_code'];
?>
I just tried that with a simple form of:
<form method="post" action="test.php">
<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />
<input type="submit" />
</form>
And it works as expected.
This seems sloppy but it worked:
foreach ( $_POST['fields'] as $key => $field ) {
$$key = $field['value'] ;
}
echo $address_postal_code;
45254
echo $_POST['fields']['zip_code']['value']
returns nothing
Sorry for the noob question. But I am stuck here.
This is my HTML form where the user-form div can be cloned to as many as possible. The #submit-form div has some hidden values which are common for all.
HTML -
<div class="user-form">
<input type="text" autocomplete="off" name="name[]" >
<input type="email" autocomplete="off" name="mail[]" >
</div>
<div class="user-form">
<input type="text" autocomplete="off" name="name[]" >
<input type="email" autocomplete="off" name="mail[]" >
</div>
<div id="submit-form">
<input type='hidden' name='refer_user_id' value='<?php echo $refer_user_id ?>'>
<input type='hidden' name='refer_user_email' value='<?php echo $refer_user_email ?>'>
<input type="submit" value="Invite" />
<input type="button" class="button" id="clonetrigger" value="Clone" />
</div>
I'm using ajax to submit the form. Basically I want to create accounts using the name and email fields. In PHP How do I use foreach to loop through the name and email fields so that I can create unique accounts?
My print_r($_POST); array looks like this.
Array
(
[name] => Array
(
[0] => david
[1] => Mark
[2] => cindy
)
[mail] => Array
(
[0] => david#abc.com
[1] => mark#abc.com
[2] => cindy#abc.com
)
[refer_user_id] => 2
[$refer_user_email] => test#abc.com
)
Create a loop with a number of iterations equal to the number of submitted name/email pairs, then use the loop counter to access the values for each user.
for ($i = 0; $i < count($_POST['name']); $i++) {
{
$name = $_POST['name'][$i];
$mail = $_POST['mail'][$i];
// Process the new user
}
go through one of the arrays with a foreach, use the key for the second array.
foreach($_POST['name'] as $key =>$name ){
$mail = $_POST[$key];
}
foreach($_POST['name'] as $key => $val) {
echo $val
}
foreach($_POST['mail'] as $key => $val) {
echo $val
}
Easiest way to loop through those elements. You can reference the other elements with $_POST['refer_user_id']. While this works for the purposes of a foreach, the for loop posted above is more efficient, so I'd recommend using it.
http://php.net/manual/en/control-structures.foreach.php More reading on it here.
You can use array_combine function:
$data = array_combine($_POST['name'],$_POST['mail']);
foreach($data as $name=>$mail){
print $name;
//...
}
See array_combine.
You could also use the JavaScript that's auto-generating the form items to give them a name that would result in linking the php object. i.e.
<div class="user-form">
<input type="text" autocomplete="off" name="user[1][name]" />
<input type="email" autocomplete="off" name="user[1][mail]" />
</div>
<div class="user-form">
<input type="text" autocomplete="off" name="user[2][name]" />
<input type="email" autocomplete="off" name="user[2][mail]" />
</div>
Then you could loop through the pairs with foreach($_POST['user'] as $key=>$value) etc...
I am generating the below html form with a foreach loop. Within $people there are 4 $person arrays, and thus 4 repetitions of the below input set.
<form action="handler.php" method="post">
<?php foreach($people as $person) { ?>
<input type="text" name="first_name">
<input type="text" name="middle_name">
<input type="text" name="last_name">
<input type="hidden" name="<?php echo $person['id'];?>
<?php } ?>
</form>
When this is submitted, it only passes the last one, and I would like it to pass each set($person) as an array(as below), so then I can then have a nice array of each one to work with.
Array([0]=>Array(['first_name']=>'James'['middle_name']=>'Green'['last_name']=>'McIntosh')
[1]=>Array(['first_name']=>'Bian'['middle_name']=>'Chip'['last_name']=>'Simpson)'
etc..
But when I return in it, i can't figure out how to get each person into a separate array. I think it might require something a little tricky with the "name" attribute, but haven't been able to get it to work yet.
<?php
foreach ($people as $person) {
printf('<input type="text" name="person[%d][first_name]">', $person['id']);
printf('<input type="text" name="person[%d][middle_name]">', $person['id']);
printf('<input type="text" name="person[%d][last_name]">', $person['id']);
}
?>
Then:
var_dump($_POST['person']);
This is probably what you are looking for:
<form action="handler.php" method="post">
<?php foreach($people as $person) {
echo sprintf('<input type="text" name="first_name[%s]">', $person['id']);
echo sprintf('<input type="text" name="middle_name[%s]">', $person['id']);
echo sprintf('<input type="text" name="last_name[%s]">', $person['id']);
} ?>
</form>