I'm not 100% sure of the correct terminology of what I'm even asking, but I currently have a form that is being posted that has an array of elements:
<form>
<input type="text" name="album_songs[0][title]" value="Let It Go" />
<input type="text" name="album_songs[0][writer]" value="Robert Lopez, Kristen Anderson-Lopez" />
<input type="text" name="album_songs[0][composer]" value="Frozen" />
<input type="text" name="album_songs[0][length]" value="3:44" />
<input type="text" name="album_songs[0][genre]" value="Soundtrack, Music, Animated" />
<input type="text" name="album_songs[0][songorder]" value="5" />
<input type="text" name="album_songs[1][title]" value="Love Is An Open Door" />
<input type="text" name="album_songs[1][writer]" value="Robert Lopez" />
<input type="text" name="album_songs[1][composer]" value="Frozen" />
<input type="text" name="album_songs[1][length]" value="2:07" />
<input type="text" name="album_songs[1][genre]" value="Soundtrack, Music, Animated" />
<input type="text" name="album_songs[1][songorder]" value="4" />
</form>
And in my code, I can access these elements like the following:
$songs = $_POST["album_songs"];
foreach($songs as $song) {
$title = $song["title"];
$writer = $song["writer"]; // turn into array??
$composer = $song["composer"];
$length = $song["length"];
$genre = $song["genre"]; // turn into array??
$songorder = $song["songorder"];
}
What I would like to have happen, is not only have an array of songs, but also an array of Writers and Genres. Is this even possible? I tried mocking up something like the following but it did not work:
<input type="text" name="album_songs[4][genre[0]]" value="Soundtrack" />
<input type="text" name="album_songs[4][genre[1]]" value="Music" />
<input type="text" name="album_songs[4][genre[2]]" value="Animated" />
Any suggestions or is it even possible?
I might consider a dropdown, but you almost had it:
<input type="text" name="album_songs[4][genre][]" value="Soundtrack" />
<input type="text" name="album_songs[4][genre][]" value="Music" />
<input type="text" name="album_songs[4][genre][]" value="Animated" />
Gives this:
Array
(
[4] => Array
(
[genre] => Array
(
[0] => Soundtrack
[1] => Music
[2] => Animated
)
)
)
Try to use square bracket syntax to convert your form inputs into an array.
I.E. :
<input type="text" name="album_songs_title[]" value="Title1" />
<input type="text" name="album_songs_title[]" value="Title2" />
Then, you will be able to browse your datas with a foreach().
I think it might work.
Related
am trying to create array with key by using html input
here's my html input
<form method="post" action="">
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<button type="submit">go</button>
</form>
my outbut is
Array ( [0] => Array ( [you] => jhon ) [1] => Array ( [he] => joy ) [2] => Array ( [she] => sarah ) [3] => Array ( [you] => samm ) [4] => Array ( [he] => petter ) [5] => Array ( [she] => susan ) )
but i want the array to be like this
Array( [0]=> array ( [you] => jhon [he] => joy [she] => sarah )[1]=> array ( [you] => pitter [he] => tom [she] => suszan ) )
is there away to do that
try like this ==>
<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>
OR
<form method="post" action="">
<?php $n = 2; // how many interval you want
for ($i = 0; $i < $n; $i++) {
?>
<input type="text" name="name[<?php echo $i; ?>][you]" value="" />
<input type="text" name="name[<?php echo $i; ?>][he]" value="" />
<input type="text" name="name[<?php echo $i; ?>][she]" value="" />
<?php } ?>
</form>
If you want to output an array with two children then set the keys manually, name[0] for the first three inputs and name[1] for the last three.
Everytime you write name="[][key]" php increment automatic the key.
if you write syntax like [] php increment the index of array.
Small explation
For example:
If you write an array like this
$array[] = "msg1";
$array[] = "msg2";
$array[] = "msg3";
$array length will be 2 (3 elements because it starts from 0) and it is same as
$array[0] = "msg1";
$array[1] = "msg2";
$array[2] = "msg3";
This is different from above
$array[0] = "msg1";
$array[1] = "msg2";
$array[1] = "msg3";
This array will have only 1 length (2 elements only)
Solution of your question is :
<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>
I hope I ask this question correctly, and if not please direct me how to repair it. I have had it deleted as a post once already...
My goal is to submit a form with one drop down, with numbers like 100, 200, 300 (for how many T-shirts you want to order)... Then depending on what is selected from the drop down have a series of text boxes (for number placement) that must add up to the selected number of shirts you want to order from the dropdown.
My idea is to capture all these text fields in an array, and send them off to a function to be added...
Can someone assist me please?
Here is the form code I know does not work, but I want it to work...
<form>
<label>
<input type="checkbox" name="PoloDesign" value="100" id="PoloDesign_0" />
100</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="200" id="PoloDesign_1" />
200</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="300" id="PoloDesign_2" />
300</label>
<br />
<input type="text" name="name[1]" id="name1" value="{$name1}"/>
<input type="text" name="name[1]" id="name2" value="{$name2}"/>
<input type="text" name="name[1]" id="name3" value="{$name3}"/>
<input type="text" name="name[1]" id="name4" value="{$name4}"/>
<input type="text" name="name[1]" id="name5" value="{$name5}"/>
<input type="text" name="name[1]" id="name6" value="{$name6}"/>
<input type="text" name="name[1]" id="name7" value="{$name7}"/>
<input type="submit" value="submit"/>
</form>
Just change each
name="name[1]"
To
name="name[]"
Then the fields are posted as an array you can iterate through in PHP
if (is_array($_POST['name']):
foreach ($_POST['name'] as $key=>$field):
// do something here
$yourKey = $key +1;
$yourValue = $field;
I have changed your code a little and tried to make it work using regular expression:
<?php
$name_array = preg_grep('/name[1-9]*/', $_GET);
?>
So, basically it checks all submitted variables and creates array from all variables that have name at start and a number at end. So, the form part should change to look like this:
<input type="text" name="name1" id="name1" value="{$name1}"/>
<input type="text" name="name2" id="name2" value="{$name2}"/>
<input type="text" name="name3" id="name3" value="{$name3}"/>
<input type="text" name="name4" id="name4" value="{$name4}"/>
<input type="text" name="name5" id="name5" value="{$name5}"/>
<input type="text" name="name6" id="name6" value="{$name6}"/>
<input type="text" name="name7" id="name7" value="{$name7}"/>
I tested on Apache2 and PHP 5.3
It is easy to add more fields to a html form by jQuery. Then, we can serialize the fields, if the have the same name but what if we have a set of fields? For example
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
Now I want to add a new set of fields by jQuery as
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
and so forth. I process the form with PHP to insert movies into mysql database with three columns of (name, director, year). In the above-mentioned example, it is hard to serialize the fields to create appropriate $_POST arrays. How should I serialize jquery-added sets of movies?
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
Nothing else. On the server you will get (in case of POST) array in $_POST['movie_name'], $_POST['movie_director'] and $_POST['movie_year'];. Elements with the same index are from the same set of inputs.
What kind of problem with serialization do you have?
<form>
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<hr />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<br />
<input type='button' id='serialize' value='Click me' />
</form>
and js code:
$('#serialize').click(function(){
alert($('form').serialize());
});
when you want to submit the data just write
$.post('script.php', $('form').serialize(), function() {alert('Saved');});
ps: if you are afraid to lose something, just compare count($_POST['movie_name']), count($_POST['movie_director']) and count($_POST['movie_year']).
or you can add indexes
<input type="text" name="movie_name[0]" />
<input type="text" name="movie_director[0]" />
<input type="text" name="movie_year[0]" />
<input type="text" name="movie_name[1]" />
<input type="text" name="movie_director[1]" />
<input type="text" name="movie_year[1]" />
Based on useful discussion with Cheery, I came to conclusion that the best and safest way is to use
<input type="text" name="movie_name[i]" />
<input type="text" name="movie_director[i]" />
<input type="text" name="movie_year[i]" />
where we define each i with jQuery to serialize the fields SAFELY. This way, we can be sure that serialized arrays are parallel and well matched, without misplacing.
You can do something like this:
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
// OTHER:
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
And do this to all... In Jquery, you create a function that create field as needed... I'm not the best in JQuery so I can't help you for this but the way I told you worked fine for me with PHP...
If I have this code:
<input type="hidden" name="hello" value="hey" />
<input type="text" name="one" />
And I'll write something, for example: "hey", it will show:
domain.com/index.php?hello=hey&one=hey
But, I want to get more element from specific input. For example:
This is my full code:
For e
<input type="hidden" name="hello" value="hey" />
<input type="text" name="one" />
<input type="text" name="two" />
If I write on "one", it will show:
domain.com/index.php?hello=hey&element=sometext&one=sometext
If I write on "two", it will show: domain.com/index.php?hello=hey&another_element=sometext&two=sometext
How can I do that?
Sorry for my english.
Just use the "GET" method :
<form method="get">
<input type="hidden" name="hello" value="hey" />
<input type="text" name="one" value="1" />
<input type="text" name="two" value="2" />
</form>
It will create the query string ?hello=hey&one=1&two=2
Ugh! Any ideas?
Have a form which tracks user activity for given time intervals. Each time interval can have inputs for "location" & multiple "activities". So the form has a variable number of time intervals and a variable number of activities per interval.
How can I set this up in HTML for smooth processing PHP? EDIT: Additional intervals and activities can be added via jQuery.
My initial thought is convoluted so anyone have a better idea?
<!--Interval 1 with 4 activities-->
<input type="text" name="interval[]" />
<input type="text" name="location[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
<!--Interval 2 with 2 activities-->
<input type="text" name="interval[]" />
<input type="text" name="location[]" />
<input type="text" name="activity[]" />
<input type="text" name="activity[]" />
This is a nightmare on the server side.
You may need to isolate activities per location / interval. A solution would be to use an index for each interval, this involve to change the way you create the new html elements for each interval.
So the resulting code would be :
<!--Interval 1 with 4 activities-->
<input type="text" name="interval[1][]" />
<input type="text" name="location[1][]" />
<input type="text" name="activity[1][]" />
<input type="text" name="activity[1][]" />
<input type="text" name="activity[1][]" />
<input type="text" name="activity[1][]" />
<!--Interval 2 with 2 activities-->
<input type="text" name="interval[2][]" />
<input type="text" name="location[2][]" />
<input type="text" name="activity[2][]" />
<input type="text" name="activity[2][]" />
But it's not as smooth to manipulate as :
<!--Interval 1 with 4 activities-->
<input type="text" name="interval[1][interval]" value="interval id" /> // optional
<input type="text" name="interval[1][location]" value="location" />
<input type="text" name="interval[1][activity][]" value="activity" />
<input type="text" name="interval[1][activity][]" value="activity" />
<input type="text" name="interval[1][activity][]" value="activity" />
<input type="text" name="interval[1][activity][]" value="activity" />
<!--Interval 2 with 2 activities-->
<input type="text" name="interval[2][interval]" value="interval id" /> // optional
<input type="text" name="interval[2][location]" value="location" />
<input type="text" name="interval[2][activity][]" value="activity" />
<input type="text" name="interval[2][activity][]" value="activity" />
Which will give you a nice tree ( in the form of arrays of arrays ) to iterate over.
In order to do that, you need a javascript function that keep a track of the current interval index ans use that index to order the array of intervals.
Server side all you have to do is ( in case of POST method ) :
foreach ( $_POST['interval'] as $k => $interval ) {
echo $interval['interval'];
echo $interval['location'];
foreach ( $interval['activity'] as $id => $activity )
echo $activity;
}