How to insert values of multiple select - php

I have a list multiple select, this is the code :
<form method="post" action="">
<select name=thematique[] multiple>
<option value="1"> MIE </option>
<option value="2"> Migration </option>
<option value="3"> Réunification familiale </option>
</select>
<button type="submit" class="button" name="categoriser">Catégoriser</button>
</form>
My problem is if I select MIE + Migration + Réunification familiale, so I have 3 lines in my data base for the same ID.
I want to have one line which will insert all options of the select (0 1 2 or 3).
$id_struct=$_POST['id_struct'];
$thematique=$_POST['thematique']);
foreach($thematique as $item) {
$string= $item.' ';
echo $string;
$bdd->exec("INSERT INTO categorisation SET id_thematique=$string
WHERE id_struct=$id_struct");
}
PS : id_thematique is a foreign key.

1)
You need to add VALUE attribute in your select box .
2) Don't treated INSERT query AS UPDATE query
Your select box
<select name=thematique[] multiple>
<option value="MIE"> MIE </option>
<option value="Migration"> Migration </option>
<option value="Reunification_familiale"> Reunification familiale </option>
</select>
Your php file for insert into three separate field
<?php
foreach ($_POST['thematique'] as $thematique)
{
///your insert code//
$bdd->exec("INSERT INTO categorisation (`thematique`) VALUES ('".$thematique."'");
}?>

You can use array of you thematique as object array:
$thematiqueArray = array(0=>'MIE', 1=>'Migration', 2=>'Réunification familiale');
set your html options element like this:
<option value=0>MIE</option>
<option value=1>Migration</option>
<option value=2>Réunification familiale</option>
and on your POST catching similiar like this:
$thematique=$_POST['thematique']);
foreach($thematique as $key => $value) {
$item = $thematiqueArray[$key];
echo $item;
}

One solution would be to give base2 values to your select. Like 1,2,4,8 etc... then you can sum the selected values up, and know which are selected. But this only works if you don't need the values in other places.

Related

Submit Array type input file in database

