When updating password I am getting error in Laravel - ajax

I am trying to update a user password, but I am getting an error.
Syntax error or access violation: 1064 You have an error in your SQL
syntax; 'where name is null' at line 1 (SQL: update users set
where name is null)
Controller
public function passwordupdate(Request $request, $id)
{
$user = User::find($id);
$user->password = Hash::make($request->password);
$user->save();
return response()->json([
'msg' => 'Password has been Updated',
]);
}
Route
Route::post('teachers/policy', 'UsersController#passwordupdate')
From the view, I am sending the user id and name to the controller through an Ajax request.
Ajax
$('.update-password').on('click', function (e) {
let user_id = $('#update-password').val();
console.log('Update password clicked!')
e.preventDefault();
$.ajax({
type: "POST",
dataType: 'json',
data: $(this).serialize(),
url: "/users/" + $('#update-password').attr("value"),
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function (data) {
if (data.msg) {
$('#response').empty();
$(".toast").toast('show');
$('#response').append(data.msg);
}
}
});
});
View
<form id="password-form">
<input type="hidden" value="">
<table>
<tr>
<td class="text-nowrap">
<select name="name" id="username" class="form-control border border-secondary border-dark" required>
<option selected value="">SELECT USER</option>
#foreach($users as $user)
<option value="{{ $user->name }}"> {{ $user->name }}</option>
#endforeach
</select>
</td>
<td class="text-nowrap">
<input type="text" name="password" placeholder="Type Password"
class="form-control border border-secondary border-dark">
</td>
<td class="text-nowrap">
<button type="submit" class="btn btn-danger btn-sm update-password rounded text-center"
value="{{ $user->id }}" id="update-password">
<span class="fa fa-key"> </span> UPDATE PASSWORD
</button>
</td>
</tr>
</table>
</form>

Change id 'update-password' to 'update_password'
Ids should not contain dash(-)

You are accessing this endpoint :
/users/" + $('#update-password').attr("value")
But there is no $id as a url parameter in this endpoint, so you don't retrieve your User
What could work to get the current logged in users details is:
Auth::user()->id;
Auth class will return you the current logged in user details

i think you should try this
$('#update-password').val()
make sure that id "update-password" place in your input type["password"]
hope answer your question

Related

How to display data to textfield from combo selection in laravel

I'd like to display data to textfield based on combo/drop box selection in laravel 5.8. then save the data. is there complete tutorial how to do it from model, view and controller.
In Customer Controller
public function create()
{
$customer= Customer::all();
return view('Customer.create', compact('Customer'));
}
in view create.blade
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>CUSTOMER</strong>
<select type="text" class="form-control" name="kde_cust" id="cde_customer" required>
<option value=""> -- PICK CUST-- </option>
#foreach($cust as $cust)
<option value="{{ $cust->cde_cust }}" selected>{{ $cust->nme_customer }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>NAME</strong>
<input type="text" name="cust_name" id="cust_name" class="form-control" value="{{ $cust->nme_customer }}">
</div>
</div>
This should helps:
Create a route to get the user address:
Route::get('user-address/{user_id}','YourController#method')->name('user.address');
Your controller method should look like this:
public function yourFunction($cde_customer)
{
$customer = Customer::where('cde_customer', $cde_customer)->first();
$address = $customer->address;
return response()->json([
'address' => $address,
]);
}
Add this script to your blade:
$("#cde_customer").on('change', function(){
var user_id = $(this).val();
$.ajax({
url: '/user-address/' + user_id,
method: 'GET',
success: function(response){
$("#textfield").html(response.address);
}
});
});
Hope it helps.

Dependent dropdown using ajax in laravel

I have a dropdown for students_id. Now I want to track the student name and class from another table when someone select any student id. I did something for that using ajax. Its not showing me any error in the console but just giving me null array and I think thats the problem related to the query. You can check my code below. :)
//Controller
public function create()
{
$students = Student::pluck('student_id', 'id')->all();
return view('admin.reports.create', compact('students'));
}
public function reportsAjax(Request $request) {
$students = DB::table("students")->where("student_id", $request->student_id)->pluck("student_id","id");
return json_encode($students);
}
//View and Ajax
<div class="form-group">
<label for="title">Select Student <span style="color: red">*</span></label>
<select name="student_id" class="form-control bg-dark text-white" >
<option>Select Student</option>
#foreach ($students as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label class="control-label">Student Full Name <span style="color: red">*</span></label>
<input name="std_name" type="text" required class="form-control" />
</div>
<div class="form-group">
<label class="control-label">Class <span style="color: red">*</span></label>
<input name="std_class" type="text" required class="form-control" />
</div>
<script type="text/javascript">
$(document).ready(function() {
$('select[name="student_id"]').on('change', function() {
var studentInfo = $(this).val();
if(studentInfo) {
$.ajax({
url: '/reports/ajax/'+studentInfo,
type: "GET",
dataType: "json",
success:function(data) {
$('input[name="std_name"]').empty();
$.each(data, function(key, value) {
$('select[name="std_name"]').append('<input value="'+ key +'">'+ value +'/>');
});
}
});
}else{
$('select[name="std_name"]').empty();
}
});
});
</script>
//Routes
Route::get('reports/ajax/{id}',array('as'=>'reports.ajax','uses'=>'ReportController#reportsAjax'));
Route::resource('admin/reports', 'ReportController', ['names'=>[
'index'=>'admin.reports.index',
'create'=>'admin.reports.create',
]]);
Try this for your queries:
public function create()
{
$students = Student::all()->only(['student_id', 'id']);
return view('admin.reports.create', compact('students'));
}
public function reportsAjax(Request $request)
{
$students = DB::table('students')->where('student_id', $request->student_id)->only(['student_id','id']);
return response()->json($students, 200);
}

