codeigniter form_radio how to get values in controllers - php

I have multiple radio buttons and a dropdown list. I want to populate the list based on the selected value. The problem is I can't get the selected value in controller.
View:
<?php echo form_open(base_url() . 'controller/method');
$type1 = array(
'name' => 'category',
'id'=> '1',
'value' => 't1',
'checked' => TRUE,
);
echo form_radio($type1);
echo form_label('Type 1');
$type2 = array(
'name' => 'category',
'id'=> '2',
'value' => 't2',
);
echo form_radio($type2);
echo form_label('Type 2');
$type3 = array(
'name' => 'category',
'id'=> '3',
'value' => 't3',
);
echo form_radio($type3);
echo form_label('Type 3');
echo form_close();
?>
Controller:
$category= $this->input->post('category');
When I try:
if(isset($category)){
echo $category;
}
else{
echo "variable is not there";
}
I always get "variable is not there".

Related

How to use javascript in codeigniter view page to generate onkeyup event

I want to calculate qty*price from give input when price and qty changes automatically total should get changed using javascript events in codeigniter and total filed should be disabled my code is as follow
<script>
functionn caltotal()
{
}
</script>
<?php
echo form_open('add_veg/insert_data', 'class="form-horizontal" id="myform" role="form"');
$sales_date = array(
'type' => 'date',
'name' => 'date',
'id' => 'date',
'class' => 'form-control'
);
$vegnames=array();
foreach($veg_names as $r){
$vegnames[$r->value]=$r->label;
}
/*
$vegnames = array(
'cauliflower' => 'Caulifower',
'Brinjal' => 'Brinjal'
);
*/
$wt = array(
'type' => 'text',
'name' => 'weight',
'id' => 'wt',
'class' => 'form-control'
);
$price = array(
'type' => 'text',
'name' => 'price',
'id' => 'price',
'class' => 'form-control'
);
$total=array(
'type'=>'readonly',
'name'=>'total',
'id'=>'total',
'class'=>'form-control disabled'
);
$save=array(
'name' =>'sub',
'value' =>'Save',
'class' =>'btn btn-success btn-sm'
);
echo form_label('Sales date', 'sdate', 'class="control-label col-sm-2"');
echo form_input($sales_date);
echo form_label('Vegitable Name', 'vname', 'class="control-label col-sm-2"');
//echo form_input($veg_name);
echo form_dropdown('v_name', $vegnames,'','class="form-control"');
echo form_label('Weight', 'wt', 'class="control-label col-sm-2"');
echo form_input($wt);
echo form_label('Price', 'price', 'class="control-label col-sm-2"');
echo form_input($price);
echo form_label('Total', 'total', 'class="control-label col-sm-2"');
echo form_input($total);
echo form_submit($save);
echo form_close();
?>
Try this
$('#quantity').keyup(function() {
var quantity = $("#quantity").val();
var iPrice = $("#item_price").val();
var total = quantity * iPrice;
$("#total_price").val(total); // sets the total price input to the quantity * price
});

How i get the real value in select form zf2

I need to get the real value, instead of the number position of a Select form field
I explain it with one example:
first in the form
$procesa=new Querys();
$datosAsignaturas=$procesa->getDatosAsignaturas();
$groups = array();
foreach ($datosAsignaturas as $id => $list) {
$groups[$id] =$list["nombreA"];
}
$selection= $factory-> createElement(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subjects',
'attributes' => array(
'id' => 'subjects',
'options' => $groups
),
));
$this->add($selection);
Second, the view
<div class="form_element">
<?php $element = $form->get('subjects');
?>
<label>
<?php echo $element->getOption('value'); ?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
third
["subjects"]=> string(1) "1"
i need something like this
["subjects"]=> string(1) "Maths"
Proper assignement of values to a Zend\Form\Element\Select is through the means of value_options inside options or using the element function setValueOptions(). Here a simple example:
$form->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'language',
'options' => array(
'label' => 'Which is your mother tongue?',
'empty_option' => 'Please choose your language',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
Now if you need to access the value options like this, you simply call getValueOptions() on the element and you'll receive exactly the same array as above. Then you could do something like this (which I'm assuming is what you're trying to do):
$elemLanguage = $form->get('language');
echo "<select name='{$elemLanguage->getName()}'>\n";
foreach($elemLanguage->getValueOptions() as $id => $language) {
echo "<option value='{$id}'>{$id} - {$language}</option>\n";
}
echo '</select>';
Maybe you mean :
<div class="form_element">
<?php $element = $form->get('subjects');
?>
<label>
<?php echo $element->getOption('label'); //-------- label instead value?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
And about the form
$procesa=new Querys();
$datosAsignaturas=$procesa->getDatosAsignaturas();
$groups = array();
foreach ($datosAsignaturas as $id => $list) {
$groups[$id] =$list["nombreA"];
}
$selection= $factory-> createElement(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subjects',
'attributes' => array(
'id' => 'subjects',
'options' => $groups
),
'options'=>array(
'label'=>"Your label",
'description' => 'your description',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
$this->add($selection);

How to select from a table and Edit the information in codeIgniter

I am building an application with Codeigniter and in this application I am currently able to search through a database table and generate a table in the application displaying the results. The next step for me is to be able to select a row in the application search result table and redirect me to a form where I can edit the information. I wanted to know how I can do that. below I will give you the code that generates the table. If more code or information is needed just let me know.
foreach ($query as $row){
$this->table->add_row($row);
}
echo $this->table->generate();
Update
Contoller
public function search(){
$this->load->model('reg_model');
$search_term = array(
'firstName' => $this->input->post('firstName'),
'lastName' => $this->input->post('lastName'),
'street' => $this->input->post('street'),
'dob' => $this->input->post('dob')
);
$data['query'] = $this->reg_model->search_voters($search_term);
$this->load->view("reg_header");
$this->load->view("reg_nav");
$this->load->view("reg_search", $data);
}
public function add(){
$this->load->view("reg_header");
$this->load->view("reg_nav");
$this->load->view("reg_form");
}
public function send(){
$this->load->library("form_validation");
$this->form_validation->set_rules("firstName", "First Name", "required");
$this->form_validation->set_rules("lastName", "Last Name", "required");
$this->form_validation->set_rules("homeNum", "Home Number", "required");
$this->form_validation->set_rules("street", "Street", "required");
$this->form_validation->set_rules("zip", "Zip Code", "required");
$this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
$this->form_validation->set_rules("district", "District", "required");
//add to database
if ($this->form_validation->run() == FALSE){
$this->load->view("reg_header");
$this->load->view("reg_nav");
$this->load->view("reg_form");
}
else{
$this->load->model('reg_model');
$this->reg_model->add_voters();
redirect(current_url());
}
}
function edit_voterS($voterNum) {
$voter = $this->reg_model->get_voter($voterNum);
$this->data['title'] = 'Edit Voter';
//validate form input
$this->load->library("form_validation");
$this->form_validation->set_rules("firstName", "First Name", "required");
$this->form_validation->set_rules("lastName", "Last Name", "required");
$this->form_validation->set_rules("homeNum", "Home Number", "required");
$this->form_validation->set_rules("street", "Street", "required");
$this->form_validation->set_rules("zip", "Zip Code", "required");
$this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
$this->form_validation->set_rules("district", "District", "required");
if (isset($_POST) && !empty($_POST))
{
$data = array(
'firstName' => $this->input->post('firstName'),
'lastName' => $this->input->post('lastName'),
'midInitial' => $this->input->post('midInitial'),
'homeNum' => $this->input->post('homeNum'),
'street' => $this->input->post('street'),
'apt' => $this->input->post('apt'),
'zip' => $this->input->post('zip'),
'dob' => $this->input->post('dob'),
'district' => $this->input->post('district')
);
if ($this->form_validation->run() === true)
{
$this->reg_model->update_voter($voterNum, $data);
$this->session->set_flashdata('message', "<p>voter updated successfully.</p>");
redirect(base_url().'reg/search/'.$voterNum);
}
}
$this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
$this->data['voter'] = $voter;
//display the edit product form
$this->data['firstName'] = array(
'name' => 'firstName',
'id' => 'firstName',
'type' => 'text',
'value' => $this->form_validation->set_value('firstName', $voter['firstName'])
);
$this->data['lastName'] = array(
'name' => 'lastName',
'id' => 'lastName',
'type' => 'text',
'value' => $this->form_validation->set_value('lastName', $voter['lastName'])
);
$this->data['midInitial'] = array(
'name' => 'midInitial',
'id' => 'midInitial',
'type' => 'text',
'value' => $this->form_validation->set_value('midInitial', $voter['firstName'])
);
$this->data['homeNum'] = array(
'name' => 'homeNum',
'id' => 'homeNum',
'type' => 'text',
'value' => $this->form_validation->set_value('homeNum', $voter['homeNum'])
);
$this->data['street'] = array(
'name' => 'street',
'id' => 'street',
'type' => 'text',
'value' => $this->form_validation->set_value('street', $voter['street'])
);
$this->data['apt'] = array(
'name' => 'apt',
'id' => 'apt',
'type' => 'text',
'value' => $this->form_validation->set_value('apt', $voter['apt'])
);
$this->data['zip'] = array(
'name' => 'zip',
'id' => 'zip',
'type' => 'text',
'value' => $this->form_validation->set_value('zip', $voter['zip'])
);
$this->data['dob'] = array(
'name' => 'dob',
'id' => 'dob',
'type' => 'text',
'value' => $this->form_validation->set_value('dob', $voter['dob'])
);
$this->data['district'] = array(
'name' => 'district',
'id' => 'district',
'type' => 'text',
'value' => $this->form_validation->set_value('district', $voter['district'])
);
$this->load->view('edit_form', $this->data);
}
function delete_voter($voterNum) {
$this->reg_model->del_voter($voterNum);
$this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');
redirect('reg/search');
}
Model:
public function search_voters($search_term){
$this->db->select('*');
$this->db->from('voterinfo');
$this->db->like('firstName', $search_term['firstName']);
$this->db->like('lastName', $search_term['lastName']);
$this->db->like('street', $search_term['street']);
$this->db->like('dob', $search_term['dob']);
$query = $this->db->get();
return $query->result_array(); }
public function get_voter($voterNum) {
$this->db->select('*');
$this->db->where('voterNum', $voterNum);
$query = $this->db->get('voterinfo');
return $query->row_array();
}
public function update_voter($voterNum, $data)
{
$this->db->where('voterNum', $voterNum);
$this->db->update('voter', $data);
}
public function del_voter($voterNum)
{
$this->db->where('voterNum', $voterNum);
$this->db->delete('voter');
}
Search View:
echo form_open("reg/search");
echo form_label("First Name: ", "firstName");
$data = array(
"name" => "firstName",
"id" => "firstName",
"value" => set_value("firstName")
);
echo form_input($data);
echo form_label("Last Name: ", "lastName");
$data = array(
"name" => "lastName",
"id" => "lastName",
"value" => set_value("lastName")
);
echo form_input($data);
echo form_label("Street: ", "street");
$data = array(
"name" => "street",
"id" => "street",
"value" => set_value("street")
);
echo form_input($data);
echo form_label("Date of Birth: ", "dob");
$data = array(
"name" => "dob",
"id" => "dob",
"value" => set_value("dob")
);
echo form_input($data);
echo form_submit("searchSubmit", "Search");
echo form_close();
$this->table->set_heading(array('', 'Voter Number', 'First Name', 'Last Name',
'Middle', 'Home #', 'Street',
'Apt', 'Zip', 'DOB',
'District', 'Edit'));
foreach ($query as $row){
$this->table->add_row($row);
}
echo $this->table->generate();
edit form view:
echo validation_errors();
echo form_open("reg/edit_voter");
echo form_label("First Name: ", "firstName");
$data = array(
"name" => "firstName",
"id" => "firstName",
"value" => $this->form_validation->set_value('firstName', $voter['firstName'])
);
echo form_input($data);
echo form_label("Last Name: ", "lastName");
$data = array(
"name" => "lastName",
"id" => "lastName",
"value" => $this->form_validation->set_value('lastName', $voter['lastName'])
);
echo form_input($data);
echo form_label("Middle Initial: ", "midInitial");
$data = array(
"name" => "midInitial",
"id" => "midInitial",
"value" => $this->form_validation->set_value('midInitial', $voter['midInitial'])
);
echo form_input($data);
echo form_label("Home Number: ", "homeNum");
$data = array(
"name" => "homeNum",
"id" => "homeNum",
"value" => $this->form_validation->set_value('homeNum', $voter['homeNum'])
);
echo form_input($data);
echo form_label("Street: ", "street");
$data = array(
"name" => "street",
"id" => "street",
"value" => $this->form_validation->set_value('street', $voter['street'])
);
echo form_input($data);
echo form_label("Apartment Number: ", "apt");
$data = array(
"name" => "apt",
"id" => "apt",
"value" => $this->form_validation->set_value('apt', $voter['apt'])
);
echo form_input($data);
echo form_label("Zip Code: ", "zip");
$data = array(
"name" => "zip",
"id" => "zip",
"value" => $this->form_validation->set_value('zip', $voter['zip'])
);
echo form_input($data);
echo form_label("Date of Birth: ", "dob");
$data = array(
"name" => "dob",
"id" => "dob",
"value" => $this->form_validation->set_value('dob', $voter['dob'])
);
echo form_input($data);
echo form_label("District: ", "district");
$data = array(
"name" => "district",
"id" => "district",
"value" => $this->form_validation->set_value('district', $voter['district'])
);
echo form_input($data);
echo form_submit("editSubmit", "edit");
echo form_close();
This requires two steps.
the first step is to load the existing data from the result.
the second step is to update the data you edited
For the first stop you want to create a link/button for each $row with the id of the result, let it call to a function in the controller with the id as parameter( for example loadEdit($id)).
let that function make a call to your model you are handling the results in, and send the id
with it as its only parameter:
public function loadEdit($id){
$this->load->model('model_results');
$id = $id;
$editData = $this->model_results->loadEdit_results($id);
}
in the loadEdit_results function in your model you only want to get 1 $row. The one corresponding with the id of the result you want to to edit.
So you write something like this query in that function:
public function edit_page($id){
$query = $this->db->get_where('pages', array('id' => $id));
}
This will get the one result with corresponding id.
Now it's time for the next step. The updating part:
I guess you know how to show the results in the view, so i don't go into that.
Just create a form with the data of the result in a form, and let that form submit to the edit function (for exmaple postEdit($id)) in your controller (dont forget, use the id as the parameter for the function)
the postEdit($id) function will have to make a call to your model, and send all the data there:
public function edit_page_validation($id){
$this->load->model('model_pages');
$this->model_pages->edit_page_update($id);
}
Then create a function in your model to update the result with the corresponding id.
public function edit_page_update($id){
$data = array(
/*
the array with the posted data from the form
*/
);
$query = $this->db->where('id', $id);
$query = $this->db->update('results', $data);
}
This might be a bit much to take in if you've never done something like this before. So here are some links to help you out:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Hopefully i helped you a bit :)

Form validation not working in CodeIgniter, controller issue

I have a controller and a view. I am trying to submit some details through a form, but when I submit it, no errors are displayed.
I am using only one controller to for 2 different types of users. The two users are -
User
Gym/Health Club Owner
And according to the $suertype, I have the same view being called but a different content being loaded.
This is my controller:
public function CreateProfile_Step2()
{
$data['page_title'] = 'Create Profile (Step 2)';
$loginid = $this->session->userdata('loginid');
$this->load->model('profilemodel', 'profile');
$userid = $this->profile->get_userid($loginid);
$this->session->set_userdata('userid', $userid);
$usertype = $this->profile->get_usertype($userid);
$data['usertype'] = $usertype;
if ($usertype == 'User') {
$this->load->model('activitymodel', 'activity');
$arr_activities = $this->activity->get_activities();
$data['options'] = $arr_activities;
}
else if ($usertype == 'Gym/Health Club Owner') {
$this->load->model('facilitymodel', 'facility');
$arr_facility = $this->facility->get_facilities();
$data['options'] = $arr_facility;
}
$config = array(
'User' => array(
array(
'name' => 'sex',
'label' => 'Sex',
'rules' => 'required'
)),
'Gym/Health Club Owner' => array(
array(
'name' => 'website',
'label' => 'Website',
'rules' => 'prep_url'
),
array(
'name' => 'hours_of_operation',
'label' => 'Hours of Operation',
'rules' => 'required|numeric'
),
array(
'name' => 'membership_charges',
'label' => 'Membership Charges',
'rules' => 'required|numeric'
))
);
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules($config);
if ($usertype == 'User') {
if ($this->form_validation->run('User') == FALSE) {
$this->load->view('create-profile-step-2', $data);
}
else {
$selected_options = $this->input->post('activities');
$this->activity->add_user_activities($userid, $selected_options);
$sex = $this->input->post('sex');
$this->profile->add_user_details($userid, $sex);
echo 'Profile Creation Completed!';
}
}
else {
if ($this->form_validation->run('Gym/Health Club Owner') == FALSE) {
$this->load->view('create-profile-step-2', $data);
}
else {
$selected_options = $this->input->post('facilities');
$this->facility->add_gym_facility($userid, $selected_options);
$website = $this->input->post('website');
$hours_of_operation = $this->input->post('hours_of_operation');
$membership_charges = $this->input->post('membership_charges');
$this->facility->add_gym_details($userid, $website, $hours_of_operation, $membership_charges);
echo 'Profile Creation Completed!';
}
}
}
This is my view:
<?php include 'user/inc/header.php'; ?>
<div id="content" class="row twelvecol">
<h1>Create Profile (Step 2 of 2)</h1>
<h2>For <?php echo $usertype; ?></h2>
<?php
echo form_open('register/CreateProfile_Step2');
if ($usertype == 'User') {
echo form_label('Sex', 'sex');
$options_sex = array(
'Male' => 'Male',
'Female' => 'Female'
);
echo form_dropdown('sex', $options_sex, 'male');
$ctr = 1;
echo form_label('Activities Interested In', 'activities');
foreach($options->result() as $option)
{
echo form_label($option->activity, 'activity-'.$ctr);
$arr_option = array(
'name' => 'activities[]',
'id' => 'activity-'.$ctr++,
'value' => $option->activity
);
echo form_checkbox($arr_option);
}
}
elseif ($usertype == 'Gym/Health Club Owner')
{
echo form_label('Website', 'website');
$arr_website = array(
'name' => 'website',
'id' => 'website',
'value' => set_value('website')
);
echo form_input($arr_website);
echo form_label('Hours of Operation', 'hours_of_operation');
$arr_hours = array(
'name' => 'hours_of_operation',
'id' => 'hours_of_operation',
'value' => set_value('hours_of_operation')
);
echo form_input($arr_hours);
echo form_label('Membership Charges', 'membership_charges');
$arr_charges = array(
'name' => 'membership_charges',
'id' => 'membership_charges',
'value' => set_value('membership_charges')
);
echo form_input($arr_charges);
$ctr = 1;
echo form_label('Facilities Available', 'facilities');
foreach($options->result() as $option)
{
echo form_label($option->facility, 'facility-'.$ctr);
$arr_option = array(
'name' => 'facilities[]',
'id' => 'facility-'.$ctr++,
'value' => $option->facility
);
echo form_checkbox($arr_option);
}
}
echo "<br/><br/>";
echo form_submit('submit', 'Create Profile');
echo form_close();
echo validation_errors();
?>
Return to previous step
</div>
<?php include 'user/inc/footer.php'; ?>
It's more likely some problem with the $config, because I have tried changing my code and used 2 separate controllers and views for the 2 cases.
Well, since no one answered my question. I was going through the code and found the errors myself. I had used the key 'name' in my configuration instead of 'field'.
INCORRECT CODE
$config = array(
'User' => array(
array(
'name' => 'sex',
'label' => 'Sex',
'rules' => 'required'
)),
'Gym/Health Club Owner' => array(
array(
'name' => 'website',
'label' => 'Website',
'rules' => 'prep_url'
),
array(
'name' => 'hours_of_operation',
'label' => 'Hours of Operation',
'rules' => 'required|numeric'
),
array(
'name' => 'membership_charges',
'label' => 'Membership Charges',
'rules' => 'required|numeric'
))
);
CORRECT CODE
$config = array(
'User' => array(
array(
'field' => 'sex',
'label' => 'Sex',
'rules' => 'required'
)),
'Gym/Health Club Owner' => array(
array(
'field' => 'website',
'label' => 'Website',
'rules' => 'prep_url'
),
array(
'field' => 'hours_of_operation',
'label' => 'Hours of Operation',
'rules' => 'required|numeric'
),
array(
'field' => 'membership_charges',
'label' => 'Membership Charges',
'rules' => 'required|numeric'
))
);

Why isn't my CodeIgniter Form Validation working?

I am trying to set up validation on a simple contact form that is created using the form helper. No validation at all occurs. What is wrong?
In the code below, the “good” keyword always shows, regardless of what is entered into the form, and the saved values set via set_value are never shown.
Controller
// Contact
function contact() {
$data['pageTitle'] = "Contact";
$data['bodyId'] = "contact";
$this->load->library('form_validation');
$config_rules = array ('email' => 'required','message' => 'required');
$this->form_validation->set_rules($config_rules);
if ($this->form_validation->run() == FALSE) {
echo "bad";
$data['include'] = "v_contact";
$this->load->view('v_template',$data);
} else {
echo "good";
$data['include'] = "v_contact";
$this->load->view('v_template',$data);
}
}
View
echo validation_errors();
echo form_open('events/contact');
// name
echo form_label('Name', 'name');
$data = array (
'name' => 'name',
'id' => 'name',
'maxlength' => '64',
'size' => '40',
'value' => set_value('name')
);
echo form_input($data) . "\n<br />";
// email address
echo form_label('Email Address', 'email');
$data = array (
'name' => 'email',
'id' => 'email',
'maxlength' => '64',
'size' => '40',
'value' => set_value('email')
);
echo form_input($data) . "\n<br />";
// message
echo form_label('Message', 'message');
$data = array (
'name' => 'message',
'id' => 'message',
'rows' => '8',
'cols' => '35',
'value' => set_value('message')
);
echo form_textarea($data) . "\n<br />";
echo form_submit('mysubmit', 'Send Message');
echo form_close();
It looks like you're not setting the validation rules according to the way the new Form_validation library does it (the user guide has a section on the new syntax). That seems to be the syntax for the old Validation library.
Try this instead for your $config_rules array and see if your validation runs properly:
$config_rules = array(
array('field' => 'email', 'rules' => 'required'),
array('field' => 'message', 'rules' => 'required')
);
$this->form_validation->set_rules($config_rules);

Categories