Codeigniter get selected values of form_multiselect - php

i'm working on a codeigniter website and i need to use form_multiselect the select shows fine but when i post it and i try to get the selected value on the controller when i do :
$category = $this->input->get_post('category');
var_dump($category);
i get string(0) "".
here is my codes :
View
<?php echo form_multiselect('category[]', $categories); ?>
Controller
$data['categories'] = $this->blog_category_model->getblogcatDropDown($app_key);
$category = $this->input->get_post('category');
var_dump($category);
die;
Model
public function getblogcatDropDown($app_key)
{
$query = $this->db->query('SELECT * FROM `webapp_blog_categories` WHERE `app_key`=('.$this->db->escape($app_key).') ORDER BY `id` ASC')->result();
$return[''] = 'Choose blog category';
foreach ($query as $row) {
$return[$row->id] = $row->cat_name;
}
return $return;
}
update
when i use html select on the view :
<select multiple name="tst[]">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
it works fine , but the generated form not working
can any one help me ? thanks

Make sure the form method="post" and simply check the whether the value is posted when you submit the form using developer tools.
for getting the value in controller. it's simply.
$category = $this->input->post('category');

Related

Select specific <option> from <select> while having a value from PHP

I have the following HTML code for my element and it is populated with elements.
<select name="category" class = "form-control">
<option value = "Awearness">Awearness</option>
<option value = "ITandTechnology">IT and Technology</option>
<option value = "Physics">Physics</option>
<option value = "Sport">Sport</option>
<option value = "Science">Science</option>
<option value = "Social">Social</option>
</select>
I also have PHP code above tag that gets me value of category from MySQL database. So for example:
...$category = "Sport";...
What I need to know is, is there a way to select certain option when page loads that corresponds to $category variable I got in PHP?
So if
...$category = "Sport";...
it selects the Sport option from tags.
To set an option as selected on page load we can use the selected attribute.
<option value="some-value" <?= $category == 'some-value' ? 'selected' : '' >>....</option>
Then do the same for each option in your list.
I suggest using JavaScript along with your php.
For example,
<?php
$cat = 'Sport';
?>
<!DOCTYPE html>
<html>
<body>
<select name="category" class = "form-control" id = "i1">
<option value = "Awearness">Awearness</option>
<option value = "ITandTechnology">IT and Technology</option>
<option value = "Physics">Physics</option>
<option value = "Sport">Sport</option>
<option value = "Science">Science</option>
<option value = "Social">Social</option>
</select>
<script>
var a= document.getElementById('i1');
a.getElementsByTagName('option');
for(i=0;i<a.length;i++)
{
if(a[i].value=='<?php echo $cat;?>')
{
a[i].setAttribute("selected","selected");
break;
}
}
</script>
</body>
</html>

Post drop down select value in HTML form

I am trying to get drop down select value posted upon send.
Markup
<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
php
$selectOption = $_POST['taskOption'];
{
$message.=$value.'<br>';
}
$message.='
What am I doing wrong?
Is this what you're trying to do?
if(!empty($_POST['taskOption']))
{
$value = $_POST['taskOption'];
$message.=$value.'<br>';
}
you need to use post in your form, method="post"
if(isset($_POST['submit'])){
$dropdwon = $_POST['taskOption'];
//code here
}

How to add a 'selected' in a select dropdown wp/php

I have a problem which I'm not sure how to approach, I have a WP plugin which has a form, in the form I have a with result that could have varied amount of data (depending on what the user submits).
How can I add 'selected' to the selected item so when the user returns he can edit/view the item he selected?
<select name="supplier">
<option value="Supplier 1">Supplier 1</option>
<option value="Supplier 2">Supplier 2</option>
<option value="Supplier 3">Supplier 3</option>
<option value="Supplier 4">Supplier 4</option>
</select>
A loop comes to mind, shall I assign a number to each option? There may be 100 suppliers so it would need to count right?
I would use a loop like...
$theirChosenSupplier = $supplierVarFromDatabaseOrWherever;
?>
<select name="supplier">
foreach($allSuppliers as $individualSupplier) {
if($individualSupplier == $theirChosenSupplier) {
$selected = "selected";
} else {
$selected = "";
}
?><option value="<?php echo $individualSupplier; ?>" <?php echo $selected; ?>>
<?php echo $individualSupplier; ?>
</option>
<?php } ?>
</select>
This code isn't tested but should give you an idea. Apologies if I've misunderstood the question.
adding an id is always a good thing. Assuming this is wordpress generated and you don't have a loop already there is always the option of using javascript and maybe an onchange event.
I dont know if the form is submitted first or you do some other stuff but maybe:
<select id="foo">
<option id="provider_1">Provider 1</option>
<option id="provider_2">Provider 2</option>
</select>
<script type="text/javascript">
window.onload = function() {
var selectedValue = '<?php echo someEscapeFunction($_SESSION['id_provider']); // or _GET, _POST ?>';
document.getElementById('foo').selectedIndex = document.getElementById(selectedValue).index;
}
</script>

