2

I have the following form:

<td>
  <input type="text" name='name[]' class="form-control"/>
</td>
<td>
  <input type="text" name='mail[]' class="form-control"/>
</td>
<td>
  <select name="gender[]" class="form-control">
    <option value="m">Male</option>
    <option value="f">Female</option>
  </select>
</td>
<td>
  <input type="date" name='birth[]' class="form-control"/>
</td>
<td>
  <input type="number" name='dni[]' class="form-control"/>
</td>
<td>
  <input type="number" name='phone[]' class="form-control"/>
</td>

My ajax call when i try to submit my form

$('#form-reserve-list').on('submit', function(e) {
    e.preventDefault();
    var names =    $("input[name='name[]']").serialize();
    var mails =    $("input[name='mail[]']").serialize();
    var genders =  $("select[name='gender[]']").serialize();
    var births =   $("input[name='birth[]']").serialize();
    var dnis =     $("input[name='dni[]']").serialize();
    var phones =   $("input[name='phone[]']").serialize();
    var _token =   $('input[name=_token]').val();
    var est_id =   $('input[name=est_id]').val();
    var event_id = $('input[name=event_id]').val();

    var url = 'http://localhost:82/boulevard/public/event/reserve/list';

    $.ajax({
        type: 'POST',
        url: url,
        data: {names:names, mails:mails, genders:genders, births:births, dnis:dnis, phones:phones, _token: _token, est_id:est_id, event_id:event_id},
        cache: false,
        success: function(data){
            alert(data);
        }
    });

I want to receive this in my Controller and do a foreach or for loop and save it to my db, but the problem is when i try:

    $names = Input::get('names'); //from ajax names
    foreach($names as $name){
        $name[]; 
        //also tried $name[$key] after i added $key =>
    }

am i doing something wrong? thank you for the help.

EDIT: when i do alert($names) in ajax it shows as name%5B%D=carlos&name%5B%D=kevin is this the way its suppose to be? i also did dd($names); and also shows name%5B%D=carlos&name%5B%D=kevin but when i use foreach loop as mentioned above, the chrome console shows me internal error 500, am i suppose to use foreach?

EDIT2: when i do dd(Input::all())
name%5B%5D=carlos&name%5B%5D=mendieta&name%5B%5D=cordero
how do i loop thru these?

1 Answer 1

2
//No need to serialize each field.
//You could do it like this:

$('#form-reserve-list').on('submit', function(e) {

    e.preventDefault();

    var formData = $(this).serialize();
    var url = 'http://localhost:82/boulevard/public/event/reserve/list';

    $.ajax({
        type: "POST",
        url: url,
        data: formData,
        dataType: "json",
        success: function(data) {

        },
        error: function(){
            alert('opps error occured');
        }
    });

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.