How to send php variable from view(html) to controller - php

This is my html code. I want to pass "$category_edit->c_name" value to my Update() controller. I am getting "$category_edit" variable from another controller.
I am using CodeIgniter framework.
<form method="post" action="<?php echo base_url('admin/category/update');?>">
<label>Parent Category: </label></br>
<select name="parent_id">
<?php
echo '<option value="' .$category_edit->id .'">';
echo $category_edit->p_name;
echo '</option>';
?>
</select>
<label>Category</label>
<input type="text" name="<?php echo $category_edit->c_name; ?>" id="category_name" value="<?php echo $category_edit->c_name; ?>">
<button>Update</button>
</form>
This is my update() controller.
I am getting Error:
Undefined variable: category_edit
Trying to get property of non-object
public function update(){
$this->load->model('category_model');
echo $category_edit->c_name;
}

Please kindly check this reference code:
public function update_view()
{
$this->load->model('category_model');
$data['category_edit'] = $this->category_model->get_category_values(); // return array
$data['extra_variable'] = 'lorem ipsum';
$this->load->view('category/update_view', $data);
}
at your category/update_view.php :
<form method="post" action="<?php echo base_url('admin/category/update');?>">
<label>Parent Category: </label></br>
<select name="parent_id">
<?php
echo '<option value="' .$category_edit['id'] .'">';
echo $category_edit['p_name'];
echo '</option>';
?>
</select>
<label>Category</label>
<input type="text" name="<?php echo $category_edit['c_name']; ?>" id="category_name" value="<?php echo $category_edit['c_name']; ?>">
<button>Update</button>
</form>
EDIT:
Please refer: http://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

Make some changes in HTML(See below code)
<input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">
public function update()
{
$this->load->model('category_model');
echo $this->input->post('category_name');
}

You need to add name of field static not any variable.
so try to add like this
<input type="text" name="category_name" id="category_name" value="<?php echo $category_edit->c_name; ?>">
and on controller you can get value of it like
$this->input->post('category_name');

Related

Codeigniter - Can't update data in MySQL Database

I have some problem with CodeIgniter, I can't update data in a record in CodeIgniter. 'id_user' is the primary key of my user table. I have tried to solve this problem but I couldn't. I hope someone can help with this problem. Thanks a lot :)
I have posted the code below :
HTML5 code :
<form action="<?php echo base_url(). 'crud/update'; ?>" method="post">
<input name="id_user" type="text" class="form-control" value="<?php echo $id_usernya ?>">
<div class="form-group">
<label>Alamat :</label>
<textarea name="alamat_user" class="form-control" rows="5" placeholder="Alamat" ><?php echo $usernya->alamat_user ?></textarea>
</div>
<div class="form-group">
<input name="kodepos_user" type="text" class="form-control" value="<?php echo $usernya->kodepos_user ?>">
</div>
<div class="form-group">
<input type="hidden" name="provinsi_user">
<select name="provinsi_id_user" class="form-control">
<option value="">Pilih Provinsi</option>
<option selected value="<?php echo $usernya->provinsi_user ?>"><?php echo $usernya->provinsi_user ?></option>
<?php echo $provinsi ?>
</select>
</div>
<div class="form-group">
<input type="hidden" name="kota_user">
<select name="kota_id_user" class="form-control">
<option value="">Pilih Kota</option>
<option selected value="<?php echo $usernya->kota_user ?>"><?php echo $usernya->kota_user ?></option>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Simpan">
</form>
Controller :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
function edit($id_user){
$where = array('id_user' => $id_user);
$data['user'] = $this->m_data->edit_data($where,'user')->result();
$this->load->view('back/user/v_beranda',$data);
redirect('beranda/index');
}
function update(){
$id_user = $this->input->post('id_user');
$alamat_user = $this->input->post('alamat_user');
$kodepos_user = $this->input->post('kodepos_user');
$provinsi_id_user = $this->input->post('provinsi_id_user');
$kota_id_user = $this->input->post('kota_id_user');
$data = array(
'alamat_user' => $alamat_user,
'kodepos_user' => $kodepos_user,
'provinsi_id_user' => $provinsi_id_user,
'kota_id_user' => $kota_id_user
);
$where = array(
'id_user' => $id_user
);
$this->m_data->update_data($where,$data,'user');
redirect('beranda/index');
}
Model code :
<?php
class M_data extends CI_Model{
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
}
Initialize your Model like what your have done on your Controller
class M_data extends CI_Model{
public function __construct() {
parent::__construct();
$this->load->database();
}
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
}
try to print query to check what exact query getting executed
$this->db->last_query():
and which error its shows when you execute above query

Disable datalist input if there is no option data

I have datalist like this :
<input type="text" list="colours" id="txt">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
The option that datalist has was populated from a foreach loop using php.
How do I disable the datalist input, if the data that foreach loop's result is null/none using jquery ?
like for example
Have Option
<input type="text" list="colours">
No Option
<input type="text" list="colours" disabled="disabled">
Here is an example of keeping your PHP and HTML separate:
<?php
$option="";
foreach ($kondisi as $kondisi) {
if($kondisi != 'null' || $kondisi != 'none'){
$option .= '<option data-value="'.$kondisi'.->nama_kondisi" value="'.$kondisi.'->id_kondisi">'.$kondisi.'->nama_kondisi</option>';
}else{
$option .= 'your disabled option code here...';
}
}
?>
<input type="text" list="colours">
<datalist id="colours">
<?php echo $option; ?>
</datalist>
If you want to disable it then do like below:-
<?php if(count($kondisi)>0){?>
<input type="text" list="colours">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
<?php }else{?>
<input type="text" list="colours" disabled="disabled"/><!-- or use autocomplete="off"-->
<?php } ?>

Put the value as title in text field

i want to get the partcular text value as tittle in that field
How can i do..
<input type="text"
<?php if($name_one != '') { ?> value="<?php echo $name_one;?>" <?php echo set_value('name1'); ?>
<?php } else { ?> value="<?php echo set_value('name1'); ?>" <?php } ?>
name="act1" id="act1" title="<?php echo set_value('name1'); ?>">
You can try like this
<input type="text"
value="<?php echo (!empty($name_one)) ? $name_one : $name1 ; ?>"
Single line if condition

Update the database with codeigniter and display the updation in same page

