I am trying to pass a variable from my javascript code to the server side PHP code. I know this must be done via an ajax, But unfortunately I do not receive the value in the PHP variable in blade view.
form : url = facture=20
{!! Form::open(array('url'=> 'admin/facture=20', 'method'=>'post', 'name'=>'pushds')) !!}
{!! Form::hidden('ms_id', $fact->id) !!}
{!! Form::hidden('mstid', $fact->b_id, array('id' => 'mstid')) !!}
{!! Form::close() !!}
jquery :
$('form[name="pushds"]').on('submit', function (e){
e.preventDefault();
var mstid = $('#mstid').val();
var ms_id = $('#ms_id').val();
$.ajax({
type: 'post',
url: 'facture=20',
data: {mstid: $('#mstid').val(), ms_id : $('#ms_id').val()},
success: function( data ) {
......
},
the current page url : facture=20
the variable php in blade view
<?php
if (!empty($_POST["mstid "])) {
echo $_POST['mstid '];
}
?>
Try this
In form (Add id field to from)
{!! Form::hidden('ms_id', $fact->id, array('id' => 'ms_id')) !!}
{!! Form::hidden('mstid', $fact->b_id, array('id' => 'mstid')) !!}
In AJAX
var mstid = $('#mstid').val(); # Calling through respective ID
var ms_id = $('#ms_id').val(); # Calling through respective ID
$.ajax({
type: 'post',
url: 'facture=20',
data: {mstid: mstid , ms_id : ms_id }, # passing variables
success: function( data ) {
Make sure your from submit reaches the AJAX code. You can test it by using
alert()
inside AJAX function ..
There are many methods to pass javascript value to server side using ajax:
Method 1:
Change your ajax code to:
From:
$.ajax({
type: 'post',
url: 'facture=20',
data: {mstid: $('#mstid').val(), ms_id : $('#ms_id').val()},
success: function( data ) {
......
},
To:
$.ajax({
type: 'post',
url: 'facture=20',
data: {mstid: $('input[name=mstid]').val(), ms_id : $('input[name=ms_id]').val()},
success: function( data ) {
......
},
And on server side you can get it as :
<?php
if (isset($_POST["mstid "]) && $_POST["mstid "] != "") {
echo $_POST['mstid '];
}
?>
Thanks