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">
Related
I am trying to send a few input fields to my PHP file and loop them there so I need them as an array.
These are my input fields:
<input type="hidden" name="productencheckout[3314,99|1 pallet][kuub]" value="1 pallet">
<input type="hidden" name="productencheckout[3314,99|1 pallet][price]" value="31499">
<input type="hidden" name="productencheckout[3314,99|1 pallet][quantity]" value="1">
<input type="hidden" name="productencheckout[2][price]" value="27999">
<input type="hidden" name="productencheckout[2][quantity]" value="1">
<input type="hidden" name="productencheckout[70][price]" value="30999">
<input type="hidden" name="productencheckout[70][quantity]" value="1">
<input type="hidden" name="productencheckout[3624,99|2 pallets][kuub]" value="2 pallets">
<input type="hidden" name="productencheckout[3624,99|2 pallets][price]" value="62499">
<input type="hidden" name="productencheckout[3624,99|2 pallets][quantity]" value="1">
And it is posted like this:
productencheckout[3314,99|1 pallet][kuub]: 1 pallet
productencheckout[3314,99|1 pallet][price]: 31499
productencheckout[3314,99|1 pallet][quantity]: 1
productencheckout[2][price]: 27999
productencheckout[2][quantity]: 1
productencheckout[70][price]: 30999
productencheckout[70][quantity]: 1
productencheckout[3624,99|2 pallets][kuub]: 2 pallets
productencheckout[3624,99|2 pallets][price]: 62499
productencheckout[3624,99|2 pallets][quantity]: 1
So I tried the following:
echo $_POST['productencheckout']['3314,99|1 pallet']['kuub'];
$productarr = $_POST['productencheckout'];
echo '<pre>';
print_r($productarr);
echo '</pre>';
But it is all empty. Why?
I made a multiple choice with PHP (Codeigniter) and MySQL. I Got trouble when trying to retrieve value from the answer (with dynamic name) for each question. Here is the code for radio button :
<input type="radio" name="question_id (according to id of question)" value="answer_id">
So, if I have 3 random questions, the structure will be
<p>Question number 1 goes here</p>
<input type="radio" name="question_id1[]" value="1">
<input type="radio" name="question_id1[]" value="2">
<input type="radio" name="question_id1[]" value="3">
<input type="radio" name="question_id1[]" value="4">
<input type="radio" name="question_id1[]" value="5">
<p>Question number 6 goes here</p>
<input type="radio" name="question_id6[]" value="1">
<input type="radio" name="question_id6[]" value="2">
<input type="radio" name="question_id6[]" value="3">
<input type="radio" name="question_id6[]" value="4">
<input type="radio" name="question_id6[]" value="5">
<p>Question number 9 goes here</p>
<input type="radio" name="question_id9[]" value="1">
<input type="radio" name="question_id9[]" value="2">
<input type="radio" name="question_id9[]" value="3">
<input type="radio" name="question_id9[]" value="4">
<input type="radio" name="question_id9[]" value="5">
How to retrieve the answer that relates to the question ? For example put it in Array like :
array p = ['id_question' => 21, 'id_answer'=4]
The radio button structure
Try with
<p>Question number 9 goes here</p>
<input type="radio" name="question_id[9]" value="1">
<input type="radio" name="question_id[9]" value="2">
<input type="radio" name="question_id[9]" value="3">
<input type="radio" name="question_id[9]" value="4">
<input type="radio" name="question_id[9]" value="5">
<p>Question number 6 goes here</p>
<input type="radio" name="question_id[6]" value="1">
<input type="radio" name="question_id[6]" value="2">
<input type="radio" name="question_id[6]" value="3">
<input type="radio" name="question_id[6]" value="4">
<input type="radio" name="question_id[6]" value="5">
And in server side
<?php
//assuming form method is post
$questions_array = array();
foreach($_POST[question_id] as $key=>$answer)
{
$questions_array[] = array('id_question' => $key, 'id_answer'= $answer);
}
print_r($questions_array);
?>
Assuming that user can select a single radio button for single question....
I have placed the code in the form and with a submit button i submitted it. Then the output i got it is
Array
(
[question_id1] => Array
(
[0] => 3
)
[question_id6] => Array
(
[0] => 2
)
[question_id9] => Array
(
[0] => 3
)
[submit] => submit
)
So we can access with the same name. I did it with normal php code. Because the array will that is formed will be same in both core php as well as codeigniter. So we can normally access it with for loop.
I have the following code, I want the user to be able to sign up to multiple lists at the same time. It is currently only signing up emails to one list randomly, even if I check all the lists. Is it possible to do? Maybe some sort of php echo?
<form action="" method="post">
<input name="accName" type="hidden" value="companyname">
<input name="listName" type="hidden" value="">
<input name="fullEmailValidationInd" type="hidden" value="Y">
<input name="doubleOptin" type="hidden" value="false">
<input name="successUrl" type="hidden" value="">
<input name="errorUrl" type="hidden" value="">
Email Address <input class="border" name="email" size='50' type="text" value="">
First Name <input class="border" name="First_Name" size='50' type="text" value="">
Last Name <input class="border" name="Last_Name" size='50' type="text" value="">
<label><input name="listName" type="checkbox" value="list1"></label>
<label><input name="listName" type="checkbox" value="list2"></label>
<label><input name="listName" type="checkbox" value="list3"></label>
<label><input name="listName" type="checkbox" value="list4"></label>
<label><input name="listName" type="checkbox" value="list5"></label>
<input type="submit" value="OK">
</form>
Use array for the checkboxes:
<input type="checkbox" NAME="listName[]" VALUE="list1" />
Or use different names...
You will need to grab this using, php side, a loop:
foreach ($_POST['listName'] as $selected)
Just notice that if none are selected this will fail, thus check if the array exists before:
if (isset($_POST['listName'])
{
foreach ($_POST['listName'] as selected)
{
DO YOUR STUFF
}
}
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]"
I have multiple checkbox and want to pass their value on next page when i checked them.
My code is
<form method="POST">
<input type="checkbox" name="city" id="city" value="Gold" />
<input type="checkbox" name="city" id="city" value="Platinum" />
<input type="checkbox" name="city" id="city" value="Silver" />
<input type="submit" value="Submit" />
</form>
By defining the name attribute as an array name="city[]"
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
print_r($_POST);
/*
Array
(
[city] => Array
(
[0] => Gold
[1] => Platinum
[2] => Silver
)
)
*/
}
?>
<form method="POST">
<input type="checkbox" name="city[]" value="Gold" />
<input type="checkbox" name="city[]" value="Platinum" />
<input type="checkbox" name="city[]" value="Silver" />
<input type="submit" value="Submit" />
</form>
Try this.
<?php
include("config.php");
if($_POST) {
$city = $_POST['card'];
header("location:target_page.php?card=".$city);
}
?>
<form method="POST">
<input type="checkbox" name="card[]" id="card" value="Gold" />
<input type="checkbox" name="card[]" id="card" value="Platinum" />
<input type="checkbox" name="card[]" id="card" value="Silver" />
<input type="submit" value="Submit" />
</form>
This is not working when both fields have the same name.
<form method="POST">
<input type="checkbox" name="city" id="city" value="Gold" />
<input type="checkbox" name="cities[]" id="city" value="Platinum" />
<input type="checkbox" name="cities[]" id="city2" value="Silver" />
<input type="submit" value="Submit" />
</form>
When you write the fieldname like cities[] then you get an array in your PHP-Script.
forearch($_POST['cities'] as $city) {
var_dump($city);
}
And an ID should be unique.