-1

I have value from posted data. And its formed in

brand_id=2&hdbrand_id=&pcat_id=38&pcat_name=SHOES.

I try to separate it with array explode, but it will be generated and i must iterate it as a key value. what i want to do is get every single variable in that formed value. Do you mind to help me out?

here is my complete code

blade view

<div class="content splite">
<div class="splite-item">
    <div class="bag-white">
        <div class="title-content"><i class="fa fa-file-text-o"></i>CREATE PRODUCT CATEGORIES FORM</div>
        <!-- <form class="form-main" action="{{ route('product-categories.store') }}" method="post"> -->
        <form class="form-main" id="formCat">
            <div class="field radio" data-title="Brands">
              @foreach($dataBrands as $brands)
                    <input type="radio" id="radio-{{$brands->brand_id}}" name="brand_id" value="{{$brands->brand_id}}"  {{ $brands->brand_id == $dataEdit['brand_id'] ? 'checked' : '' }}>
                    <label for="radio-{{$brands->brand_id}}">{{ $brands->brand_name }}</label>     
                @endforeach
                <input type="hidden" name="hdbrand_id" >
                <input type="hidden" id="pcat_id" name="pcat_id" value="{{$dataEdit['pcat_id']}}" >
            </div>
            <div class="field" data-title="Nama Kategori">
            <input type="text" id="pcat_name" name="pcat_name" value="{{ $dataEdit['pcat_name'] }}" autofocus>
            </div>
            <div class="field" data-title="Action">
                <button id="btnSave" type="button" class="but-main">add</button>
                <a href="/product-categories" class="but-main"><i class="fa fa-times"></i>Batal</a>
            </div>
        </form>
    </div>
</div>

Jquery

$('#btnSave').click(function(){
var form = $('#formCat').serialize();
var id = $('#pcat_id').val();
var url = "{{route('product-categories.update',':id')}}";
url = url.replace(':id',id);
$.ajax({
    type:'POST',
    headers: {
        'X-CSRF-TOKEN' : "{{ csrf_token() }}"
    },
    url: url,
    data: {form:form, "_method":"PUT"},
    success:function(data){

        console.log(data);
    },
    error:function(data){
      console.log(data);

    }

});

});

Controller

    public function update($id){

    $category = Input::get('form');
    return $category;

}
maljaddinu
  • 37
  • 4
  • Why is your form data coming through like a GET request? – user3783243 Jul 02 '19 at 04:29
  • Possible duplicate of [Parse query string into an array](https://stackoverflow.com/questions/5397726/parse-query-string-into-an-array) – user3783243 Jul 02 '19 at 04:31
  • i am using PUT. The data is serialized() and then send to controller. In my controller i'm using $category = Input::get('form') and then returning it. – maljaddinu Jul 02 '19 at 04:37
  • Please add more information to the question. If it is being serialized there should be inverse function used. `Input::get(` seems like a framework is being used.. perhaps the frameworks has native functionality for this? – user3783243 Jul 02 '19 at 04:48

1 Answers1

1

This you can archive with parse_str.

$str = "brand_id=2&hdbrand_id=&pcat_id=38&pcat_name=SHOES";
parse_str($str, $output);

foreach ($output as $key => $value) {
    echo "KEY  : {$key}\nVALUE: {$value}\n\n";
}

/**
 * OUTPUT
 * -------
 * KEY  : brand_id
 * VALUE: 2
 * 
 * KEY  : hdbrand_id
 * VALUE: 
 * 
 * KEY  : pcat_id
 * VALUE: 38
 * 
 * KEY  : pcat_name
 * VALUE: SHOES
 */
CodyKL
  • 1,024
  • 6
  • 14
  • it's correct only if string doesn't have spaces. If they have spaces, potentially the above array will not work in case of comparison/calculation.example:-https://3v4l.org/XeNYt . But in the same case mine produce correct output:-https://3v4l.org/scRod – Alive to die - Anant Jul 02 '19 at 04:35
  • To get rid of the whitespace he can also use the `trim()` function on the values. The OP also mentioned that he send serialized data to his controller, which doesn't leads to the string format you described. – CodyKL Jul 02 '19 at 04:46