How to fetch data from multiple multiselect dropdowns? - php

<form action="a.php" method="post">
<select id="sel_1" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
Now when i am trying to fecth the data like this
$offer = $_POST['sel'];
print_r($offer);
its displaying data like this:
Array
(
[0] => 1 // 1, 2 selected for sel_1
[1] => 2
[2] => 2 // 2, 3 selected for sel_2
[3] => 3
)
Shouldn't it come like this?
Array
(
[0] => Array(
[0] => 1
[2] => 2
)
[1] => Array(
[0] => 2
[2] => 3
)
)
I want to create string data like this(in the nxt a.php file):
for sel_1 data is created like "1, 2";
for sel_2 data is created like "2, 3";
How can i fetch the data in the above format.
I am trying this
for($i = 0; $i<count($offer) ; $i++)
{
for($j = 0; $j<count($offer[$i]); $j++)
{
$string = $tring. $offer[$i][$j];
}
}

Set the names sel1[] and sel2[] (different). In PHP you can use array_merge to obtain another array with the values from the first and from the second array:
$offer = array_merge($_POST['sel1'], $_POST['sel2']);
$string = '';
for($i = 0; $i < count($offer); $i++)
{
$string .= $offer[$i];
}

Try like this,it's working as per your requirement:
Instead of this
$offer = $_POST['sel'];
Put like this
$offer[] = $_POST['sel'];
Code:-
<form action="" method="POST">
<select id="sel_1" name="sel1[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel2[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
<?php
error_reporting(0);
$offer=array();
$offer[] = $_POST['sel1'];
$offer[]= $_POST['sel2'];
echo "<pre>";
print_r($offer);
echo "</pre>";
?>
For output click here: Output

Try this:
<form action="a.php" method="post">
<select id="sel_1" name="sel[1][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[2][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
Worked for me

Related

How to use index of loop in Post method PHP

I need your help! I am trying to save a variable in sql table using Php but I have problem. There are two questions in php, the first concern the continent and the second is depended from the continent. I want to use a loop to check which of the continents has been selected in the first question and then save the value of the second question. I hide the option of unchecked continent using some javascript code (I don't have problem).
The HTML code:
<form method="post" action="">
<fieldset><legend>Continents</legend>
<select id="q1" name="q1">
<option value="1">Africa</option>
<option value="2">Asia</option>
<option value="3">Australia</option>
<option value="4">America</option>
<option value="5">Europe</option>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
The Php code
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
for ($i = 1; $i <= 5; $i++) {
if($q1 == $i) {
$q2 = $_POST[$continents[$i-1]]
}
}
Your array should be from 1 to 5 instead of 0 to 4 as you have values 1 to 5 in q1.
Alternatively, I suggest that change your HTML structure to get the continent value in a single line without using loop. You need to change the values of continent like,
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
And to get the value of selected continent use $_POST[$_POST['q1']]. For egs, $_POST['q1']=Asia, then $_POST['Asia'] will return the Asia's choice,
$q2= $_POST[$_POST['q1']];
change
$continents =
array(1 => "Africa",
2 => "Asia",
3 => "Australia",
4 => "America",
5 => "Europe");
or
if(($q1-1) == $i) {
$q2 = $_POST[$continents[$i]]
}
First, you may change your select continent HTML:
<select id="q1" name="q1">
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="America">America</option>
<option value="Europe">Europe</option>
</select>
Then you could loop over your continents and get the answer:
$q1 = $_POST['q1'];
$continents = array("Africa","Asia", "Australia","America","Europe");
foreach($continents as $continent) {
if ($_POST['q1'] == $continent) {
$q2 = $_POST[$continent];
}
}
Now you have your answer to the second question in $q2.
How about this?
// Always try to seperate logic from your view
// Intialize data
$continents = array("Africa", "Asia", "Australia", "America", "Europe");
// Check for $_POST
if(isset($_POST["q1"])) {
foreach($continents as $id => $continent) {
if($_POST["q1"] == $id) {
// Do something special here
}
}
}
// Render HTML
<form method="post" action="">
<fieldset>
<legend>Continents</legend>
<select id="q1" name="q1">
<!-- Notice that I echo only variables not all of the html -->
<?php foreach($continents as $id => $continent) { ?>
<option value="<?php echo $id; ?>"><?php echo $continent; ?></option>
<?php } ?>
</select>
<select id="q2" name="Africa">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Asia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Australia">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="America">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
<select id="q2" name="Europe">
<option value="1">Choice 1</option>
<option value="2">Choice 2</option>
</select>
</fieldset>
Beautiful thing about my solution is that now you can create whatever array you want and your code will always work.
You can for example SELECT data from database and store them in $continents variable.
$query = "SELECT id, name FROM continents";
$result = mysql_query($query);
$continents = array();
while($row = mysql_fetch_assoc($result)) {
$continents[$row["id"]] = $row["name"];
}
Cool right? :)
I found the solution finally!!!
Replace
$q2 = $_POST[$continents[$i-1]]
with
$q2 = $_POST["{$continents[$i-1]}"];
That solution I wanted!!

