duanping2005 2017-02-23 13:17
浏览 110

Laravel使用AJAX将Javascript变量传递给PHP

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 ']; 
}
?>
  • 写回答

2条回答 默认 最新

  • dtpd58676 2017-02-23 13:26
    关注

    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 ..

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据