How to multiple insert in ci in the same table in database - php

I have a two table to be inserted, I want to insert two times in every in each table. But in my code the only first amount will be inserted. Can you give me an advice on what should i do because I'm not really good in CI.
Here is my controller
function addloads(){
$data = array(
'member' => $this->input->post('member'),
'cell_number' => $this->input->post('cell_number'),
'amount' => $this->parseAmount($this->input->post('amount')),
'total' => $this->parseAmount($this->input->post('total')),
'status' => $this->input->post('status'),
'added_by' => $this->session->userdata('admin_id'),
'date' => date('Y-m-d',strtotime($this->input->post('date'))),
'after_rebate' => $this->input->post('rebate'),
);
$this->db->where('mem_id',$this->input->post('member'));
$this->db->insert('loading_service',$data);
$status = $this->input->post('status');
if($status == 'paid'){
$data2 = array(
'incomefrom_id' => $this->input->post('member'),
'income_name' => 'Load',
'date_created' => date('Y-m-d',strtotime($this->input->post('date'))),
);
$this->db->set('time_created', 'NOW()', FALSE);
$this->db->insert('incomefrom',$data2);
$amount = $this->parseAmount($this->input->post('amount'));
$total = $this->parseAmount($this->input->post('total'));
$all = $total - $amount;
$data3 = array(
'amount' => $all,
'incomefrom' => $this->input->post('member'),
'trans_date' => date('Y-m-d',strtotime($this->input->post('date'))),
);
$this->db->set('trans_time', 'NOW()', FALSE);
$this->db->insert('income',$data3);
$amount = $this->parseAmount($this->input->post('amount'));
$after = $this->parseAmount($this->input->post('rebate'));
$income = $amount - $after;
$data9 = array(
'amount' => $income,
'incomefrom' => $this->input->post('member'),
'trans_date' => date('Y-m-d',strtotime($this->input->post('date'))),
);
$this->db->set('trans_time', 'NOW()', FALSE);
$this->db->insert('income',$data9);
$data10 = array(
'incomefrom_id' => $this->input->post('member'),
'income_name' => 'Rebate',
'date_created' => date('Y-m-d',strtotime($this->input->post('date'))),
);
$this->db->set('time_created', 'NOW()', FALSE);
$this->db->insert('incomefrom',$data10);
}
//$this->load->view('loadingservice',$data);
//redirect(base_url('loadscontroller/loadingserviceview'));
}
And this is my view
<form action="<?php echo base_url('loadscontroller/addloads') ?>" method="post">
<div class="row">
<div class="col-xs-6">
<div class="form-group" id="member_form">
<label for="member">Member</label>
<?php
$sql=mysql_query("SELECT * FROM member WHERE mem_is_deleted='0' ORDER BY mem_fname ASC ");
if(mysql_num_rows($sql)){
$select= '<select class="form-control" id="member" name="member" required >';
while($rs=mysql_fetch_array($sql)){
$select.='<option value="'.$rs['mem_id'].'">'.$rs['mem_fname'].' ' .$rs['mem_lname'].'</option>';
}
}
$select.='</select>';
echo $select;
?>
<p class="help-block text-danger" id="member_err"></p>
</div>
<div class="form-group" id="cell_number">
<label for="cell_number">Cellphone Number</label>
<input type="text" class="form-control" id="cell_number" name="cell_number" placeholder="" required>
<!-- <p class="help-block text-danger" id="grossamount_err"></p> -->
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="text" id="txt1" onkeyup="sum();" value="5" hidden>
<input type="text" class="form-control " id="amount" name="amount" placeholder="00" required onkeyup="sum();">
</div>
</div>
</div>
<!-- <div class="row" id="willsend" style="display:none">
<div class="col-xs-6">
<div class="form-group">
<p>Will send <span id="lblnet"></span> to <span id="lblbaccountname"></span> <span id="lblbaccount"></span></p>
</div>
</div>
</div> -->
<div class="row">
<div class="col-xs-3">
<div class="form-group">
<label for="total">Total</label>
<input type="text" class="form-control" id="total" name="total" placeholder="00" required >
</div>
</div>
</div>
<div class="row">
<div class="col-xs-3">
<div class="form-group">
<!-- <label for="total">With Rebate</label> -->
<input type="text" id="rebate" name="rebate" >
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<div class="form-group" style="width: 200px;">
<label for="date">Date</label>
<input type="text" class="form-control readonly datepicker" id="date" name="date" value="<?php echo date('F d, Y') ?>" required readonly>
</div>
</div>
</div>
<div class="form-group" id="status">
<label for="status">Status</label>
<select class="form-control" id="status" name="status" required style="width: 150px;">
<option selected="" value="">--select--</option>
<option value="paid">PAID</option>
<option value="unpaid">UNPAID</option>
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<!-- /.row -->
</form>

