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

报告相同问题?