I am trying to update the data to the database by php CodeIgniter and display the result in the same page during update. Below is my code.The problem is it didn't show any error message and didn't update the values in the database and It is not displaying any values in the page after update. Please help.
View Page
<?php
echo form_open('Welcome/service1');
?>
<input type="hidden" name="servicer_id" value="<?php echo $value['service_id']?>">
<input type="hidden" name="servicer_name" value="<?php echo $value['servicer_name']?>">
<input type="hidden" name="servicer_email" value="<?php echo $value['servicer_email']?>">
<input type="hidden" name="servicer_checkin" value="<?php echo $value['servicer_checkin']?>">
<input type="hidden" name="servicer_checkout" value="<?php echo $value['servicer_checkout']?>">
<input type="hidden" name="servicer_requestdate" value="<?php echo $value['servicer_requestdate']?>">
<input type="hidden" name="servicer_detail" value="<?php echo $value['servicer_detail']?>">
<input type="hidden" name="servicer_contact" value="<?php echo $value['servicer_contact']?>">
<input type="hidden" name="servicer_priority" value="<?php echo $value['servicer_priority']?>">
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Complete") {?> checked="checked"<?php } ?> name="current_status" value="Complete">Complete
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Ongoing") {?> checked="checked"<?php } ?> name="current_status" value="Ongoing">Ongoing
</label>
<label class="radio-inline">
<input type="radio" <?php if($value['status']=="Rejected") {?> checked="checked"<?php } ?> name="current_status" value="Rejected">Rejected
</label>
<button type="submit" class="btn btn-default">
<span>Status</span>
</button>
<?php
echo form_close();
?>
Controller page
public function service1()
{
$this->load->helper('url');
$page_id =$this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status();
$data['s']=$this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service.php',$data);
}
Model page
public function add_status()
{
this->load->database();
this->load->helper('url');
hotel_id=1;
servicer_name= $this->input->post('servicer_name');
servicer_email= $this->input->post('servicer_email');
servicer_checkin= $this->input->post('servicer_checkin');
servicer_checkout= $this->input->post('servicer_checkout');
servicer_requestdate= $this->input->post('servicer_requestdate');
servicer_detail= $this->input->post('servicer_detail');
servicer_contact= $this->input->post('servicer_contact');
servicer_priority= $this->input->post('servicer_priority');
status= $this->input->post('status');
service_id= $this->input->post('servicer_id');
data=array('hotel_id'=>$hotel_id,'servicer_name'=>$servicer_name,'servicer_email'=>$servicer_email,'servicer_checkin'=>$servicer_checkin,'servicer_checkout'=>$servicer_checkout,'servicer_requestdate'=>$servicer_requestdate,'servicer_detail'=>$servicer_detail,'servicer_contact'=>$servicer_contact,'servicer_priority'=>$servicer_priority,'status'=>$status);
this->db->where('service_id',$service_id);
this->db->update('service_hotel1',$data);
}
You are passing inputs to the model but they are getting passed to the controller function.. please update
Controller:
public function service1()
{
/** you are posting the form to this function and not to the model.
* which is why all input->post must be here
*/
$servicer_name = $this->input->post('servicer_name');
$servicer_email = $this->input->post('servicer_email');
$servicer_checkin = $this->input->post('servicer_checkin');
$servicer_checkout = $this->input->post('servicer_checkout');
$servicer_requestdate= $this->input->post('servicer_requestdate');
$servicer_detail = $this->input->post('servicer_detail');
$servicer_contact = $this->input->post('servicer_contact');
$servicer_priority = $this->input->post('servicer_priority');
$status = $this->input->post('status');
$service_id = $this->input->post('servicer_id');
$this->load->helper('url');
$page_id = $this->uri->segment(3);
$this->load->model('Login_set');
$this->Login_set->add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id);
$data['s'] = $this->Login_set->select2();
$this->load->view('App_stay/pages/hotel1_service',$data); //also view must be loaded without .php so $this->load->view('App_stay/pages/hotel1_service',$data);
}
Model:
public function add_status($servicer_name, $servicer_email, $servicer_checkin, $servicer_checkout, $servicer_requestdate, $servicer_detail, $servicer_contact, $servicer_priority, $status, $service_id)
{
$this->load->database();
$this->load->helper('url');
$hotel_id =1 ;
$data = array(
'hotel_id' => $hotel_id,
'servicer_name' => $servicer_name,
'servicer_email' => $servicer_email,
'servicer_checkin' => $servicer_checkin,
'servicer_checkout' => $servicer_checkout,
'servicer_requestdate' => $servicer_requestdate,
'servicer_detail' => $servicer_detail,
'servicer_contact' => $servicer_contact,
'servicer_priority' => $servicer_priority,
'status' => $status
);
$this->db->where('service_id',$service_id);
$this->db->update('service_hotel1',$data);
}

POST a hidden input + multiple options PHP

Hi there I'm quite new to PHP
I have this problem:
I would like to POST a multiple choice + a hidden field from a form:
<?php
if (isset($_SESSION['nickname']))
{
$result = mysql_query("SELECT * FROM users");
$teamsCount = ceil(mysql_num_rows($result)/2);
for ($i=1; $i<=$teamsCount; $i++)
{
// TEST: echo $i . " TeamsCount er: " . $teamsCount. "<br>";
?>
Team <? echo $i; ?>
<form name="addTeam" action="buildTeams.php" method="POST">
<input type="hidden" name="hiddenField" value="<?php $i; ?>" />
<select name="teams[]" multiple="multiple" size="<?php echo mysql_num_rows($result); ?>">
<?php
$query = mysql_query("SELECT * FROM users");
while ($row=mysql_fetch_array($query))
{
$id=$row["ID"];
$nick=$row["Nick"];
?>
<option value="<?php echo $id; ?>"><?php echo ucfirst($nick); ?></option>
<?php
}
?>
</select>
<input type="submit" value="Make them teams!!" />
</form>
<?php
}
}
?>
I think you have an error in this line:
<input type="hidden" name="hiddenField" value="<?php $i ?>" />
It should be
<input type="hidden" name="hiddenField" value="<?php echo $i ?>" />
Edit:
Put the team id in the select name. Example:
<select name="teams[<?=$i?>][]">
And in PHP do:
foreach ($_POST['teams'] as $team_id => $choices)
I think you should check $_POST['hiddenField'] to obtain hidden value

Categories