Laravel ajax only the first button excute the other button will not response

I need your help guys im stuck because when i click the first button it will execute but the other button it will not excute im using ajax for the form and i use foreach. If someone know how to solve please tell me.
This is my current view code:
<tbody>
#foreach($customers as $name => $member)
#foreach($member->slice(0,1) as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->MATNR}}</td>
<td>{{$value->MAKTX}}</td>
<td>
<select class="form-control">
<option>{{$value->UNIT1}}({{$value->CONV1}})</option>
<option>{{$value->UNIT2}}({{$value->CONV2}})</option>
</select>
</td>
<td>
<select class="form-control">
#foreach($member as $value)
<option value="{{$value->CHARG}}">{{$value->CHARG}}</option>
#endforeach
</select>
</td>
<td>{{$value->VERME}}</td>
<td>
<form name="ajaxForm" id="ajaxForm" class="form-horizontal" novalidate="">
<input type="text" name="_token" value="{{ csrf_token() }}">
<input type="text" name="id" value="{{$value->id}}">
<button type="button" class="btn btn-primary" id="btnSave" value="add">Save</button>
</form>
</td>
</tr>
#endforeach
#endforeach
</tbody>
This is my current controller code
public function cart() {
if (Request::isMethod('post')) {
$id = Request::get('id');
$customers = DB::table('Materials')->select('Materials.MATNR','Materials.MAGRV','Materials.CONV1','Materials.id','Materials.CONV1','Materials.UNIT1','Materials.CONV2','Materials.UNIT2','Materials.MHDHB','Materials.MAKTX','Inventory.CHARG','Inventory.VERME')->join('Inventory',function($join){
$join->on('Inventory.MATNR','LIKE',DB::raw("CONCAT('%',Materials.MATNR,'%')"));
})->groupBy('Materials.MATNR','Materials.MAGRV','Materials.CONV1','Materials.id','Materials.CONV1','Materials.UNIT1','Materials.CONV2','Materials.UNIT2','Materials.MHDHB','Materials.MAKTX','Inventory.CHARG','Inventory.VERME')->find($id);
$cart = ShoppingCart::add($customers->id, $customers->CHARG, 5, 100.00, ['MAKTX' => $customers->MAKTX, 'kg' => $customers->VERME]);
}
}
And my current route
Route::post('/cart', 'system\request_customer#cart');
Ajax current code
$(document).ready(function(){
$("#btnSave").click(function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
})
e.preventDefault();
$.ajax({
type: 'POST',
url: 'cart',
data: $('#ajaxForm').serialize(),
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
So this is my new view
<form method="POST" class="form-horizontal ajaxForm">
<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" id="id" value="{{$value->id}}">
<input type="hidden" name="MATNR" value="{{$value->MATNR}}">
<button>Submit</button>
</form>
and my new ajax
$(".ajaxForm").submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: 'cart',
type: 'POST',
data: formData,
success: function (data) {
console.log(data)
},
cache: false,
contentType: false,
processData: false
});
});
I had the same problem. I solved it by replacing id with class. I don't know why this is a solution, but it worked in my case. To make it easier for you, here is what you have to do. Just try it, according to me, It must work!
Replace every #ajaxForm with .ajaxForm.
Replace <form name="ajaxForm" id="ajaxForm" class="form-horizontal" novalidate=""> with <form name="ajaxForm" class="form-horizontal ajaxForm" novalidate="">
Let me know if it works. It worked in my case. In my case, I was having the same error. I mean, only first button or submit button worked.
If you are JQuery expert or know the reason behind this, please let me know!

