dongzong5017 2018-07-17 07: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 10: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');
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog