Calling controllers different methods from view page using codeigniter - php

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);
}
}

Related

Update Page without reload AJAX/PHP

I'm having some troble with AJAX call to update a page after insert the info in the DB.
I update the form, but i need to update the form after the update.
Need some help, i'm not so good whit javascript.
Thks!
<script>
function chk()
{
var nome=document.getElementById('nome').value;
var dataString= 'nome='+ nome;
$.ajax({
type:"post",
url: default,
data:dataString,
cache:false
//success: function(html){
// $('#msg').html(html);
//}
});
return false;
}
</script>
All the code i use, it's not final and not uptimized, its a form, update the field, and i want to update with the changes, without make a page refresh
<div class="col-md-12 personal-info">
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<label class="col-lg-4 control-label">Nome:</label>
<div class="col-lg-8">
<input class="form-control" type="text" id="nome" name="nome" value="<?php echo name($resultado); ?>">
<!--<input class="form-control" type="text" value="<?php $nome; ?>">-->
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Apelido:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo apelido($chamada); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Morada:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo morada(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Código Postal:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo codigopostal(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Localidade:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo localidade(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="<?php echo email(); ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Username:</label>
<div class="col-md-8">
<input class="form-control" type="text" value="<?php echo $current_user->user_login ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password:</label>
<div class="col-md-8">
<input class="form-control" type="password" value="11111122333">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Confirm password:</label>
<div class="col-md-8">
<input class="form-control" type="password" value="11111122333">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-8">
<input type="submit" name="submit" class="btn btn-primary" value="Save Changes" onclick="return chk()">
<!--<input type="submit" name="submit" class="btn btn-primary" value="Save Changes">-->
<span></span>
<input type="reset" name="reset" class="btn btn-default" value="Cancel">
</div>
</div>
</form>
</div>
</div>
</div>
<hr>
<!--teste final-->
<?php
global $wpdb;
global $seconddb;
global $current_user;
if ($_POST['submit']){
$new = $_POST['nome'];
$query = $seconddb->query("UPDATE {$wpdb->prefix}sgc_socios SET nome='$new' WHERE email = '{$current_user->user_email}' ");
}
?>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
function chk()
{
var nome=document.getElementById('nome').value;
var dataString= 'nome='+ nome;
$.ajax({
type:"post",
url: default,
data:dataString,
cache:false
//success: function(html){
// $('#msg').html(html);
//}
});
return false;
}
</script>
Thks for the help!

File uploading issue in Codeigniter

I have this error in controller
A PHP Error was encountered
Severity: Notice
Message: Undefined property: dashboardController::$upload
Filename: controllers/dashboardController.php
Line Number: 70
Call to a member function data() on a non-object in
C:\xampp\htdocs\High_tack\application\controllers\dashboardC‌​ontroller.php
on line 71
just do as i done it in my project using wamp and codeigniter. I uploaded my pictures directly into the folder named uploads in the project folder.
here is my controller named addproduct,
<?php class Addproduct extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('addproduct');
}
function save() {
$name_file = $_FILES['pro_filename']['name'];
//$user=$this->session->userdata['logged_in'];
$data=array (
'pro_name'=>$this->input->post('pro_name'),
'pro_code'=>$this->input->post('pro_code'),
'hsn_code'=>$this->input->post('hsn_code'),
'pro_price'=>$this->input->post('pro_price'),
'pro_tax1'=>$this->input->post('pro_tax1'),
'pro_tax2'=>$this->input->post('pro_tax2'),
'pro_tax3'=>$this->input->post('pro_tax3'),
'pro_tax4'=>$this->input->post('pro_tax4'),
'pro_description'=>$this->input->post('pro_description'),
'pro_brand'=>$this->input->post('pro_brand'),
'pro_category'=>$this->input->post('pro_category'),
'pro_scategory'=>$this->input->post('pro_scategory'),
'pro_sscategory'=>$this->input->post('pro_sscategory'),
'pro_qauntity'=>$this->input->post('pro_qauntity'),
'pro_service'=>$this->input->post('pro_service'),
'pro_certificate'=>$this->input->post('pro_certificate'),
'pro_filename'=>$name_file,
'pro_date'=>$this->input->post('pro_date')
);
$this->load->model('base_model');
$this->base_model->save('addproduct',$data);
$this->do_upload();
$this->load->view('addproduct');
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|gif|jpeg';
$config['max_size'] = 500000;
$config['max_width'] = 200000;
$config['max_height'] = 200000;
$this->load->library('upload', $config);
$this->upload->do_upload('pro_filename');
}
}
here is my view page named addproduct
<div class="panel-body">
<div class="form">
<div class="col-md-12 sign-up text-left">
<div class="form-group ">
<label for="pro_name" class="control-label col-lg-3">Product Name</label>
<div class="col-lg-6">
<input class="form-control " id="pro_name" name="pro_name" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_code" class="control-label col-lg-3">Product Code</label>
<div class="col-lg-6">
<input class="form-control " id="pro_code" name="pro_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="hsn_code" class="control-label col-lg-3">HSN Code</label>
<div class="col-lg-6">
<input class="form-control " id="hsn_code" name="hsn_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_price" class="control-label col-lg-3">Prize</label>
<div class="col-lg-6">
<input class="form-control " id="pro_price" name="pro_price" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax1" class="control-label col-lg-3">Tax 1</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax1" name="pro_tax1" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax2" class="control-label col-lg-3">Tax 2</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax2" name="pro_tax2" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax3" class="control-label col-lg-3">Tax 3</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax3" name="pro_tax3" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax4" class="control-label col-lg-3">Tax 4</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax4" name="pro_tax4" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_description" class="control-label col-lg-3">Description</label>
<div class="col-lg-6">
<input class="form-control " id="pro_description" name="pro_description" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_brand" class="control-label col-lg-3">Brand</label>
<div class="col-lg-6">
<input class="form-control " id="pro_brand" name="pro_brand" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_category" class="control-label col-lg-3">Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_category" name="pro_category" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_scategory" class="control-label col-lg-3">Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_scategory" name="pro_scategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_sscategory" class="control-label col-lg-3">S-Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_sscategory" name="pro_sscategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_quantity" class="control-label col-lg-3">Qauntity</label>
<div class="col-lg-6">
<input class="form-control " id="pro_quantity" name="pro_quantity" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_service" class="control-label col-lg-3">Service Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_service" name="pro_service" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_certificate" class="control-label col-lg-3">Certificate Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_certificate" name="pro_certificate" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_filename" class="control-label col-lg-3">Photo:</label>
<div class="col-lg-6">
<input class="form-control" type="file" name="pro_filename" required/>
</div>
</div><br><br>
<div class="clearfix"></div>
<?php
$today = date("Y-m-d");
?>
<input type="hidden" name="pro_date" value="<?php echo $today ?>">
<div class="form-group">
<div class="col-lg-offset-3 col-lg-6">
<button class="btn btn-primary" type="submit">Save</button>
<button class="btn btn-default" >Cancel</button>
</div>
</div>
<br>
</div>
<br>
</div>
</div>
this is my code in model named base_model
public function save($table, $data) {
return $this->db->insert($table, $data);
}
delete the portion you dont want, if you dont need to upload the image to folder simply change the controller field where the upload occurs
'pro_filename'=>$this->input->post('pro_filename'),

Codeigniter insert not working Database error occured

I have following code tried out.
class Vendor extends CI_Controller{
function __construct(){
parent::__construct();
}
public function vendor_add(){
$this->load->helper('url');
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('add_vendor');
$this->load->view('footer');
}
public function save(){
$this->load->helper(array('form','url'));
$this->load->model('Save_vendor');
$vendor_data = array(
'ven_name'=> $this->input->post['firstName'],
'ven_shop'=>$this->input->post['companyName'],
'ven_email_id'=>$this->input->post['email'],
'ven_contactno'=>$this->input->post['contactno'],
'ven_othercontactno'=>$this->input->post['contactno2'],
'ven_commission'=>$this->input->post['assigncommission'],
'ven_accname'=>$this->input->post['bankaccname'],
'ven_accountno'=>$this->input->post['bankaccno'],
'ven_ifsccode'=>$this->input->post['ifsccode'],
'ven_type'=>$this->input->post['optionsRadios']
);
$this->Save_vendor->vendor_insertdata($vendor_data);
redirect(base_url().'/vendor/vendor_add');
}
}
/* Model File */
class Save_vendor extends CI_Model{
public function vendor_insertdata($vendor_data){
$this->db->insert('tbl_vendor',$vendor_data);
}
/* VIew FIle */
<!-- BEGIN CONTENT -->
<div class="page-content-wrapper">
<div class="page-content">
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<!-- BEGIN PAGE HEADER-->
<h3 class="page-title">
Add Vendor <small> Add New Vendor</small>
</h3>
<div class="row">
<div class="col-md-12">
<div class="portlet-body form">
<?php echo base_url();?>
<!-- BEGIN FORM-->
<form action="<?php echo base_url("index.php/Vendor/save") ?>" class="horizontal-form" method="post">
<div class="form-body">
<h3 class="form-section">Add Vendor</h3>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<div class="radio-list">
<label class="radio-inline">
<input type="radio" name="optionsRadios" id="1" value="1" checked> Shop Vendor </label>
<label class="radio-inline">
<input type="radio" name="optionsRadios" id="2" value="2"> Vendor </label>
</div>
</div>
</div>
<!--/span-->
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Vendor Name<span class="required">*</span></label>
<input type="text" id="firstName" class="form-control" name="firstName">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Company/Shop/Business Name<span class="required">*</span></label>
<input type="text" id="companyName" class="form-control" name="companyName" >
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Email Id<span class="required">*</span></label>
<input type="text" id="email" class="form-control" name="email">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Contact No<span class="required">*</span></label>
<input type="text" id="firstName" class="form-control" name="contactno" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">2nd Contact No</label>
<input type="text" id="firstName" class="form-control" name="contactno2">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">PIN Code<span class="required">*</span></label>
<input type="text" id="pincode" class="form-control" name="pincode" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Assign Commision %<span class="required">*</span></label>
<input type="text" id="assigncommission" class="form-control" name="assigncommission">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">BANK Account Name<span class="required">*</span></label>
<input type="text" id="firstName" class="form-control" name="bankaccname" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Account Number<span class="required">*</span></label>
<input type="text" id="bankaccno" class="form-control" name="bankaccno" >
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">IFSC Code<span class="required">*</span></label>
<input type="text" id="ifsccode" class="form-control" name="ifsccode">
</div>
</div>
</div>
<div class="form-actions right">
<button type="submit" class="btn yellow-crusta"> Add Vendor</button>
</div>
</form>
</div>
</div>
</div>
<!-- END CONTENT -->
</div>
<!-- END CONTAINER -->
Above code but it will give an error Database error occured. Column name not be null. I am beginner in codeigniter please any suggestions for it.
I want to add data in my database.but it is not working.
Check your table "tbl_vendor" in which you are inserting values. One or more field is defined as NOT NULL means it will not accept empty value. You have to pass value to that field which is marked as NOT NULL.
Please read Codeigniter Query Builder reference first. You're making big mistakes. Your code should be like below:
$vendor_data = array(
'ven_name'=> $this->input->post['firstName'],
'ven_shop'=>$this->input->post['companyName'],
'ven_email_id'=>$this->input->post['email'],
'ven_contactno'=>$this->input->post['contactno'],
'ven_othercontactno'=>$this->input->post['contactno2'],
'ven_commission'=>$this->input->post['assigncommission'],
'ven_accname'=>$this->input->post['bankaccname'],
'ven_accountno'=>$this->input->post['bankaccno'],
'ven_ifsccode'=>$this->input->post['ifsccode'],
'ven_type'=>$this->input->post['optionsRadios']
);
$this->db->insert('your_table_name', $vendor_data);

Cannot add or update a child row 'user_id'

When I submit my form I can see in the error that all my values are passed except 'user_id':
('insert into `products` (`title`, `body`, `address`, `condition`, `quantity`, `price`, `bid_price`, `img_1`, `updated_at`, `created_at`)
But there's missing 'user_id', why ? This is my Product.php
protected $fillable = ['slug', 'title', 'body', 'user_id', 'price', 'bid_price', 'address', 'condition', 'quantity', 'img_1', 'img_2', 'img_3', 'img_4', 'views'];
The function in controller:
public function storeAuction(AuctionSubmitRequest $request)
{
$product = Product::create($request->all());
Auth::user()->product()->save($product);
$request->file('img_1')->move(public_path('images'), $request->file('img_1')->getClientOriginalName());
$data = $request->except(['img_1']);
$data['img_1'] = public_path('images') . '/' . $request->file('img_1')->getClientOriginalName();
$product->create($data);
return redirect('/');
}
My form:
<form id="tryitForm" class="form-horizontal" method="POST" action="/paskelbti-nauja-aukciona" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="form-group">
<label class="col-md-3 control-label">Prekės pavadinimas</label>
<div class="col-md-4">
<input type="text" class="form-control" name="title" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės aprašymas</label>
<div class="col-md-4">
<textarea name="body" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Adresas</label>
<div class="col-md-6">
<input type="text" class="form-control" name="address" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Pristatymas</label>
<div class="col-md-6">
<div class="radio">
<label><input type="radio" name="dispatch" value="kurjeris" /> Kurjeriu</label>
</div>
<div class="radio">
<label><input type="radio" name="dispatch" value="pastas" /> Paštu</label>
</div>
<div class="radio">
<label><input type="radio" name="dispatch" value="kita" /> Kita (parašysiu "prekės aprašyme")</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės būklė</label>
<div class="col-md-6">
<div class="radio">
<label><input type="radio" name="condition" /> Nauja</label>
</div>
<div class="radio">
<label><input type="radio" name="condition" /> Labai gera</label>
</div>
<div class="radio">
<label><input type="radio" name="condition" /> Gera</label>
</div>
<div class="radio">
<label><input type="radio" name="condition" /> Panaudota</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Barkodas (jei turite)</label>
<div class="col-md-4">
<input class="form-control" name="barcode" type="number" min="0" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekių kiekis</label>
<div class="col-md-2">
<input class="form-control" name="quantity" type="number" min="1" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės nuotraukos</label>
<div class="col-md-6">
<input type="file" class="form-control" name="img_1" /><br>
<input type="file" class="form-control" name="img_2" /><br>
<input type="file" class="form-control" name="img_3" /><br>
<input type="file" class="form-control" name="img_4" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės kaina</label>
<div class="col-md-2">
<input class="form-control" name="price" type="number" step="0.01" min="0.01" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Minimali siūlymo suma</label>
<div class="col-md-2">
<input class="form-control" name="bid_price" type="number" step="0.01" min="0.01" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-8">
<button type="submit" class="btn btn-primary">Pradėti aukcioną</button>
</div>
</div>
</form>
You may try something like the following.
public function storeAuction(AuctionSubmitRequest $request)
{
$request->file('img_1')->move(public_path('images'), $request->file('img_1')->getClientOriginalName());
// first create the related model, do not save or create
// because you will get a 'Cannot add or update a child row'
$product = new Product();
$product->property1 = $request->get('property1');
$product->property2 = $request->get('property2');
// ...
$product->img_1 = public_path('images') . '/' . $request->file('img_1')->getClientOriginalName();
// here you save a product associated to the user.
Auth::user()->product()->save($product);
return redirect('/');
}

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