Issue
I have a form, which is opened in the Android web view, I want to save data to the database, but when I have making an ajax call and try to print data is showing a blank array, following are my code :
<div class="card-body card-padding">
<form id="submitdataform">
@csrf
@foreach($formfields as $key=>$value)
<input type="hidden" name="campaign_id" value="{{$key}}"/>
@foreach($value as $attrkey)
<input type="hidden" name="attribute_id[]" value="{{$attrkey->id}}"/>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="pure-material-textfield-outlined w-100">
{{--<input placeholder=" " type="text" required>--}}
<?php
if ($attrkey->attribute_type == 'alpha') {
echo "<input placeholder=' ' type='text' name='attribute_value[]' required>";
} elseif ($attrkey->attribute_type == 'date') {
echo "<input placeholder=' ' type='date' name='attribute_value[]' required>";
} elseif ($attrkey->attribute_type == 'numberic') {
echo "<input placeholder=' ' type='number' name='attribute_value[]' required>";
}
?>
<span>{{$attrkey->attribute_name}}</span>
</label>
</div>
</div>
</div>
@endforeach
@endforeach
<button type="button" id="submitbtn">Submit</button>
</form>
</div>
Now ajax call
$("#submitbtn").click(function (event) {
event.preventDefault();
var data = $("#submitdataform").serialize();
$.ajax({
type: "post",
url: "savecampaigndata",
data: {data: data},
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function (data) {
// Android.passParams('dashboard');
},
error: function (data) {
// Android.passParams(url);
}
});
});
And this is the controller method where I want to get all my data ,
public function SaveCampaignData(Request $request)
{
return response()->json($request->all());
$data = $request->all();
$attribute_id = [];
$attribute_value = [];
$campaign_id = $data['campaign_id'];
$user_id = Auth::user()->id;
foreach ($data as $key => $value) {
if ($key == "attribute_id") {
foreach ($value as $attrkey) {
$attribute_id[] = $attrkey;
}
} else {
if ($key == "attribute_value") {
foreach ($value as $attrvalue) {
$attribute_value[] = $attrvalue;
}
}
}
}
$mainArray = array_combine($attribute_id, $attribute_value);
$currentdate = Carbon::now();
foreach ($mainArray as $key => $value) {
DB::table('campaign_attribute_values')->insert(
[
'campaign_id' => $campaign_id,
'campaign_attribute_id' => $key,
'user_id' => $user_id,
'campaign_attribute_value' => $value,
'created_at' => $currentdate,
'updated_at' => $currentdate,
]
);
}
if ($this->CheckSmsLimit() > 0) {
$this->checkAutoReply($campaign_id, $user_id);
}
return redirect()->back()->with('success', 'Request Registered');
// return response()->json(['status' => true, 'message' => 'Record saved succesfully', 'code' => 200]);
}
This is postman route URL :
https://subdomain.xyz.com/savecampaigndata
But it's giving a blank array as output in my controller.
Solution
$("#submitbtn").click(function (event) {
event.preventDefault();
var data = $("#submitdataform").serialize();
$.ajax({
type: "post",
url: "{{ url('savecampaigndata) }}",
data: {data: data},
success: function (data) {
// Android.passParams('dashboard');
},
error: function (data) {
// Android.passParams(url);
}
});
});
- if you don't want to send csrf token
then
app/Http/Middleware/VerifyCsrfToken.php
and add this route's url to the except array
protected $except = [
'savecampaigndata'
];
- if want
then add in head
<meta name="csrf-token" content="{{ csrf_token() }}">
and in script
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Answered By - Akash Kumar Verma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.