Laravel - htmlspecialchars() expects parameter 1 to be string, array given (View: in input field - laravel

In my Laravel-5.8 project consisting of Sales application, I have this codes:
Controller:
public function create()
{
$suppliers =Supplier::all();
$categories = Category::all();
$taxes = Tax::all();
$units = Unit::all();
return view('product.create', compact('categories','taxes','units','suppliers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|min:3|unique:products|regex:/^[a-zA-Z ]+$/',
'serial_number' => 'required',
'model' => 'required|min:3',
'category_id' => 'required',
'sales_price' => 'required',
'unit_id' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'tax_id' => 'required',
]);
$product = new Product();
$product->name = $request->name;
$product->serial_number = $request->serial_number;
$product->model = $request->model;
$product->category_id = $request->category_id;
$product->sales_price = $request->sales_price;
$product->unit_id = $request->unit_id;
$product->tax_id = $request->tax_id;
if ($request->hasFile('image')){
$imageName =request()->image->getClientOriginalName();
request()->image->move(public_path('images/product/'), $imageName);
$product->image = $imageName;
}
$product->save();
foreach($request->supplier_id as $key => $supplier_id){
$supplier = new ProductSupplier();
$supplier->product_id = $product->id;
$supplier->supplier_id = $request->supplier_id[$key];
$supplier->price = $request->supplier_price[$key];
$supplier->save();
}
return redirect()->back()->with('message', 'Product Created Successfully');
}
view
<div class="row mt-2">
<div class="clearix"></div>
<div class="col-md-10">
<div class="tile">
<h3 class="tile-title">Product</h3>
<div class="tile-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form method="POST" action="{{route('product.store')}}" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Product Name</label>
<input name="name" class="form-control #error('name') is-invalid #enderror" type="text" placeholder="Product Name">
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Serial Number</label>
<input name="serial_number" class="form-control #error('serial_number') is-invalid #enderror" type="number" placeholder="Enter Tax Name">
#error('serial_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Model</label>
<input name="model" class="form-control #error('name') is-invalid #enderror" type="text" placeholder="Enter Tax Name">
#error('model')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Category</label>
<select name="category_id" class="form-control">
<option>---Select Category---</option>
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Sale Price</label>
<input name="sales_price" class="form-control #error('sales_price') is-invalid #enderror" type="number" placeholder="Enter Tax Name">
#error('sales_price')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Unite</label>
<select name="unit_id" class="form-control">
<option>---Select Unit---</option>
#foreach($units as $unit)
<option value="{{$unit->id}}">{{$unit->name}}</option>
#endforeach
</select>
#error('unit_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Image</label>
<input name="image" class="form-control #error('image') is-invalid #enderror" type="file" >
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Tax </label>
<select name="tax_id" class="form-control">
<option>---Select Tax---</option>
#foreach($taxes as $tax)
<option value="{{$tax->id}}">{{$tax->name}} %</option>
#endforeach
</select>
#error('tax_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="tile">
<div id="example-2" class="content">
<div class="group row">
<div class="form-group col-md-5">
<select name="supplier_id[]" class="form-control">
<option>Select Supplier</option>
#foreach($suppliers as $supplier)
<option value="{{$supplier->id}}">{{$supplier->name}} </option>
#endforeach
</select>
</div>
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control #error('supplier_price') is-invalid #enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
<div class="form-group col-md-2">
<button type="button" id="btnAdd-2" class="btn btn-primary float-right"><i class="fa fa-plus"></i></button>
<button type="button" class="btn btn-danger btnRemove float-right"><i class="fa fa-trash"></i></button>
</div>
</div>
</div>
</div>
<div class="form-group col-md-4 align-self-end">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Update</button>
</div>
</form>
</div>
</div>
</div>
</div>
When I submitted the form, I got this error:
htmlspecialchars() expects parameter 1 to be string, array given (View:...
Then when I removed this:
value="{{old('supplier_price')}}"
from:
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control #error('supplier_price') is-invalid #enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
the error vanished.
I wanted to use it for the input field to retain its value after any error in validation. Where did I get it wrong and how do I correct it?
Thank you

supplier_price is an array since you define it as name="supplier_price[]"
your old() function will output an array while the mustache function {{}} expects a string.
If I understand your code properly you should be better off removing the [] from name="supplier_price[]"

Related

How to Handle Laravel error messages in vue?

I know I did something wrong here, but how do you handle error messages of Laravel in vue template?... is there a way of bypassing the #error of the laravel Blade templating
this is the vue component file (fileComponent.vue)
<template>
<div class="row my-2">
<div class="col-md-2 font-weight-bold pt-2">Grade:</div>
<div class="col-md-10">
<select name="grade" class="form-control #error('grade') is-invalid #enderror" v-model="selected_grade" #change="onChange($event)">
<option v-for="grade in grading" :key="grade.id" :value="grade.name">{{grade.name}}</option>
</select>
#error('grade')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<div class="row my-3" v-if="subjects !== null">
<div class="col-md-3">
<div class="card shadow">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold">Subjects:</h6>
</div>
<div class="card-body">
<div class="custom-control custom-checkbox" v-for="subject in subjects" :key="subject.id">
<input type="checkbox" class="custom-control-input #error('subjects') is-invalid #enderror" name="subjects[]" :id="subject.name" :value="subject.name"/>
<label class="custom-control-label" :for="subject.name">{{subject.name}}</label>
</div>
#error('subjects')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
</div>
</div>
</div>
</template>
This is Shown on the page instead of error message

syntax error, unexpected 'layouts' (T_STRING), expecting ')' (View: C:\xampp7\htdocs\shopping\resources\views\auth\register.blade.php)

ican not open to page "register.blade.php" when I click to register itt alert this error
syntax error, unexpected 'layouts' (T_STRING), expecting ')' (View: C:\xampp7\htdocs\shopping\resources\views\auth\register.blade.php) I can open page login.blade.php
my code in app.blade.php::
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
#if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
There is a problem with your Register helper function, you are missing ')
<button type="submit" class="btn btn-primary">
{{ __('Register}}
</button>
Change to
<button type="submit" class="btn btn-primary">
{{ __('Register')}}
</button>
register.blade.php::
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register}}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection

laravel blade syntax for loop how to call iteration index inside laravel helper functions

I have the following for loop
#for($i = 0; $i < $contactAmount; $i++)
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">voornaam</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname.0') is-invalid #enderror" name="firstname[]" value="{{ old('firstname.0') }}" autofocus>
#error('firstname.0')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
#endfor
but if I replace the hard coded '0' with {{ $i }} my validation does not work anymore. So how do I call my integer and use it inside laravel helper functions?
#for($i = 0; $i < $contactAmount; $i++)
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">voornaam</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname.{{ $i }}') is-invalid #enderror" name="firstname[]" value="{{ old('firstname.'. $i) }}" autofocus>
#error('firstname.{{ $i }}')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
#endfor
Why don't you use a #foreach? You can access the index of the loop as follows:
#foreach($contactAmount as $amount)
<div class="form-group row">
{{ $loop->iteration }}
...
</div>
#endforeach

How to solve Missing required parameters for [Route: update_pns] [URI: update/{id}]

I want to create Update fucntion . i create the form , i displaying dataon this form , i create button submit to submit this data . but after i submit i get error
Missing required parameters for [Route: update_pns] [URI: update/{id}].
its my Controller to showing data
public function edit_pns($id)
{
$pns = Data_pns::findOrFail($id);
if (!$pns)
abort(404);
$agama = DB::table('master_agama')->pluck('agama','id');
$golongan = DB::table('master_golongan')->pluck('golongan','id');
$jabatan = DB::table('master_jabatan')->pluck('nama_jabatan','id');
$unit_kerja = DB::table('master_unit_kerja')->pluck('unit_kerja','id');
$status_pegawai = DB::table('master_status_pegawai')->whereIn('id',[3,4])->pluck('status_pegawai','id');
//dd($pns);
return view('admin/edit/edit_pns', ['pns' => $pns ,'golongan'=>$golongan,
'jabatan'=>$jabatan,'unit_kerja'=>$unit_kerja,'agama'=>$agama,'status_pegawai'=>$status_pegawai]);
}
and its my controller to store data to update
public function update_pns(Request $request,$id)
{
$pns = User::find($id);
$pns->nama = $request->input('nama');
$pns->email = $request->input('email');
$pns->alamat = $request->input('alamat');
$pns->NIK = $request->input('NIK');
$pns->tempat_lahir = $request->input('tempat_lahir');
$pns->tanggal_lahir = $request->input('tanggal_lahir');
$pns->pendidikan = $request->input('pendidikan');
$pns->nomor = $request->input('nomor');
$pns->agama_id = $request->input('agama_id') ;
$pns->status_pegawai_id = $request->input('status_pegawai_id');
$pns->unit_kerja_id = $request->input('unit_kerja_id') ;
dd($pns);
// $pns->update();
// return redirect('pns1')->with('success', 'Your info are updated');
}
and its this route
Route::get('/edit_pns/{id}', 'AdminController#edit_pns' );
Route::post('/update/{id}', 'AdminController#update_pns')->name('update_pns');
whats wrong about this ?
UPDATE
<form method="POST" action="{{ route('update_pns') }}" class="form-horizontal form-label-left">
#csrf
<span class="section">Lengkapi Form Dibawah ini :</span>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="nama">NAMA <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="nama" type="text" class="form-control #error('nama') is-invalid #enderror" name="nama" value="{{$pns->users->nama}}" required autocomplete="nama">
#error('nama')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="NIK">NIK <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="NIK" type="text" class="form-control #error('NIK') is-invalid #enderror" name="NIK" value="{{$pns->users->NIK}}" required autocomplete="NIK" placeholder="3314********">
#error('NIK')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{$pns->users->email}}" required autocomplete="email" placeholder="contoh#gmail.com" required>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="tempat_lahir">Tempat Lahir <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="tempat_lahir" type="text" class="form-control #error('tempat_lahir') is-invalid #enderror" name="tempat_lahir" value="{{$pns->users->tempat_lahir}}" required autocomplete="tempat_lahir">
#error('tempat_lahir')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="tanggal_lahir">Tanggal Lahir <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="tanggal_lahir" type="date" class="form-control #error('tanggal_lahir') is-invalid #enderror" name="tanggal_lahir" value="{{$pns->users->getOriginal('tanggal_lahir')}}" required autocomplete="tanggal_lahir">
#error('tanggal_lahir')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label for="jenis_kelamin" class="control-label col-md-3">Jenis Kelamin <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<td>
<label> Laki-Laki<input type="radio" name="jenis_kelamin" value="L" checked>
</label>
</td>
<td>
<label> Perempuan
<input type="radio" name="jenis_kelamin" value="P">
</label>
</td>
#error('jenis_kelamin')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="pendidikan">Pendidikan Terakhir <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="pendidikan" type="text" class="form-control #error('pendidikan') is-invalid #enderror" name="pendidikan" value="{{$pns->users->pendidikan}}" required autocomplete="pendidikan">
#error('pendidikan')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="nomor">Nomor HP <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="nomor" type="text" class="form-control #error('nomor') is-invalid #enderror" name="nomor" value="{{$pns->users->nomor}}" required autocomplete="nomor" placeholder="contoh 628572510294">
#error('nomor')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Agama <span class="required">*</span></label>
<div class="col-md-6 col-sm-9 col-xs-12">
<select name="agama_id" id="agama_id" class="form-control">
#foreach($agama as $masters => $agamas )
<option value="{{ $masters }}" #if ($pns->users->agama_id == $masters ) selected #endif>{{ $agamas }}</option>
#endforeach
</select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="alamat">Alamat <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea id="alamat" required="required" name="alamat" value="" class="form-control col-md-7 col-xs-12">{{$pns->users->alamat}}</textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Unit Kerja <span class="required">*</span></label>
<div class="col-md-6 col-sm-9 col-xs-12">
<select name="unit_kerja_id" id="unit_kerja_id" class="form-control">
#foreach($unit_kerja as $units => $unit )
<option value="{{ $units }}" #if ($pns->users->unit_kerja_id == $units ) selected #endif>{{ $unit }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Status Pegawai <span class="required">*</span></label>
<div class="col-md-6 col-sm-9 col-xs-12">
<select name="status_pegawai_id" id="status_pegawai_id" class="form-control">
#foreach($status_pegawai as $status => $pegawai )
<option value="{{ $status }}" #if ($pns->users->status_pegawai_id == $status ) selected #endif>{{ $pegawai }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button id="send" type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Your route require a parameter in order to be resolved, {id}, so instead of route('update_pns') inside the action of your form, you should have used route('update_pns', $idDesired) and instead of $idDesired, you should write the id you want to get on the update_pns method as $id
Since you're using request in your controller
Change your route to:
Route::post('/update', 'AdminController#update_pns')->name('update_pns');
And to your controller you can do this.
public function update_pns(Request $request)
{
$pns = User::find($request->id);
...
}
Also create a hidden input to your html to get the request id
<input tpye="hidden" value="{{ $id }}" name="id" />

How to show data after user click an option from drop down list?

I want to show the data after user have selected the project.
I've tried several method but none of them can show the next data.
I want to create as simple as when user choose a project, it'll show the next question such as select product, select period, etc.
this is my form.blade.php
#extends('layouts.master')
#section('style')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheer" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
.box{
width:600px;
margin:0 auto;
border:1[x solid #ccc;]
}
</style>
#endsection
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Add Prospect</div>
<form action="{{ url('/add/outlet') }}" method="post" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<select name="project_name" id="project" class="form-control input-lg dynamic" data-dependent="product">
<option value="">Select Project</option>
#foreach($project_list as $project)
<option value="{{$project->project}}"> {{$project->project}} </option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="product_name" id="product" class="form-control input-lg">
<option value="">Select Product</option>
<option value="HelloBill Retail">HelloBill Retail</option>
<option value="HelloBill FNB">HelloBill FNB</option>
</select>
</div>
<div class="form-group">
<select name="period" id="period" class="form-control input-lg">
<option value="">Select Period</option>
<option value="Bulanan">Bulanan</option>
<option value="Tahunan">Tahunan</option>
</select>
</div>
<div class="form-group row">
<label for="outlet" class="col-md-4 col-form-label text-md-left">Outlet</label>
<div class="col-md-8">
<input id="outlet" type="text" class="form-control{{ $errors->has('outlet') ? ' is-invalid' : '' }}" name="outlet" value="{{ old('outlet') }}" required autofocus>
#if ($errors->has('outlet'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('outlet') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="alamat" class="col-md-4 col-form-label text-md-left">Alamat</label>
<div class="col-md-8">
<input id="addressLine1" type="text" class="form-control{{ $errors->has('addressLine1') ? ' is-invalid' : '' }}" name="addressLine1" value="{{ old('addressLine1') }}" placeholder="Address Line #1" required autofocus>
#if ($errors->has('addressLine1'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('addressLine1') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="addressLine2" type="text" class="form-control" name="addressLine2" value="{{ old('addressLine2') }}" placeholder="Address Line #2" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="kelurahan" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="kelurahan" type="text" class="form-control{{ $errors->has('kelurahan') ? ' is-invalid' : '' }}" name="kelurahan" value="{{ old('kelurahan') }}" placeholder="Kelurahan" required autofocus>
#if ($errors->has('kelurahan'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('kelurahan') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="kecamatan" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="kecamatan" type="text" class="form-control{{ $errors->has('kecamatan') ? ' is-invalid' : '' }}" name="kecamatan" value="{{ old('kecamatan') }}" placeholder="Kecamatan" required autofocus>
#if ($errors->has('kecamatan'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('kecamatan') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="kota" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="kota" type="text" class="form-control{{ $errors->has('kota') ? ' is-invalid' : '' }}" name="kota" value="{{ old('kota') }}" placeholder="Kota" required autofocus>
#if ($errors->has('kota'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('kota') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="provinsi" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="provinsi" type="text" class="form-control{{ $errors->has('provinsi') ? ' is-invalid' : '' }}" name="provinsi" value="{{ old('provinsi') }}" placeholder="Provinsi" required autofocus>
#if ($errors->has('provinsi'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('provinsi') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="zipCode" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="zipCode" type="text" class="form-control{{ $errors->has('zipCode') ? ' is-invalid' : '' }}" name="zipCode" value="{{ old('zipCode') }}" placeholder="Kode Pos" required autofocus>
#if ($errors->has('zipCode'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('zipCode') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="PIC" class="col-md-4 col-form-label text-md-left">PIC</label>
<div class="col-md-8">
<input id="PIC" type="text" class="form-control{{ $errors->has('PIC') ? ' is-invalid' : '' }}" name="PIC" value="{{ old('PIC') }}" required autofocus>
#if ($errors->has('PIC'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('PIC') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-left">Email</label>
<div class="col-md-8">
<input id="email" type="text" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Add
</button>
</div>
</div>
#if($errors->any())
<div class="category-msg-error">{{ $errors->first() }}</div>
#endif
</div>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('extra')
<script>
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val()!=''){
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var __token = $('input[name=_token"]').val();
$.ajax({
url:"{{route('dynamicdependent.fetch')}}",
method:"POST",
data:{select:select, value:value, _token:_token,
dependent=dependent},
success:function(result){
$('#+dependent').html(result);
}
})
}
});
});
</script>
#endsection
please help. and i am also new in Laravel
There are two mistakes:-
In ajax request data you are setting dependent=dependent that should be dependent: dependent.
In Success response, you are setting this $('#+dependent').html(result); but that is not correct. It should be this $('#'+dependent).html(result);
Below is the full jQuery code:
<script>
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val()!=''){
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var __token = $('input[name=_token"]').val();
$.ajax({
url:"{{route('dynamicdependent.fetch')}}",
method:"POST",
data:{
select:select,
value:value,
_token:_token,
dependent:dependent
},
success:function(result){
$('#'+dependent).html(result);
}
});
}
});
});
</script>
Please check and let me know if that will not help. I will try to solve your problem.

Resources