jQuery change form action based on selected value

ive run into a quite tricky problem while using a method to set the form action when value changes. I have got the following code :
var actions = new Array();
actions[0] = "some-site.html";
actions[1] = "some-other-site.html";
actions[2] = "another-site.html";
etc....
$("select#feld-urlaubsart").change(function () {
if ($("select#fieldname").val() !== "") {
$("form#searchform").attr("action","http://www.mysite.com/" +
actions[$("select#fieldname").val()]);
}
});
On the other side, my select looks like this :
<select id="fieldname" name="fieldname">
<option value="0" selected="selected">All</option>
<optgroup label="Golfurlaub mit Greenfees">
<option value="1">Alle Greenfee-Angebote</option>
<option value="2">Top-Angebote</option>
<option value="3">Golfurlaub mit 4 Tage Greenfee</option>
</optgroup>
Actually it does work but i am not happy with it as i need to rely on the value="1" value="2" etc.
Now i need to use the actual description as value to be compatible with another script that i pass the value to for usage in PHP/JSON. I would need the data like this
<option value="All">All</option>
<option value="Option 1">Option 1</option>
etc
How can i change the action of the form based on the selected VALUE while not needing to use array keys like "1" "2" etc?
Thanks for any advice.
Use a json object:
actions = {"All":"some-site.html","Option1":"some-other-site.html"}
Then
actions["All"]
is
"some-site.html"

Codeigniter update form not retrieving dropdown values

I'm fairly new to CI. I have a customer database which stores a bunch of customer information. I have also created an Update controller to update current customer information. The update form is the same form as the new customer form but for the value I've got it pulling the old data from the database. My problem is it pulls all the data and displays it in it's propor field except the drop down field. Any ideas how to fix this?
CONTROLLER:
function edit_customer($id){
$data['success']=0;
if($_POST){
$data_customer=array(
'first_name'=>$_POST['first_name'],
'last_name'=>$_POST['last_name'],
'phone'=>$_POST['phone'],
'email'=>$_POST['email'],
'website'=>$_POST['website'],
'business_name'=>$_POST['business_name'],
'business_add'=>$_POST['business_add'],
'business_cityState'=>$_POST['business_cityState'],
'cc_type'=>$_POST['cc_type'],
'cc_number'=>$_POST['cc_number'],
'cc_exp'=>$_POST['cc_exp'],
'cc_cvd'=>$_POST['cc_cvd'],
'billing_add'=>$_POST['billing_add'],
'billing_zip'=>$_POST['billing_zip'],
'package'=>$_POST['package'],
'assigned_zip_code'=>$_POST['assigned_zip_code'],
'active'=>1
);
$data_customer['active'] = 1;
$this->customer->update_customer($id,$data_customer);
$data['success']=1;
}
$data['customer']=$this->customer->get_customer($id);
$this->load->view('header');
$this->load->view('edit_customer',$data);
$this->load->view('footer');
}
MODEL:
function update_customer($id, $data_customer){
$this->db->where('id', $id);
$this->db->update('customers', $data_customer);
}
VIEW DROPDOWN:
<label for="cc_type">Credit Card Type:</label>
<select name="cc_type" value="<?=$customer['cc_type'] ?>">
<option></option>
<option>Visa</option>
<option>Mastercard</option>
<option>American Express</option>
<option>Discover</option>
</select>
For an option to be selected, you need to add the selected attribute to the <option> elements.
For example:
<select name="type">
<option>a</option>
<option>b</option>
<option selected="selected">c</option>
<option>d</option>
</select>​
View it here: http://jsfiddle.net/3M4xv/
So in your code, you could do something like this:
<select name="cc_type">
<option <?php echo ($customer['cc_type']=='Visa')?'selected="selected"':''; ?>>Visa</option>
<option <?php echo ($customer['cc_type']=='Mastercard')?'selected="selected"':''; ?>>Mastercard</option>
<option <?php echo ($customer['cc_type']=='American Express')?'selected="selected"':''; ?>>American Express</option>
<option <?php echo ($customer['cc_type']=='Discover')?'selected="selected"':''; ?>>Discover</option>
</select>
Hope it helps :)

Categories