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}";
}
Related
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
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.
I want to save list option values into MySQL then again want to display them as a list.
I need that for a dynamic query.
<select multiple id="to" size="15" name="to[]">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
This is php code to change it into (a,b,c,d):
$joinedString = array();
$var1 = $_POST['to'];
$joinedString = implode(',', $var1);
$joinedString;
I just change this into a,b,c,d with implode function, and its working fine.
But don't know how to save it in mysql and how to again fetch data from database and make this as:
<select multiple id="to" size="15" name="to[]">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
Now that you have the string a,b,c,d in variable $joinedString, Just use it to insert into your database. Remember you need to insert this string like this 'a,b,c,d' and not simply a,b,c,d. So, first format your string in inverted commas like 'a,b,c,d'. Now, that you have done it, just use insert command.
Insert into Table_name set column_name='".$joinedString."' where some_condition.
After that, you retrieve it if you have to use it again, using "Select" query.
Select column_name from Table_name where Some_condition
It would give you your variable back to you in format, 'a,b,c,d'. Save it in some variable $variable.
Now you use explode function to make an array of this string using comma.
$arr_string = explode(",",$variable);
This $arr_string would be an array like this
array(0=> a,
1=> b,
2=> c,
3=> d)
Now you loop over this array using foreach and make the select option.
<select multiple id="to" size="15" name="to[]">
<?php
foreach($variable as $key=>$val)
{
echo "<option value='".$val."'>".$val."</option>";
}
?>
</select>
If you are storing comma separated value within database you can get the value from database using query and from result you can simply implement that within your file using explode function of PHP as :
<?php
$joinedString = 'a,b,c,d'; // fetched data from database using query if you are storing data within database comma separated
$data = explode(',',$joinedString);
?>
<select multiple id="to" size="15" name="to[]">
<?php foreach($data as $key => $value){?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
I think this might be the expected answer..
I want to store 4 values of this dropdownlist into an array using PHP, and I also want to separate them by comma and save them into different single variable.
<td>
<select name="Ty" size=4 multiple>
<option value="Action">Action</option>
<option value="Adventure">Adventure</option>
<option value="Animation">Animation</option>
<option value="Bollywood">Bollywood</option>
<option value="Marathi">Marathi</option>
<option value="Comedy">Comedy</option>
<option value="crime">Crime</option>
<option value="Documentary">Documentary</option>
<option value="Drama">Drama</option>
<option value="Family">Family</option>
<option value="Horror">Horror</option>
<option value="Romance">Romance</option>
<option value="Sci">Sci-Fi</option>
</select>
</td>
Putting values into an array
Replace
<select name="Ty" size=4 multiple>
With
<select name="Ty[]" size=4 multiple>
Separate the values by a comma
In the page the form points to, insert this code
<?php
$count = count($_POST['Ty']); //number of elements in the array
for($i=0;$i<$count;$i++){
$allvalues .= $_POST['Ty'][$i];
$minus = $count-1;
if($i<$minus){$allvalues .= ',';} //prevents to add a comma also to the last element of the array
}
echo $allvalues;
?>
Saving values into different variables
Not sure what you're exactly meaning with this. If I understood well, you could use something like this
$var1 = $_POST['Ty'][1];
If $_POST['Ty'] is an array you can convert it into a single comma separated string like this:
$comma_separated = implode(',',$_POST['Ty']);
I have this form:
<form name="summaryform">
<select name="category" id="category" style="width:500px;">
<option value="">Choose category...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
</select><br>
<select name="subcategory" id="subcategory" style="width:500px;">
<option value="">Choose sub category...</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
<option value="Scheme">Scheme</option>
</select><br>
<input type="text" name="comments" id="comments" value=" Enter request comments..." onfocus="clearText(this)" onblur="restoreText(this)" style="width:493px;color:#999;font-size:9pt;height:20px;">
On the form above, I have two dropdowns (name="categoryId" and "subcategoryid") and one textboxt (name="comments")
How do I pass the values from this form to another form?
Let's say below is the form I want to pass the values to:
<div>
<p>
<form name="summaryform" method='POST' action='process.php'>
<div style="font-size:12pt;">
value from one dropdown
value from the other dropdown
value from textbox
</form>
</p>
</div>
jQuery Method
If the forms are on the same page, you can use jQuery to get the values and write to the div:
var content = $('#category').val()+'<br>'+$('#subcategory').val()+'<br>'+$('#comments').val();
$('#div-to-write-to').html(content);
PHP Method
If the forms are on different pages, you will need to use PHP and the $_POST[] variable to pass the values you are looking for.
NOTE: for this method to work the page being POSTed to MUST be PHP. You will also need to add an action and method in the first form to POST to the second form.
This would be the code in the form on the page being POSTed to:
<?php echo $_POST['category'].'<br>'.$_POST['subcategory'].'<br>'.$_POST['comments']; ?>
A value can be passed between forms using the POST and GET methods. The first form (Which is sending the values) does not have this defined, so modify the first form to this -
<form name="summaryform" method='POST' action='process.php'>
Then, in process.php, put the following php code -
$dropdown_first = $_POST['category'];
$dropdown_second= $_POST['subcategory'];
$comment = $_POST['comments'];
After that, you can just use the variables as you see fit!
If you want just print the selected values from first form #zsaa14's
answer is good
If you want to print whole select list, autoselected your value, heres a code.
foreach($categories as $category) {
$selected = $category['name'] === $_POST['category'] ? 'selected="selected"' : '';
echo " <option value="{$category['name']}" {$selected}>{$category['name']}</option>" ;
}
NOTE: the variable names and keys are for example, use Yours. This is only fragment of code.
You can use post data fields to send the values to the next form