This question already has answers here:
How to get an array of data from $_POST
(3 answers)
Closed 1 year ago.
I have a form that is a little complex and I am hoping to simplify the server-side (PHP) processing by natively POSTing an array of tuples.
The first part of the form represents a User:
First Name
Last Name
Email
Address
etc
The second part of the form represents a Tree:
Fruit
Height
etc
The problem is that I need to be able to POST multiple Trees for a single User in the same form. I would like to send the information as a single User with an array of Trees but this might be too complex to do with a form. The only thing that comes to mind is using javascript to create some JSON message with a User object and an array of Tree objects. But it would be nice to avoid javascript to support more users (some people have scripts turned off).
check this one out.
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">
<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">
<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">
<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">
it should end up like this in the $_POST[] array (PHP format for easy visualization)
$_POST[] = array(
'firstname'=>'value',
'lastname'=>'value',
'email'=>'value',
'address'=>'value',
'tree' => array(
'tree1'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree2'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree3'=>array(
'fruit'=>'value',
'height'=>'value'
)
)
)
You can also post multiple inputs with the same name and have them save into an array by adding empty square brackets to the input name like this:
<input type="text" name="comment[]" value="comment1"/>
<input type="text" name="comment[]" value="comment2"/>
<input type="text" name="comment[]" value="comment3"/>
<input type="text" name="comment[]" value="comment4"/>
If you use php:
print_r($_POST['comment'])
you will get this:
Array ( [0] => 'comment1' [1] => 'comment2' [2] => 'comment3' [3] => 'comment4' )
Related
I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array
I'm making order page which contain a list of items and each of them has input to type in the amount you want to order.
The markup is like this:
<label for="item1">Fried Rice</label>
<input type="number" id="item1" name="item1">
...
//I have like 10 more items below
So basically, how do I get the string Fried Rice in PHP?
I'm thinking of hard-coding the input's name to "fried rice", but it will be troublesome if someday the item name is changed.
Any solution? Thanks. I'm new to PHP by the way.
I would do it like this
<label for="item1">Fried Rice</label>
<input type="number" id="item1" name="item1[Fried Rice]">
--EDIT--
if you have a mapping of your indices (textual name) with the number then just put the index number
<label for="item1">Fried Rice</label>
<input type="number" id="item1" name="item1[id goes here]">
--EDIT-- with sample output
Array
(
[item1] => Array
(
[Fried Rice] => 1
)
)
you can take as hiddne value for item like
<label for="item1">Fried Rice</label>
<input type="hidden" id="item1_desc" name="item1_desc" value="Fried Rice">
<input type="number" id="item1" name="item1">
...
please take as you need more for 10
in post you will get [item1_desc] for your label value
let me know if i can help you more.
Try something like
<?php $name='Fried Rice' ?>
<label for="item1"><?php echo $name ?></label>
<input type="number" id="item1" name="<?php echo $name ?>">
This way, if the name changes, you only need to change it in one place.
We are building an advanced form with following fields...
<input type="text" size="50" name="name" >
<input type="text" size="50" name="custid" >
<input type="text" size="50" name="projectname" >
<input type="text" size="50" name="overdue" >
We are using jquery autocomplete to select name from MySQL and what we would like to do is:
We want to add custid, projectname, and overdue to their fields after adding name automatically, we can do it in PHP but how can we do it using jQuery or ajax? Can you help us to write up the code? We can add 1 output using ajax but how do we add multiple outputs? thanks.
You could retrieve the remaining fields using AJAX, by having a PHP script which outputs their details in JSON format. So if your data is stored in an associative array called $data, you can simply do echo json_encode( $data );. Each field name in the array, will be the name of the instance variable you'll need to call in the jQuery. The code below assumes your array field names are the same as your inputs.
You then use jQuery.get( ) (or similarly jQuery.ajax( )) to obtain the result. jQuery below:
$.get('myscript.php',
{name: name},
function(data) {
// If successful, fill out the fields
$('#custid').val(data.custid);
$('#projectname').val(data.projectname);
$('#overdue').val(data.overdue);
},
// tells jQuery it's retrieving a JSON encoded object
'json'
);
This will also require you to slightly alter your HTML as below:
<input type="text" size="50" name="custid" id="custid" >
<input type="text" size="50" name="projectname" id="projectname" >
<input type="text" size="50" name="overdue" id="overdue" >
I have a form, it passes the following values:
image_title,
image_description,
image_file
I wanted to present this form multiple times, so I did the following:
image_title_1,
image_description_1,
image_file_1
image_title_2,
image_description_2,
image_file_2
Multiple times, so I have the fields 1 - 10. I submit the form and print out the contents of the POST array, the only problem is any "image_title_#" after "image_title_1" doesn't exist in the array: but everything else does.
So the array would look something like:
image_title_1 -> "title!"
image_description_1 -> "description!"
image_file_1 -> file
image_description_2 -> "description!"
image_file_2 -> file
image_description_3 -> "description!"
image_file_3 -> file
So to work out what it is I swapped description and title with each other, however title still doesn't display for after 1. I'm not doing ANY processing, I'm literally just printing out the $_POST array before even touching it. This makes no sense, what could be causing it?
To clarify: The problem is "image_title_#" (example: image_title_3) doesn't get passed except for image_title_1, even if I re-arrange the order. I do no processing before outputting.
Edit, the html source is just:
<form method="post" action="">
<input type="text" name="image_title_1"></input>
<input type="text" name="image_description_1"></input>
<input type="text" name="image_file_1"></input>
<input type="text" name="image_title_2"></input>
<input type="text" name="image_description_2"></input>
<input type="text" name="image_file_2"></input>
<input type="text" name="image_title_3"></input>
<input type="text" name="image_description_3"></input>
<input type="text" name="image_file_3"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
A better solution would be converting them to array, try this instead:
<form method="post" action="">
<input type="text" name="image_title[]"></input>
<input type="text" name="image_description[]"></input>
<input type="text" name="image_file[]"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
Now, in your PHP script, you can get their array like this:
print_r($_POST['image_title']);
print_r($_POST['image_description']);
print_r($_POST['image_file']);
.
Suffixing field name with [] converts it to array. The other good thing here is that it has shortened your code too.
Once you have the array, you can loop through them using foreach:
foreach($_POST['image_title'] as $key => $value)
{
// process them any way you want
}
The code works. I just cut and paste your form and do a test submit
Array
(
[image_title_1] => 1
[image_description_1] => 2
[image_file_1] => 3
[image_title_2] => 4
[image_description_2] => 5
[image_file_2] => 6
[image_title_3] => 7
[image_description_3] => 8
[image_file_3] => 9
[submit] => submit
)
I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )
Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:
$_POST['form1']['datax']
or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:
$array1 = array($_POST['datax'],$_POST['datay'],...);
$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);
since there are around 60 fields.
I hope I was able to explain this well and as always thank you very much..
If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:
<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />
Goes to:
print_r($_POST)
foo => array (
0 => a
1 => b
2 => c
bar => d
baz => array(
0 => e
1 => f
)
)
If you name your inputs properly, you can do that. Example:
<input type="text" name="textInput[]" />
<input type="text" name="textInput[]" />
That will populate an array in $_POST named textInput. That is:
$_POST['textInput'][0] == "whatever the first was set to be"
$_POST['textInput'][1] == "whatever the second was set to be"
Using square brackets after the input name will cause it to be grouped in PHP:
<input name="foo[]" type="text" value="1" />
<input name="foo[]" type="text" value="2" />
You can also make an associative array:
<input name="foo[bar]" type="text" />
I think multi-dimensional arrays would also work, but I'm not sure if I've actually tried it.
Edit: Here's the same thing answered in the PHP FAQ.
you can use your form fields like this:
<input type="text" name="form1['datax']"/>