When i try to submit this simple test form to PHP:
<form action="test.php" method="post">
<input name ="lang_learn[0]lang" type="text" value="1"><br>
<input name ="lang_learn[0]level" type="text" value="2"><br>
<input name ="lang_learn[1]lang" type="text" value="3"><br>
<input name ="lang_learn[1]level" type="text" value="4"><br>
<input type="submit">
</form>
i expect to have in the $_POST array something like this:
Array
(
[lang_learn] => Array
(
[0] => Array ([lang] => 1, [level] => 2)
[1] => Array ([lang] => 3, [level] => 4)
)
)
instead i get this:
Array
(
[lang_learn] => Array
(
[0] => 1
[1] => 4
)
)
i tried with different installations over different servers, and i always get the same result.
where is the problem? reading around this should be the right way to do that.
The names of the input fields need fixing:
<input name ="lang_learn[0][lang]" type="text" value="1"><br>
<input name ="lang_learn[0][level]" type="text" value="2"><br>
<input name ="lang_learn[1][lang]" type="text" value="3"><br>
<input name ="lang_learn[1][level]" type="text" value="4"><br>
You need to use sub-arrays, like you would in PHP. Each key should be surrounded with [ and ]. Try this...
<form action="test.php" method="post">
<input name ="lang_learn[0][lang]" type="text" value="1"><br>
<input name ="lang_learn[0][level]" type="text" value="2"><br>
<input name ="lang_learn[1][lang]" type="text" value="3"><br>
<input name ="lang_learn[1][level]" type="text" value="4"><br>
<input type="submit">
</form>
Try this,
<form action="test.php" method="post">
<input name ="lang_learn[0][lang]" type="text" value="1"><br>
<input name ="lang_learn[0][level]" type="text" value="2"><br>
<input name ="lang_learn[1][lang]" type="text" value="3"><br>
<input name ="lang_learn[1][level]" type="text" value="4"><br>
<input type="submit">
</form>
You probably need to do this:
<form action="test.php" method="post">
<input name ="lang_learn[0][lang]" type="text" value="1"><br>
<input name ="lang_learn[0][level]" type="text" value="2"><br>
<input name ="lang_learn[1][lang]" type="text" value="3"><br>
<input name ="lang_learn[1][level]" type="text" value="4"><br>
<input type="submit">
</form>
Your syntax is not correct: name ="lang_learn[0]lang" must be name ="lang_learn[0][lang]"
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 have text boxes with a default value. I want to put them in array output as shown below.
P.S: I need the expected array output (shown below) for my dynamic options in my online quiz program when creating questions
if(isset($_POST['btn_submit'])
{
//code here
}
<form method="post">
//question 1 array index [0]
<input type="text" name="option" value="1">
<input type="text" name="option" value="2">
<input type="text" name="option" value="3">
//question 2 array index [1]
<input type="text" name="option" value="1">
<input type="text" name="option" value="2">
<input type="submit" name="btn_submit">
</form>
EXPECTED ARRAY OUTPUT VALUES:
array (
[0] => 1,2,3
[1] => 1,2
)
EDIT:
It's possible to have the same name but still gets the expected array value? then, put them in one variable array
You need to use array in name attribute as:
<form method="post">
<input type="text" name="option_1[]" value="1">
<input type="text" name="option_1[]" value="2">
<input type="text" name="option_1[]" value="3">
<input type="text" name="option_2[]" value="1">
<input type="text" name="option_2[]" value="2">
</form>
PHP:
if(isset($_POST['btn_submit']))
{
$newArr[] = implode(',',$_POST['option_1']);
$newArr[] = implode(',',$_POST['option_2']);
echo "<pre>";
print_r($newArr);
}
Result:
Array
(
[0] => 1,2,3
[1] => 1,2
)
If you just want to use one single name option than use this:
<form method="post">
<input type="text" name="option[0][]" value="1">
<input type="text" name="option[0][]" value="2">
<input type="text" name="option[0][]" value="3">
<input type="text" name="option[1][]" value="1">
<input type="text" name="option[1][]" value="2">
<input type="submit" name="btn_submit">
</form>
PHP:
if(isset($_POST['btn_submit']))
{
$newArr[] = implode(',',$_POST['option'][0]);
$newArr[] = implode(',',$_POST['option'][1]);
echo "<pre>";
print_r($newArr);
}
Replace your code with following code and IT WILL WORK.
if(isset($_POST['btn_submit'])
{
//code here
}
<form method="post">
//question 1 array index [0]
<input type="text" name="option[0][]" value="1">
<input type="text" name="option[0][]" value="2">
<input type="text" name="option[0][]" value="3">
//question 2 array index [1]
<input type="text" name="option[1][]" value="1">
<input type="text" name="option[1][]" value="2">
<input type="submit" name="btn_submit">
</form>
You can simply use [] before the option name to make it as an array.
//question 1 array index [0]
<input type="text" name="option_1[]" value="1">
<input type="text" name="option_1[]" value="2">
<input type="text" name="option_1[]" value="3">
//question 2 array index [1]
<input type="text" name="option_2[]" value="1">
<input type="text" name="option_2[]" value="2">
So I have a form which is build up like:
<input name="website['menu']['id']" type="number" value="1">
<input type="text" value="#000000" name="website['color']['primary']['hex']">
Now i want to read out those values in php after they are submit.
I tried the following code, but when I var_dump it it gives me null.
if (isset($_POST['website'])) {
$result = $_POST['website'];
var_dump($result['menu']['id']);exit;
}
Remove the quote from field name
<input name="website[menu][id]" type="number" value="1">
<input type="text" value="#000000" name="website[color][primary][hex]">
And access the variable in php like:
echo $_POST['website']['menu']['id'];
Try this,
<?php
print_r($_POST['website']);
print_r($_POST['website']['menu']['id']);
?>
<form action="" method="POST"/>
<input name="website[menu][id]" type="number" value="1">
<input type="text" value="#000000" name="website['color']['primary']['hex']">
<input type="submit" value="submit"/>
</form>
I have a form:
<form>
<input type="text" name="aaa" id="aaa">
<input type="submit">
</form>
I have also jQuery script and can append this input.
Example:
Generating name.
<form>
<input type="text" name="aaa" >
<input type="text" name="aaa2" >
<input type="text" name="aaa3" >
<input type="submit">
</form>
Problems is MySQL insert:
How to insert from generated names?
I know $_POST['aaa'], but I don't know from generated names.
If you're generating dynamically text boxes from jQuery and you want to push them to mySQL, instead of creating them with names like 'aaa', 'aaa2' or 'aaa3' you can use square brackets like this:
<form method="POST">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<input type="text" name="field_name[]">
<button type="submit">Send</button>
</form>
That will allow you to use "field_name" as an array of strings.
<?php
$field_name = $_REQUEST['field_name'];
foreach($field_name as $value) {
$query = sprintf("INSERT INTO table SET field_name = '%s'", $value);
/* Execute Insert Query */
}
OK. if i have form like this:
<form method="POST">
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
and i apend 3 input's
<input type="text" name="aaa">
<input type="text" name="bbb">
<input type="text" name="ccc">
<button type="submit">Send</button>
</form>
in mysql i have table:
id
aaa
bbb
ccc
this is one insert
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.