php form using array

I want to create a form using array that user can select a number and echoes the number's corresponding object name after submit. I don't know why this code does not work, could someone please teach me how to do it the right way :( Thank you so much for your time.
<form name="train" method="GET" action="test.php">
<select name="object">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] = "pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
$train = $_GET['obejct'];
echo "<p>I have $train!</p>";
}
?>
Thank you so much!
Looks like you're setting $train to the value of whatever the form passes for the "object" select field, and then echoing that. You would expect then to see a number between 0 and 8, or the word "all" print out, but your reference of the object key has the word "object" misspelled as "obejct", so my guess is you're getting nothing to print as the value of $train.
Either way, what you really want to do is print the value at the key in the $train array that corresponds with what was provided by the user. This means that once you've created your array, which functions as a map, you must select the item from the array that you want to print.
You also need to handle the "all" case or you will get an error.
Here's how it would look if you continue using the array option:
<form name="train" method="GET" action="test.php">
<select name="object">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="all">Show All</option>
</select>
<input type="submit" name="submit" id="submit" value="submit" size="10">
</form>
<?php
$train[0] = "pencil";
$train[1] = "macaron";
$train[2] = "notes";
$train[3] = "book";
$train[4] = "eraser";
$train[5] = "cake";
$train[6] = "laptop";
$train[7] = "mint";
$train[8] = "cup";
if ($_GET['submit']) {
if ($_GET['object'] != 'all') {
//Handle the non-all case
$value = $train[$_GET['object']]; //This references a key in your array, like $train[0]
echo "<p>I have $value!</p>";
} else {
//Handle the all case here
}
}
?>

array in php function undefined

