我正在尝试将一个变量从我的javascript代码传递到服务器端PHP代码。我知道这必须通过ajax来完成,但不幸的是我在刀片视图中没有收到PHP变量中的值。
form:url = facture = 20
bosh director
jquery:
{!! 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() !!}
当前页面网址:facture = 20
刀片视图中的变量php
$('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 ) {
......
},
答案 0 :(得分:0)
试试这个
格式(从添加ID字段)
{!! Form::hidden('ms_id', $fact->id, array('id' => 'ms_id')) !!}
{!! Form::hidden('mstid', $fact->b_id, array('id' => 'mstid')) !!}
在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 ) {
确保您的提交到达AJAX代码。您可以使用AJAX函数中的
alert()
来测试它。
答案 1 :(得分:0)
有很多方法可以使用ajax将javascript值传递给服务器端:
方法1:
将您的ajax代码更改为:
来自:
$.ajax({
type: 'post',
url: 'facture=20',
data: {mstid: $('#mstid').val(), ms_id : $('#ms_id').val()},
success: function( data ) {
......
},
要:
$.ajax({
type: 'post',
url: 'facture=20',
data: {mstid: $('input[name=mstid]').val(), ms_id : $('input[name=ms_id]').val()},
success: function( data ) {
......
},
在服务器端,你可以得到它:
<?php
if (isset($_POST["mstid "]) && $_POST["mstid "] != "") {
echo $_POST['mstid '];
}
?>
由于