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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?