dongzong5017 2018-07-16 23:20
浏览 85
已采纳

有没有办法转换文本框值并分配给PHP变量?

My goal is to get some of the value from ajax data to assign in php variable. As you can see the ajax code below, i could pass the value through a class (edit_id). How to put the value from the textbox to a php variable?

Ajax

$.ajax({
        type: 'POST',
        url: "{{ URL::route('test') }}",
        headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
        data: { "camp_id" : camp_id },
        success: function(data){
          if(data.data.success){
                $('#edit_id').val(data.data.success.id);
                $('#edit_desc').val(data.data.success.description);
                console.log(data.data.success.id); // this will output the id eg. 1
                console.log(data.data.success.description); // this will output the description eg. hello world
          }
        }
});

View

<input type="text" class="form-control" id="edit_id" name="id"> //will diplay textbox with value
  • 写回答

1条回答 默认 最新

  • drg14799 2018-07-17 02:01
    关注

    first in your js file get the value of textbox using jquery .val() method and store it in javaScript variale. Then using AJAX post method you can pass it in your controller and can store it in php variable.

    Ex. in your JS file

    $(document).on('click','#payment',function(){
             var user_id = $(this).attr('data-id');
            $.ajax({
                  type:"post",
                  dataType:"json",
                  url:"/payment",
                  data:{
                    user_id:user_id,
                    type:type,
                    amount:amount,
                    remarks:remarks,
                    _token:token
                  },
                  success:function(data){
                    if(data.type=="success"){
                      $('#amount').val('');
                    }
            }
          });
            return false;
        });
    

    in your controller

    public function addPost(Request $request){
    
                    $title = request('title');
                    $id = request('id');
    
            }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部