I have a form, which has two fields: "product_name" and "product_q" and in my form i have an option to increase the number of fields in product_name and product_q
<select name="product_name[]" id="product_name">
<option value="please select"></option>
<option value="1">demo1</option>
<option value="2">demo2</option>
</select>
<select name="product_q[]" id="product_q">
<option value="please select"></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
and an action page like
public function order(){
$Product_q = $this->input->post('quantity');
$sponserid = $this->session->userdata('number');
$pname['product_name'] = $this->input->post('product_name');
foreach($Product_q as $key => $value){
$data['Product_q'] = $value;
$data['Product_Name'] = $pname['product_name'][$key];
$this->mymodel->insert_items($data);
}
and my model
function insert_items($data){
$this->db->insert("lucky_order", $data);
return;
}
Please help to insert data database my form look like this:
FORM with one field
Form with two field
We cannot insert array directly into database so before inserting it to database we need to encode array into json. You can do it by following <?php json_encode($data); ?>
and then later where you want fetch this data then you can simply decode that data back to array by using following method <?php json_decode($data); ?>
you can try this :
<select name="product_name[]" class="product_name">
<option value="please select"></option>
<option value="1">demo1</option>
<option value="2">demo2</option>
</select>
<select name="product_q[]" class="product_q">
<option value="please select"></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="product_count" value="1" />
You have to increment /decrement the product_count value on addition
or removal or product
for controller :
loop your function in model for inserting into database
for times of product_count
and the values for each using $key
something like this :
for ($i=0; $i<$_POST['product_count']; $i++){
call the function of insert by passing the each value using above $i variable.
like
$_POST['product_name'][$i];
this will get you your first product name similarly for all
and you can call the model function and pass the above values each
time with incremented value of $i

Value of Multiple select input text field in CodeIgniter controller

How can I get the value of a multiple select input field in CodeIgniter controller?
I want a multiple select input field shown here
I have added below html code.
<select class="js-example-basic-multiple multiple_selection form-control" name="student[]" multiple="multiple" >
<option value="1">Student1</option>
<option value="2">Student2</option>
<option value="3">Student3</option>
<option value="4">Student4</option>
<option value="5">Student5</option>
<option value="6">Student6</option>
</select>
I am using given code to fetch the value of this input field. But I didn't get the value.
$studentname = array();
$studentname =$this->input->post('student[]');
echo 'studentName:'.$studentname;
Do you have any suggestions?
Your values will be posted in an array:
$studentNames = $this->input->post('student');
You can then access each value using loop:
foreach($studentNames as $name){
echo "Student name is $name";
}
there is no need to use [] while you are echoing post value, you already define in name='student[]';
$studentname =$this->input->post('student');
echo "<pre>";
print_r($studentname);
echo "</pre>";
for naming sake make it student students since its a multislect
<select class="js-example-basic-multiple multiple_selection form-control" name="students[]" multiple="multiple" >
<option value="1">Student1</option>
<option value="2">Student2</option>
<option value="3">Student3</option>
<option value="4">Student4</option>
<option value="5">Student5</option>
<option value="6">Student6</option>
</select>
in controller:
//get the post values of students (if you dump the post you will see that $_POST['students'] is an array
$students = $this->input->post('students');
https://www.codeigniter.com/user_guide/libraries/input.html
//will contain the values of the selected students ex. var_dump($students) will produce [1,2,4];
//you will need to loop over students since it is an array
foreach($students as $id){
echo "Student id is {$id}";
}

How to get all values of multiple select box through POST?

I need to get all(Selected and unselected) the values of a multiple select box through POST. How can I do the trick?
Create a select tag like this
<select name="data[]" multiple="multiple" >
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
You can get the selected values:
foreach ($_GET['data'] as $selected) {
echo $selected."\n";
}
You can not get the unselected values.

How can I POST a form where name field is generated from a loop?

I'm so close!!
I can't figure out how to post these values. Here's what I've got:
<form action="process.php" method="post">
<?php
foreach (array_combine($UndefinedEvents, $EventDates) as $event=>$dates){
echo "This Event does not have a Timeline associated with it: " .$event . " on ".$dates. '<br>';
echo "Choose a Timeline:<br>";
?>
<?php echo "<select name=".$EventID[$i].">"; ?>
<option selected = "selected"></option>
<?php foreach (array_combine($TimelineID, $UserTimelines) as $temptimelineID=>$timeline){
echo "<option value=".$temptimelineID."> ".$timeline. "</option>";
}
echo " </select><br><br>";
$i = $i+1;
}
?>
<input type="submit" />
</form>
There's a lot happening above, but the markup is giving me what I want. Here's an example of what the above looks like:
<form action="processGoogle.php" method="post">
This Event does not have a Timeline associated with it:
First Event on 2011-07-01 00:00:00
<br>
Choose a Timeline:
<br>
<select name=3576> //THIS is $eventID
<option selected = "selected"> </option>
<option value=257> Timeline One </option>
<option value=258> Timeline Two </option>
<option value=259> Timeline Three </option>
</select>
<br>
<br>
This Event does not have a Timeline associated with it:
Next Event on 2011-06-30 00:00:00
<br>
Choose a Timeline:
<br>
<select name=3573>//THIS is $eventID
<option selected = "selected"> </option>
<option value=257> Timeline One </option>
<option value=258> Timeline Two </option>
<option value=259> Timeline Three </option>
</select>
<br>
<br>
...
<input type="submit">
</form>
What I need to do is post the value for $EventID and $temptimelineID.
What do I need to do to pass that information in post, and what do I need to have in my process.php form to read it?
Thanks for any help!
Looks like you have variable $_POST. Try
$x = array_keys($_POST);
foreach($x as $y) {
echo $y ." = ". $_POST[$y]."<br/>";
}
To just see the values of your $_POST superglobal, it would be easier to simply write:
print "<pre>"; var_dump($_POST); print "</pre>";
That said, there are a couple of ways that you can pass which fields are machine generated.
Option 1) Give the fields a unique prefix, such as:
<select name="timeline_3576"> //THIS is $eventID
Then one simply has to look through the $_POST values for the appropriate prefix, explode the string on the underscore and take the second value. Bob's your uncle.
Option 2) Build an array and pass that in a hidden field.
<select name="3576"> //code snipped
<select name="9999"> //code snipped
<input type="hidden" name="timelines" value="3576,9999">

Multiple PHP dropdowns and getting the POSTed value from the array

I have , within a foreach , a dropdown:
`<select name="position[]">
<option value="1st">First</option>
<option value="2nd">Second</option>
<option value="3rd">Third</option>
</select>`
I need to be able to get the values from position[]when the form is posted
I had assumed it was $_POST['position'][0] , $_POST['position'][1] etc.
But that doesn't work .
Try this:
<?php
foreach($array as $key=>$value){ ?>
<select name="position[<?php echo $key; ?>]">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
<?php } ?>
You should then be able to access each select like this:
$_POST['position'][$key]
You have not included multiple in html code of select.
You should use
<select name="name[]" multiple size"5">
Try this:
$test=$_POST['position'];
if ($test){
foreach ($test as $t){
echo 'You selected '.$t.'<br />';
}
}
and also in select tag enable multiple selection by:
<select name="position[]" multiple="multiple">

Categories