Codeigniter select box click get product details

Am creating a Billing System using Codeigniter. Here i want to select product using droupdown that product details show without refresh the page like ajax. My code is following:
View
<?php
foreach($productlists as $product)
{
?>
<tr>
<td>1</td>
<td>
<select class="form-control" name="product_id">
<option>--Select Payment--</option>
<option value="<?php echo $product['sno']; ?>"><?php echo $product['product_name']; ?></option>
</select>
</td>
<td><input type="number" name="product_order_qty" class="form-control" value="1"></td>
<td>0.8</td>
<td>0.8</td>
<td><input type="number" name="product_price" class="form-control" value="100"></td>
<td><input type="number" name="product_total" class="form-control" value="101.60"></td>
<td><a class="btn btn-primary waves-effect waves-light btn-xs"><i class="fa fa-plus"></i> Add</a></td>
</tr>
<?php } ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
//Ajax code here
</script>
And Pass the product id to controller
controller
function productdetails()
{
$id = $_GET['product_id'];
$data['product'] = $this->orders->productdetails($id);
$data['title'] = "View Customer Order";
$this->load->view('customer_order_view',$data);
}
Please help me to pass the product_id using Ajax.
Thanks in advance
<select class="form-control" name="product_id" onchange="getProduct(this.value)">
<option>--Select Payment--</option>
<option value="<?php echo $product['sno']; ?>"><?php echo $product['product_name']; ?></option>
</select>
//your jquery function
function getProduct(product_id){
alert(product_id);
$.ajax({
url: "your_url",
type: "POST",
data:
{
id:product_id,
}
}).done(function( data )
{
// alert(data);
});
}

delete all that are checked in laravel

Hello I want to delete all the rows from the database which are being checked by the user but whenever I do this I get this error "Missing argument 1 for App\Http\Controllers\UploadController::deleteSelectedUploads()". I know why this error is coming because I haven't passed any id to it and if I pass any id to it then it says Undefined variable: upload. Please help me out. I know I have been posting a lot of questions nowadays and later solve it on my own but this is the only way of learning I guess.
Controller
public function deleteSelectedUploads($id) {
//$uploads = $this->upload->get($id);
// $checkboxes = $this->request->input('checkbox[]');
// if((Auth::user()->role=='admin')) {
// $this->upload->deleteUpload($id);
// } else {
// Session::flash('message', "Oooopsss ... ! seems like you don't have the authority to
// <span style=font-weight:bold;'><u>DELETE</u></span> this file.
// <span style='font-size:15pt; font-weight:bold;'> Contact ADMIN </span>");
// return redirect('home');
// }
$checkbox = Input::all();
foreach($checkbox['checkbox'] as $id) {
Upload::where('id', $id)->delete();
}
Session::flash('message', 'You File has been deleted');
return redirect('home');
}
View
<th><form action="/delete-selected-uploads" method="get">
<input type="checkbox" name="checkbox" onClick="toggle(this)"/>
<input type="submit" class="btn btn-block btn-success btn-xs" value="Delete all checked">
</form>
</center>
</th>
<td>
<input type="checkbox" name="checkbox[]" id="{{ $upload['id'] }}" value="{{$upload['id']}}">
</td>
Route
Route::get('/delete-selected-uploads', 'UploadController#deleteSelectedUploads');
Your problem is that you maybe put the parameter to
The function public function deleteSelectedUploads($id) { it need the parameter $id.
And you call this function avoid ::deleteSelectedUploads().
One solutions can be put not requerid the $id parameter as public function deleteSelectedUploads($id = '') {
UPDATE: Try in the view:
<th>
<form action="/delete-selected-uploads" method="get">
<input type="checkbox" name="checkbox" onClick="toggle(this)"/>
<input type="submit" class="btn btn-block btn-success btn-xs" value="Delete all checked">
<td>
<input type="checkbox" name="checkbox[]" id="{{ $upload['id'] }}" value="{{$upload['id']}}">
</td>
</center>
</th>
</form>
I hope help you.

Resources