Related

Getting error while uploading image and files in CodeIgniter

I have created an employee registration form but I'm getting errors in image uploading and storing in the image folder and also getting errors in document file uploading. and errors like
Error Number: 1048
Column 'resume' cannot be null
INSERT INTO add_employee (photograph, name, date_of_birth, date_of_joining, designation, qualification, Blood_Group, exp_before_joining, exp_after_joining, total_exp, permanent_address, correspondance_address, resume, education_certificate, experience_certificate, id_proof, address_proof, contact, emergency_contact, emergency_contact_person) VALUES ('', 'xxx', '2021-04-10', '2021-04-10', 'Production', 'MCA', 'O+ve', '4', '4', '4', 'asdfgh \r\n ', ' hjghjfghdgfd,ghfchhf\r\n ', NULL, NULL, NULL, NULL, NULL, '6754321234', '6789557890', 'xzxz')
Filename: C:/xampp/htdocs/maheshfoundation/system/database/DB_driver.php
Line Number: 691
my view file
<?php echo form_open_multipart('employee/store');?>
<div class="row">
<div class="col-3">
<div class="form-group">
<label for="photograph">Photograph</label>
<input type="file" class="form-control" name="photograph" id="photograph" placeholder="Choose file" required>
<small><?php echo form_error('photograph'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="name">Enter Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Enter Name" required>
<small><?php echo form_error('name'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="date_of_birth">Date of Birth</label>
<input type="date" class="form-control" name="date_of_birth" id="date_of_birth" placeholder="DOB" required>
<small><?php echo form_error('date_of_birth'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="date_of_joining">Date of Joining</label>
<input type="date" class="form-control" name="date_of_joining" id="date_of_joining" placeholder="DOJ" required>
<small><?php echo form_error('date_of_joining'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<div class="form-group">
<label for="designation">Designation</label>
<select class="form-control" name="designation" id="designation" required>
<option value="select">---Select---</option>
<option value="Marketing">Marketing</option>
<option value="Design">Design</option>
<option value="Super Admin">Super Admin</option>
<option value="Purchase">Purchase</option>
<option value="Planning">Planning</option>
<option value="Production">Production</option>
<option value="Quality">Quality</option>
<option value="Stores">Stores</option>
<option value="Management">Management</option>
<option value="HR">HR</option>
</select>
<small><?php echo form_error('designation'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="qualification">Qualification</label>
<input type="text" class="form-control" name="qualification" id="qualification" placeholder="Enter qualification" required>
<small><?php echo form_error('qualification'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="Blood Group">Blood Group</label>
<input type="text" class="form-control" name="Blood_Group" id="Blood_Group" placeholder="Enter Blood Group" required>
<small><?php echo form_error('Blood_Group'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="exp_before_joining">Experience Before Joining</label>
<input type="number" class="form-control" name="exp_before_joining" id="exp_before_joining" placeholder="Enter Experience Before Joining" required>
<small><?php echo form_error('exp_before_joining'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<div class="form-group">
<label for="exp_after_joining">Experience After Joining</label>
<input type="number" class="form-control" name="exp_after_joining" id="exp_after_joining" placeholder="Enter Experience After Joining" required>
<small><?php echo form_error('exp_after_joining'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="total_exp">Total Experience</label>
<input type="text" class="form-control" name="total_exp" id="total_exp" placeholder="Enter Total Experience" required>
<small><?php echo form_error('total_exp'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="Permanent_Address">Permanent Address *</label>
<textarea class="form-control" id="permanent_address" name="permanent_address" rows="4" cols="50" required>
</textarea>
<small><?php echo form_error('permanent_address'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="Correspondance_Address">Correspondance Address</label>
<textarea class="form-control" id="correspondance_address" name="correspondance_address" rows="4" cols="50" required>
</textarea>
<small><?php echo form_error('correspondance_address'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<div class="form-group">
<label for="resume">Resume</label>
<input type="file" class="form-control" name="resume" id="resume" placeholder="Choose file" required>
<small><?php echo form_error('resume'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="Education_Certificate">Education Certificate</label>
<input type="file" class="form-control" name="education_certificate" id="education_certificate" placeholder="Enter Name" required>
<small><?php echo form_error('education_certificate'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="Experience_Certificate">Experience Certificate</label>
<input type="file" class="form-control" name="experience_certificate" id="experience_certificate" placeholder="DOB" required>
<small><?php echo form_error('experience_certificate'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="id_proof">ID Proof *</label>
<input type="file" class="form-control" name="id_proof" id="id_proof" placeholder="DOJ" required>
<small><?php echo form_error('id_proof'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<div class="form-group">
<label for="Address_Proof">Address Proof</label>
<input type="file" class="form-control" name="address_proof" id="address_proof" placeholder="Choose file" required>
<small><?php echo form_error('address_proof'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="contact">Permanent Contact Number *</label>
<input type="text" class="form-control" name="contact" id="contact" placeholder="Enter Permanent Contact Number" required>
<small><?php echo form_error('contact'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="emergency_contact">Emergency Contact Number</label>
<input type="text" class="form-control" name="emergency_contact" id="emergency_contact" placeholder="Enter Emergency Contact Number" required>
<small><?php echo form_error('emergency_contact'); ?></small>
</div>
</div>
<div class="col-3">
<div class="form-group">
<label for="emergency_contact_person">Emergency Contact Person Name ?</label>
<input type="text" class="form-control" name="emergency_contact_person" id="emergency_contact_person" placeholder="Enter Emergency Contact Person Name" required>
<small><?php echo form_error('emergency_contact_person'); ?></small>
</div>
</div>
</div>
<button type="submit" value="submit" class="btn btn-primary">Submit</button>
<button type="reset" value="reset" class="btn btn-primary">Reset</button>
my controller file
public function store()
{
$photograph = $this->input->POST('photograph');
$name = $this->input->POST('name');
$date_of_birth = $this->input->POST('date_of_birth');
$date_of_joining = $this->input->POST('date_of_joining');
$designation = $this->input->POST('designation');
$qualification = $this->input->POST('qualification');
$Blood_Group = $this->input->POST('Blood_Group');
$exp_before_joining = $this->input->POST('exp_before_joining');
$exp_after_joining = $this->input->POST('exp_after_joining');
$total_exp = $this->input->POST('total_exp');
$permanent_address = $this->input->POST('permanent_address');
$correspondance_address = $this->input->POST('correspondance_address');
$resume = $this->input->POST('resume');
$education_certificate = $this->input->POST('education_certificate');
$experience_certificate = $this->input->POST('experience_certificate');
$id_proof = $this->input->POST('id_proof');
$address_proof = $this->input->POST('address_proof');
$contact = $this->input->POST('contact');
$emergency_contact = $this->input->POST('emergency_contact');
$emergency_contact_person = $this->input->POST('emergency_contact_person');
$configUpload['upload_path'] = 'images/'; #the folder placed in the root of project
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; #allowed types description
$configUpload['max_size'] = '0'; #max size
$configUpload['max_width'] = '0'; #max width
$configUpload['max_height'] = '0'; #max height
$configUpload['encrypt_name'] = TRUE; #encrypt name of the uploaded file
$this->load->library('upload', $configUpload); #init the upload class
if (!$this->upload->do_upload())
{
$uploadedDetails = $this->upload->display_errors();
}
else
{
$uploadedDetails = $this->upload->data();
}
$emp_filename = $this->upload->data('file_name');
//print_r($uploadedDetails);
// // die;
echo "<br><br>";
$data = array(
'photograph' => $emp_filename,
'name' => $name,
'date_of_birth' => $date_of_birth,
'date_of_joining' => $date_of_joining,
'designation' => $designation,
'qualification' => $qualification,
'Blood_Group' => $Blood_Group,
'exp_before_joining' => $exp_before_joining,
'exp_after_joining' => $exp_after_joining,
'total_exp' => $total_exp,
'permanent_address' => $permanent_address,
'correspondance_address' => $correspondance_address,
'resume' => $resume,
'education_certificate' => $education_certificate,
'experience_certificate' => $experience_certificate,
'id_proof' => $id_proof,
'address_proof' => $address_proof,
'contact' => $contact,
'emergency_contact' => $emergency_contact,
'emergency_contact_person' => $emergency_contact_person
);
$this->load->model('EmployeeModel','emp');
$this->emp->insertEmployee($data);
redirect(base_url('add'));
}
my model file
public function insertEmployee($data)
{
return $this->db->insert('add_employee', $data);
}

Calling controllers different methods from view page using codeigniter

I am working on codeigniter project in which viwe page having textboxes, radio buttons and button.I fetched textbox values from database and manually enter from date and to date then by clicking on save button insert query gets empty values because in controller i called all functions serially in index function.What is the best solution to call controller methods on each event of view page?
leave_appl_view.php
<form action="leave_apply_control/index" method="post" id="myForm">
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Emp ID</label>
<?php if(!empty($emp_leave))
{
foreach ($emp_leave as $liv)
{ ?>
<input type="text" class="form-control" name="id" value="<?php echo $liv->employee_id ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Employee Name</label>
<input type="text" class="form-control" name="empname" id="empname" value="<?php echo $liv->employee_name ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Email address</label>
<input type="email" class="form-control" name="empmail" id="empmail" value="<?php echo $liv->email ?>" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Leave type: </label>
<input type="radio" id="half" name="type" onclick="Checkradiobutton()" value="half"> Half Day
<input type="radio" id="full" name="type" onclick="Checkbutton()" value="full" Checked> Full Day
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="frm_dt">
<label class="control-label">From Date: </label>
<input type="text" class="form-control" id="datepicker1" name="from" ></p>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="to_dt">
<label class="control-label">To Date: </label>
<input type="text" class="form-control" id="datepicker2" name="to" ></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Balance Leaves</label>
<input type="text" class="form-control" name="bal_leave" value="<?php echo $liv->balanced_leaves ?>" >
</div>
<?php }
}
?>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Leaves</label>
<input type="text" class="form-control" name="totaldays" id="totaldays" readonly>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Date</label>
<input type="text" class="form-control" name="todayDate" id="todayDate" value="<?php echo date("d/m/Y"); ?>" readonly>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
<div class="form-group label-floating">
<label class="control-label">Reason </label>
<TEXTAREA class="form-control" id="reason" name="reason"> </TEXTAREA>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" id="submit" name="submit" onclick="<?php // echo base_url() ?>leave_apply_control/save_data">Apply</button>
<button type="reset" class="btn btn-primary" onclick="<?php echo base_url();?>leave_apply_control" >Reset</button>
</form>
leave_Apply_control.php
<?php
class leave_apply_control extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->library('calendar');
$this->load->database();
$this->load->model('user_model');
$this->load->helper('date');
}//contruct close
function index()
{
$id=$this->session->userdata('id');
$data['foren_id'] = $this -> user_model -> get_emp_id($id);
foreach($data['foren_id'] as $fk)
{
//echo "////control page fk is ".$fk->emp_id;
}
$data2['emp_leave'] = $this->user_model->get_leave_count($fk->emp_id);
$this->load->view('leave_application',$data2);
$this->save_data();
}
public function save_data()
{
$fd = date('Y-m-d',strtotime($this->input->post('from')));
$td = date('Y-m-d',strtotime($this->input->post('to')));
$data = array(
'employee_id' => $this->input->post('id'),
'emp_name' => $this->input->post('empname'),
'leave_from' => $fd,
'leave_to' => $td,
'no_of_days' => $this->input->post('totaldays'),
'applied_date' => date('Y-m-d H:i:s'),
'reason' => $this->input->post('reason'),
'mail' => $this->input->post('empmail'),
'leave_type' => $this->input->post('type'),
'balance_leaves' => $this->input->post('bal_leave'),
'status' => 1,
);
$this->user_model->insert_leave($data);
}
}

generate a insert statment with hash

I want to add my id hashed into my hash column in the same insert. I was thiking about adding a hidden , but I think is not okay , however trigger is not necessary for this job , how can I do it in php without adding a new button?
the query it will be generated depending on id.
where md5(id) then isnert in the column , how can I put all together=
table
id hash
1 27669f3f141da48bfe5e6b7aa37c38f9
add controller
public function addProduct(){
$descripcion = $this->input->post('description');
$cost_price = $this->input->post('cost_price');
$selling_price = $this->input->post('selling_price');
$wprice = $this->input->post('wprice');
$min_stock = $this->input->post('min_stock');
$stock = $this->input->post('stock');
$max_stock = $this->input->post('max_stock');
$data = array(
'hash' => $this->hash_id($descripcion),
'descripcion' => $descripcion,
'precio_compra' => $cost_price,
'precio_venta' => $selling_price,
'precio_mayoreo' => $wprice,
'existencia_minima' => $min_stock,
'existencia' => $stock,
'existencia_maxima' => $max_stock
);
if ($data['existencia'] > $data['existencia_minima']) {
$this->json(array('min_stock' => 'el stock no puede ser mayor al min'));
}elseif ($data['existencia_maxima'] < $data['existencia']) {
$this->json(array('max_stock' => 'el stock no puede ser mayor al max'));
}else{
if (!$this->products->isExistsProduct($data)) {
$this->products->addProduct($data);
$this->json(array('msg' => 'successfully added'));
}else{
$this->json(array('duplicate' => 'product already exists'));
}
}
}
model
public function addProduct($data){
$this->db->insert('storelte_articulos',$data);
}
form
<form id="update_product">
<div class="row">
<div class="form-group">
<div class="col-xs-12">
<label for="description">Name: </label>
<input type="text" class="form-control" id="description" name="description" title="product description" required>
<div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="cost_price">Cost Price: </label>
<div class="input-group"> <span class="input-group-addon">$</span>
<input type="text" class="form-control input-group-lg reg_name" id="cost_price" name="cost_price" title="cost_price" placeholder="Last name" required>
</div>
</div>
<div class="col-xs-8 col-sm-6">
<label for="selling_price">Selling price: </label>
<input type="text" class="form-control input-group-lg reg_name" id="selling_price" name="selling_price" title="selling_price" placeholder="Last name" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="wprice">Wholeprice: </label>
<input type="text" class="form-control" id="wprice" name="wprice" title="wprice" required>
</div>
<div class="col-xs-8 col-sm-6">
<label for="min_stock">Min stock: </label>
<input type="text" class="form-control" id="min_stock" name="min_stock" title="min_stock" required>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-8 col-sm-6">
<label for="stock">Stock: </label>
<input type="text" class="form-control" id="stock" name="stock" title="stock" required>
</div>
<div class="col-xs-8 col-sm-6">
<label for="max_stock">Max stock: </label>
<input type="text" class="form-control" id="max_stock" name="max_stock" title="max_stock" required>
</div>
</div>
</div>
</form>
get the inserted id using this method
$this->db->insert_id()
and updated the data (hash)

Using CodeIgniter, how to find out if a record already exists in a database?

I have to check whether the email id and phone not already updated in the database or not.
Can you please help me to find solution for this?
As simple as
$this->form_validation->set_rules('username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[TABLE NAME.FIELD
NAME]');
Controller code:
public function add_lead()
{
if($this->session->userdata('logged_in') && ($this->session->userdata('role')=='support'))
{
date_default_timezone_set('Asia/Calcutta');
$id='';
$id= $this->uri->segment(4);
$data = array(
'title' =>"add_lead",
'name' => $this->input->post('name'),
'company' => $this->input->post('company'),
'company_type' => $this->input->post('company_type'),
'email1' => $this->input->post('email1'),
'email2' => $this->input->post('email2'),
'phone1' => $this->input->post('phone1'),
'phone2' => $this->input->post('phone2'),
'website_url' => $this->input->post('website_url'),
'fb_url' => $this->input->post('fb_url'),
'city' => $this->input->post('city'),
'other_info' => $this->input->post('other_info'),
'status'=>'Active'
);
$rul = array(
array('field' => 'name', 'label' => 'Name', 'rules' => 'required'),
//array('field' => 'company_logo', 'label' => 'Company Logo', 'rules' => 'required')
);
$email1=$this->input->get("email1");
$this->form_validation->set_rules($rul);
$this->form_validation->set_rules('email1', 'email1', 'callback_rolekey_exists');
if ($this->form_validation->run() == FALSE)
{
$this->load->view($this->support_header,$data);
$this->load->view($this->add_lead,$data);
$this->load->view($this->support_footer);
}
else
{
$unique=getUniqueID(12);
$unique_id= getUniqueID(6);
if(!isset($error))
{
if(isset($_POST['submit']))
{
$ser=mysql_query("SELECT COUNT(*) as count from support_leads where email1='".$email1."' ");
$count=mysql_fetch_assoc($ser);
$count=$count['count'];
if($count==1)
{
echo 'email already existing';
}
}
else{
$fields = array(
'name' => $this->input->post('name'),
'company' => $this->input->post('company'),
'company_type' => $this->input->post('company_type'),
'email1' => $this->input->post('email1'),
'email2' => $this->input->post('email2'),
'phone1' => $this->input->post('phone1'),
'phone2' => $this->input->post('phone2'),
'website_url' => $this->input->post('website_url'),
'fb_url' => $this->input->post('fb_url'),
'city' => $this->input->post('city'),
'others' => $this->input->post('other_info'),
'created_date' => date("Y-m-d H:i:s"),
'lead_status' => 'New',
'status'=>'Active'
);
$this->am->insertdata("support_leads",$fields);
redirect('support/leads/');
}
}
else
{
$this->load->view($this->support_header,$data);
$this->load->view($this->add_lead,$data);
$this->load->view($this->support_footer);
}
}
}
else
{
$this->login();
}
}
View Code:
<div>
<ul class="breadcrumb">
<li>
Dashboard <span class="divider">/</span>
</li>
<li>
<a href="<?php echo site_url('support/add_lead/');?>" >Add Lead</a>
</li>
</ul>
</div>
<div class="row-fluid sortable">
<div class="box span12">
<div class="box-header well" data-original-title>
<h2><i class="icon-edit"></i> <?php echo $title;?></h2>
</div>
<div class="box-content" style="color:#F00">
<?php if(!empty($msg))
{
echo $msg;
}
?>
</div>
<div class="box-content">
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
<fieldset>
<div class="control-group">
<label class="control-label" for="inputSuccess">Name</label>
<div class="controls">
<input type="text" id="name" name="name" value="<?php echo $name; ?>" required/>
<?php if(form_error('name')!='') { ?>
<div class="error-left"></div>
<div class="error-inner"><?php echo form_error('name'); ?></div><?php } ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Company</label>
<div class="controls">
<input type="text" id="company" name="company" value="<?php echo $company; ?>" />
<?php if(form_error('company')!='') { ?>
<div class="error-left"></div>
<div class="error-inner"><?php echo form_error('company'); ?></div><?php } ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess"> Company Type </label>
<div class="controls">
<select name="company_type" class="" style="" >
<option value="">Select Type</option>
<option value="E-commerce" <?php if($company_type == 'E-commerce') echo "selected";?> > E-commerce </option>
<option value="SME" <?php if($company_type == 'SME') echo "selected";?> > SME </option>
<option value="Enterprise" <?php if($company_type == 'Enterprise') echo "selected";?> > Enterprise </option>
<option value="Individual" <?php if($company_type == 'Individual') echo "selected";?> > Individual </option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Email 1</label>
<div class="controls">
<input type="text" id="email1" name="email1" value="<?php echo $email1; ?>" />
<?php if(form_error('email1')!='') { ?>
<div class="error-left"></div>
<div class="error-inner"><?php echo form_error('email1'); ?></div><?php } ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Email 2</label>
<div class="controls">
<input type="text" id="email2" name="email2" value="<?php echo $email2; ?>" />
<?php if(form_error('email2')!='') { ?>
<div class="error-left"></div>
<div class="error-inner"><?php echo form_error('email2'); ?></div><?php } ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Phone 1</label>
<div class="controls">
<input type="text" id="phone1" name="phone1" value="<?php echo $phone1; ?>" />
<?php if(form_error('phone1')!='') { ?>
<div class="error-left"></div>
<div class="error-inner"><?php echo form_error('phone1'); ?></div><?php } ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Phone 2</label>
<div class="controls">
<input type="text" id="phone2" name="phone2" value="<?php echo $phone2; ?>" />
<?php if(form_error('phone2')!='') { ?>
<div class="error-left"></div>
<div class="error-inner"><?php echo form_error('phone2'); ?></div><?php } ?>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">website URL </label>
<div class="controls">
<input type="text" id="website_url" name="website_url" value="<?php echo $website_url; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Facebook URL </label>
<div class="controls">
<input type="text" id="fb_url" name="fb_url" value="<?php echo $fb_url; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">City </label>
<div class="controls">
<input type="text" id="city" name="city" value="<?php echo $city; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputSuccess">Other Info</label>
<div class="controls">
<textarea name="other_info" style="height:130px;" ></textarea>
</div>
</div>
<div class="form-actions">
<?php if(isset($_REQUEST['id']))
{?>
<input type="hidden" value="<?php echo $_REQUEST['id']; ?>" name="id" />
<input type="submit" value="Update Changes" name="submit" class="btn btn-primary" />
<?php }
else {?>
<input type="submit" id="submit" value="Add Changes" name="submit" class="btn btn-primary" />
<?php }?>
<!-- <input type="hidden" value="<?php echo $user_id; ?>" name="user_id" />-->
<input type="reset" value="Cancel" class="btn btn-primary" />
</div>
</fieldset>
</form>
</div>
</div><!--/span-->
</div><!--/row-->

Password field is not arriving to php using ajax

So i am having an issue debugging where the issue is. Could be Jquery or PHP issue. I have done quite a bit of alert testing and i believe the issue lies in the if statement of ajax, eg i am posting pass if the field is not disabled when posting. Because when doing a echo $_POST['pass'] and alert(data), i get nothing. So i believe the php is working fine please tell me where the issue lies and if you know also the remedy to this situation. Thanks Guys.
Ajax
$(document).on('submit', '#editaccsount', function(event) {
event.preventDefault();
myData = {
contactname: $('input[name=contactname]').val(),
business: $('input[name=business]').val(),
email: $('input[name=email]').val(),
code: $('input[name=code]').val(),
phone: $('input[name=phone]').val(),
priceband: $('input[name=priceband]').val(),
address: $('input[name=address]').val(),
active: $('input[name=active]').val(),
mon: $('input[name=mon]').val(),
tue: $('input[name=tue]').val(),
wed: $('input[name=wed]').val(),
thu: $('input[name=thu]').val(),
fri: $('input[name=fri]').val(),
sat: $('input[name=sat]').val(),
sund: $('input[name=sund]').val(),
adminname: $('input[name=adminname]').val(),
accountid: $('input[name=accountid]').val(),
isadmin: $('input[name=isadmin]').val(),
};
var isDisabled = $('input[name=pass]').prop('disabled');
if (isDisabled == false) {
myData.pass = $('input[name=pass]').val();
}
$.ajax({
url: 'php/editaccount.php',
type: "POST",
data: myData,
success: function(data) {
if ($('input[name=isadmin]').val() == 1) {
$('input[name=accountsearch]').val($('input[name=email]').val());
$('input[name=accountsearch]').submit();
} else {
$('input[name=accountsearch]').val($('input[name=business]').val());
$('input[name=accountsearch]').submit();
}
alert(data);
}
});
});
This is my PHP that does all the updating based on conditions:
<?php
require '../../core/init.php';
$auth = new Auth();
$accid = $_POST['accountid'];
echo $accid, '<br/>';
echo 'Password is: ', $_POST['pass'], '<br/>';
echo 'Is Admin: ', $_POST['isadmin'];
if (!empty($_POST['pass'])) {
echo 'Pass Detected';
echo 'Password: ', $_POST['pass'], "<br/>";
$salt = $auth->randomString();
$newpass = $salt . $_POST['pass'];
$newpass = $auth->hashData($newpass);
if ($_POST['isadmin'] == 0) {
$customer = DBPDO::getInstance()->update('customer', $accid, array(
'businessName' => $_POST['business'],
'contactName' => $_POST['contactname'],
'email' => $_POST['email'],
'code' => $_POST['code'],
'phone' => $_POST['phone'],
'priceBand' => $_POST['priceband'],
'deliveryAddress' => $_POST['address'],
'is_active' => $_POST['active'],
'mon' => $_POST['mon'],
'tue' => $_POST['tue'],
'wed' => $_POST['wed'],
'thu' => $_POST['thu'],
'fri' => $_POST['fri'],
'sat' => $_POST['sat'],
'sun' => $_POST['sund'],
'password' => $newpass,
'user_salt' => $salt
));
} elseif ($_POST['isadmin'] == 1) {
$admin = DBPDO::getInstance()->update('admin', $accid, array(
'adminName' => $_POST['adminname'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'password' => $newpass,
'user_salt' => $salt
));
}
} elseif (empty($_POST['pass'])) {
echo 'Pass NOT detected';
if ($_POST['isadmin'] == 0) {
$customer = DBPDO::getInstance()->update('customer', $accid, array(
'businessName' => $_POST['business'],
'contactName' => $_POST['contactname'],
'email' => $_POST['email'],
'code' => $_POST['code'],
'phone' => $_POST['phone'],
'priceBand' => $_POST['priceband'],
'deliveryAddress' => $_POST['address'],
'mon' => $_POST['mon'],
'tue' => $_POST['tue'],
'wed' => $_POST['wed'],
'thu' => $_POST['thu'],
'fri' => $_POST['fri'],
'sat' => $_POST['sat'],
'sun' => $_POST['sund']
));
} elseif ($_POST['isadmin'] == 1) {
$admin = DBPDO::getInstance()->update('admin', $accid, array(
'adminName' => $_POST['adminname'],
'email' => $_POST['email'],
'phone' => $_POST['phone']
));
}
}
What gets outputted as HTML:
<?php
require '../../core/init.php';
$data['result_1'] = '';
$searchquery = $_POST['accountsearch'];
if ($_POST['accounttype'] == 'Users') {
$customer = DBPDO::getInstance()->get('customer', array(
array('businessName', '=', "'$searchquery'" )
));
foreach ($customer->results() as $row)
{
$data['result_1'] .= '
<div class="row row-centered">
<input class="btn btn-lg btn-primary" type="button" name="editbtn" value="Edit Account">
<input class="btn btn-lg btn-danger remove-btn" type="button" name="removebtn" value="Remove Account" uid="'.$row->id.'">
</div><br/>
<form id="editaccount" action="php/editaccount.php" method="POST">
<h3>General Details</h3>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="contactname">Customer Name</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="contactname" class="form-control" value="'.$row->contactName.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="business">Business</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="business" class="form-control" value="'.$row->businessName.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="email">Email</label>
<div class="input-group input-group-lg">
<input type="email" disabled="disabled" name="email" class="form-control" value="'.$row->email.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="code">Code</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="code" class="form-control" value="'.$row->code.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="phone">Phone</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="phone" class="form-control" value="'.$row->phone.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="priceband">Price Band</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="priceband" class="form-control" value="'.$row->priceBand.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="address">Delivery Address</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="address" class="form-control" value="'.$row->deliveryAddress.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="pass">Active</label>
<div class="input-group input-group-lg">
<input type="test" disabled="disabled" name="active" class="form-control" value="'.$row->is_active.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="pass">Password</label>
<div class="input-group input-group-lg">
<input type="password" disabled="disabled" name="userpass" class="form-control" value="'.$row->password.'">
</div>
<div class="input-group input-group-lg">
<input id="editpasswordbtn" class="btn btn-sm btn-primary" type="button" value="Edit Password">
</div>
</div>
</div>
</div>
<div class="wrning" style="color:red;display:none;">
<b>Warning: </b> If you do NOT want to edit Password please click Cancel!<br/><br/>
<input type="button" class="btn btn-sm btn-danger" value="Cancel Password Change">
</div>
<br/><br/>
<h3>Standing Order Details</h3>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="mon">Monday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="mon" class="form-control" value="'.$row->mon.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="tue">Tuesday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="tue" class="form-control" value="'.$row->tue.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="wed">Wednesday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="wed" class="form-control" value="'.$row->wed.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="thu">Thursday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="thu" class="form-control" value="'.$row->thu.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="fri">Friday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="fri" class="form-control" value="'.$row->fri.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="sat">Saturday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="sat" class="form-control" value="'.$row->sat.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="sund">Sunday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="sund" class="form-control" value="'.$row->sun.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<input type="hidden" name="accountid" value="'.$row->id.'">
<input type="hidden" name="isadmin" value="0">
<input class="btn btn-lg btn-rj" disabled="disabled" type="submit" name="editbtn" value="Send to Database">
</div>
</form>
';
}
} elseif ($_POST['accounttype'] == 'Admins') {
$customer = DBPDO::getInstance()->get('admin', array(
array('email', '=', "'$searchquery'" )
));
foreach ($customer->results() as $row)
{
$data['result_1'] .= '
<div class="row row-centered">
<input class="btn btn-lg btn-primary" type="button" name="editbtn" value="Edit Account">
<input class="btn btn-lg btn-danger remove-btn adminremove-btn" type="button" name="adminremovebtn" value="Remove Account" uid="'.$row->id.'">
</div><br/>
<form id="editaccount" action="php/editaccount.php" method="POST">
<h3>General Details</h3>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="contactname">Admin Name</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="adminname" class="form-control" value="'.$row->adminName.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="contactname">Email</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="email" class="form-control" value="'.$row->email.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="business">Phone</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="phone" class="form-control" value="'.$row->phone.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="email">Email</label>
<div class="input-group input-group-lg">
<input type="hidden" name="isadmin" value="1">
<input type="password" disabled="disabled" name="pass" class="form-control" value="'.$row->password.'">
</div>
</div>
</div>
<div class="row row-centered">
<input type="hidden" name="accountid" value="'.$row- >id.'">
<input type="hidden" name="isadmin" value="1">
<input class="btn btn-lg btn-rj" disabled="disabled" type="submit" name="editbtn" value="Send to Database">
</div>
</div>
</form>
';
}
}
echo json_encode($data);
exit;
?>
You are checking the property of disabled with:
var isDisabled = $('input[name=pass]').prop('disabled');
If I recall this will return a string empty or disabled as a way to use this would be:
$('input[name=pass]').prop('disabled', 'disabled');
If you're looking for a true/false maybe try with:
var isDisabled = $('input[name=pass]').is(':disabled');
and it will return true or false.

Categories