For training purposes i need to make a function which tells me the 'travel cost' between 2 cities. The book tells me to type this function:
<?php
function travelcost($start, $destination)
{
$travelcost = array();
$travelcost[1] = array();
$travelcost[2] = array();
$travelcost[3] = array();
$travelcost[4] = array();
$travelcost[1][1] = 0;
$travelcost[1][2] = 30;
$travelcost[1][3] = 60;
$travelcost[1][4] = 90;
echo($travelcost[$start][$destination] . " Euro's");
}
?>
In addition i've created this form to ask for a start and a destination:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Start: <select name="start" value="true">
<option value="start[]">Amsterdam</option>
<option value="start[]">Utrecht</option>
<option value="start[]">Den Haag</option>
<option value="start[]">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="destination[]">Amsterdam</option>
<option value="destination[]">Utrecht</option>
<option value="destination[]">Den Haag</option>
<option value="destination[]">Rotterdam</option>
</select>
<p><input type="submit" name="calculate" value="Calculate"</p>
</form>
Followed by:
<?php
if(isset($_POST["start"])&& isset($_POST["destination"]))
{
travelcost($_POST['start'], $_POST['destination']);
}
?>
This gives me Undefined index: start[]
I know im doing it wrong, but i just can't see the logic in the function and the array. I assume the function is correct because it's right out of the book but i'm also not sure about that.
Can someone help me out?
This is wrong,
<option value="start[]">Amsterdam</option>
^ ^
It should be
<option value="start">Amsterdam</option>
or
<option value="Amsterdam">Amsterdam</option>
Same for all options in start and destination.
According to your function `travelcost(), your select should be
Start: <select name="start" value="true">
<option value="1">Amsterdam</option>
<option value="1">Utrecht</option>
<option value="1">Den Haag</option>
<option value="1">Rotterdam</option>
</select>
Destination: <select name="destination" value="true">
<option value="1">Amsterdam</option>
<option value="2">Utrecht</option>
<option value="3">Den Haag</option>
<option value="4">Rotterdam</option>
</select>

Form submit 2 arrays

I am trying to make a form submit.
Form is select a task, then show several rows with checkbox and select with values 1-10
If checkbox is selected is need to add values from select into database.
this is PHP code
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] = 'POST'){
if(isset($_POST['submita'])){
foreach($_POST['nota'] as $key => $value)
if (isset($_POST['boxes'])){
foreach($_POST['boxes'] as $key => $value2){
if(isset($value2)){
$nota = htmlent($_POST['nota']);
$box = htmlent($_POST['boxes']);
$task = htmlent($_POST['task']);
$db->insert(array(
"task" => $task,
"nota" => $value,
"box" => $value2,
),
"erp_notes");
}
else
{
echo 'Select';
}
}
}
}
}
HTML
<input type="checkbox" name="boxes[]" value="<?=$row['id'];?>"></input>
<?=$row['name'];?>
<select name="nota[]" >
<option value="">Select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
I try many time but nothing.
In the very first if you have = when you mean ==.
You haven't posted the entire HTML so I'll have to assume there is a submita element there that's being set. Your best bet is to do var_dump($_POST) to see what you are actually getting, and then build your code to match.
Your code is incorrect because it loops over every boxes for each nota, when what I suspect you want is to just check the checkbox that corresponds to that element.
You probably want something more like this:
if (isset($_POST['nota']))
{
for ($i = 0; $i < count($_POST['nota']); ++$i)
{
if (isset($_POST['boxes'][$i]))
$db->insert(array('task' => $task, 'nota' => $_POST['nota'][$i], 'box' => $_POST['boxes'][$i], 'erp_notes');
}
}
i was edited cod , but work like in comment from first answer
PHP
if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['nota']))
{
for ($i = 0; $i < count($_POST['nota']); ++$i)
{
if (isset($_POST['boxes'][$i]))
$task = htmlent($_POST['task_id']);
$db->insert(array(
"nota" => $_POST['nota'][$i],
"date" => time(),
"box" => $_POST['boxes'][$i],
"task" => $task
),
"erp_note");
}
}
}
HTML
<form class="grid_12" action="" method="post" enctype="multipart/form-data">
<input type="checkbox" name="boxes[]" value="<?=$row['id'];?>"></input><?=$row['box_name'];?>
<select name="nota[]" multiple><option value="">select</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>
<input name="task" type="text" value="<?=$row['task_id'];?>" style="display:none;">
</form>

Array of multiple select inputs

I have this form :
<form>
<select name="data[][]" multiple="multiple">
<option>1</option>
<option selected="selected">2</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">3</option>
<option>4</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">5</option>
<option selected="selected">6</option>
</select>
</form>
I would like to get this :
$_POST['data'] = array(
0 => array(2),
1 => array(3),
2 => array(5,6)
);
I instead get this :
$_POST['data'] = array(
0 => array(2),
1 => array(3),
2 => array(5),
3 => array(6)
);
The solution would be to set the index : name="data[0][]" but i want it to be automatically done ..
Any ideas?
One way.Change to :
<select name="data[0][]" multiple="multiple">
...
<select name="data[1][]" multiple="multiple">
...
<select name="data[2][]" multiple="multiple">
Also see #CBroe comment.
You don't need to insert the keys manually.
I tested your code and it seems to be correct for what you intend to get or you expect.
I tested it like this;
<html>
<body>
<?php
if (!isset($_GET["submit"])) {
?>
<form method="get" action="yourfilename.php">
<select name="data[][]" multiple="multiple">
<option>1</option>
<option selected="selected">2</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">3</option>
<option>4</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">5</option>
<option selected="selected">6</option>
</select>
<input type="submit" value="submit" name="submit"/>
</form>
<?php
}
else {
$data= $_GET["data"];
print_r($data);
echo $show;
}
\\ So to get the 5 or 6 value in the array with key "2";
\\ Output: 5
$show= $data[2][0];
\\ Or
\\ Output: 6
$show= $data[2][1];
?>
</body>
</html>

Categories