Codeigniter update form not retrieving dropdown values - php

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 :)

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

Dynamic generate form based on select box change value

I want to generate dynamic form based on select box change value.
Suppose i have a parent select box :
<select name="select1" id="select1">
<option value="1">Fruit</option>
<option value="2">Animal</option>
<option value="3">Bird</option>
<option value="4">Car</option>
</select>
below select box is depend on first
<select name="select2" id="select2">
<option value="banana">Banana</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select>
Here is my 2 select boxes and i want 2 things
1) create dependency of 2 this select box
2 ) I want to generate a dynamic form based on change second select box value.
for example if i select apple then a form open with related apple value.
I am a new in jquery and i do not have any idea about it ( share me code script if any one knows)
You can follow this way to making a dynamic dropdown.
The following HTML code contains dependent dropdowns for countries and states. Country options are read from the database and shown in the dropdown on page load. Initially, the state dropdown has no options. On changing the country dropdown values, a jQuery function is called to get dependent state options and loaded dynamically.
<div class="frmDronpDown">
<div class="row">
<label>Country:</label><br/>
<select name="country" id="country-list" class="demoInputBox" onChange="getState(this.value);">
<option value="">Select Country</option>
<?php
foreach($results as $country) {
?>
<option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>State:</label><br/>
<select name="state" id="state-list" class="demoInputBox">
<option value="">Select State</option>
</select>
</div>
</div>
The following jQuery script shows a function to send an AJAX request to PHP to read state list depends on the selected country. This AJAX call is set with the selected country id.
<script>
function getState(val) {
$.ajax({
type: "POST",
url: "get_state.php",
data:'country_id='+val,
success: function(data){
$("#state-list").html(data);
}
});
}
</script>
In PHP, it connects the database to retrieve “states” table values based on the country id passed by jQuery AJAX call. It forms state dropdown options and returns as the AJAX response. This response is inserted to the state dropdown.
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["country_id"])) {
$query ="SELECT * FROM states WHERE countryID = '" . $_POST["country_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select State</option>
<?php
foreach($results as $state) {
?>
<option value="<?php echo $state["id"]; ?>"><?php echo $state["name"]; ?></option>
<?php
}
}
?>

How to get id of selected option in dropdown?

i want to get the id of selected option in dropdown.
I have a dropdow that displays company names from database. Now I want get the id of that company. How do i do that?
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value) { ?>
<option id="org" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name']; ?></option>
<?php } ?>
</select>
The Model
public function get_organisation()
{
$q = $this->db->select('*')
->from('company')
->get();
return $q->result_array();
}
And in my controller i want the id of selected option from database.
$data = $this->key_m->array_from_post(array('id','org-list','keys'));
$data['keys'] = $license;
var_dump($data);
You should put the organization id in option tag's value. like this
<option value="<?php echo $value['org_id'];?>"><?php echo $value['org_name']; ?></option>
No need to provide id attribute to each option tag. When you will submit the form value of "org-list" will be selected organization id.
This is the ajax way
$.post(url,{ id : "id value from drop down here" },function(data){
//This is the call back for success
})
For that one, you need jquery. The is the easiest way to use. Url is where you are posting back to server.
In your server side catch like this
$this->input->post('id')
Because you used "id" in the ajax request. So you have to retrieve it using the field name 'id' . Hope you get it.
There are a couple things which you "should" / "could" change. At first I noticed that your ID attribute is "org", Id's are supposed to be unique and yours is not. Though, back to the actual question. You should change your code as follow:
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value): ?>
<option id="<?php echo $value['org_id'];?>" value="<?php echo $value['org_id'];?>"><?php echo $value['org_name']; ?></option>
<?php endforeach; ?>
</select>
Now in your controller you grab the selected ID like: $this->input->post('org-list').
You have to provide value for each id attr. for corresponding <option>
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value): ?>
<option id="<?php echo $value['org_id'];?>" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name']; ?></option>
<?php endforech; ?>
</select>

retrieve selected value in form_dropdown() and act on it in the same view

I am using codeigiter's form_dropdown() and would like to make the selection alter data farther down in the same view without submitting the form.
In the view php file:
echo form_dropdown('department_select',$options,'1');
<?php $test_list = $this->trainingmodel->get_dept_tests($deptselected); ?>
I would like $deptselected to reflect what the user has selected in the form_dropdown(). I would like this to update every time the user changes their dropdown selection but without submitting the form.
Things like the following would work if the form was submitted, but not before:
$deptselected = $_POST['department_select'];
or
$deptselected = $this->input->post('department_select');
There must be a way to do what I want using javascript, or onchange or the 4th input parameter to form_dropdown().
You can use the chained select jquery plugin http://www.appelsiini.net/projects/chained
A simple example taken from the website:
<select id="mark" name="mark">
<option value="">--</option>
<option value="bmw" selected>BMW</option>
<option value="audi">Audi</option>
</select>
<select id="series" name="series">
<option value="--">--</option>
</select>
<select id="engine" name="engine">
<option value="--">--</option>
</select>
<select id="transmission" name="transmission">
<option value="--">--</option>
</select>
And on
$("#transmission").remoteChained({
parents : "#engine",
url : "/api/transmissions.json",
depends : "#series"
});
You can set the ajax url to your Codeigniter controller that will serve the data to the view in json format.

Change the list of dropdown according to the selection of other dropdown

I have two dropdowns .i want second dropdown list shoul changed according to the value selected in first dropdown.
this is my first dropdown
Category :<select name="Category" id="a1_txtBox5" required="required">
<option value="select">select..</option>
<?php while($selectcategoryarray=mysql_fetch_array($selectcategory)) {
?>
<option value="<?php echo $selectcategoryarray[1];?>"><?php echo $selectcategoryarray[1];?></option>
<?php
}
?>
</select>
And here is my second dropdown:
<label style="margin-left:24px;">Subcategory :</label><select style="margin-right:35px;" name="subcategory" id="a1_txtBox3" required="required">
<option value="select"> select..</option>
<?php while($selectsubcategoryarray=mysql_fetch_array($selectsubcategory)) {
?>
<option value="<?php echo $selectsubcategoryarray[2];?>"><?php echo $selectsubcategoryarray[2];?></option>
<?php
}
?>
</select>
Please help.
Exactly you need to handle the Change Event for your first Select element and in the body of the event you need to send request to server for getting data of second Select element.
I recommend to use an ajax process to doing this.
And o this you should use jQuery for handling events and have ajax.

Categories