Good afternoon, there is a form of such a structure:

<form action="" id="add-user"> <input type="text"> <input type="text"> <input type="text"> <input type="text"> <input type="text"> <button type="submit" name="save">Сохранить</button> </form> 

How to make it so that when you serialize a form using jQuery:

 var formData = $("#add-user").serialize(); 

pass $ _POST ['save']?

That would solve many problems ...

Thanks in advance for the answers.

  • it is not clear what you want. The serialize function is not to POST. - zb '
  • I submit a POST request using an Ajax request, and inside I shove a serialized form ... - spoilt
  • what is your problem? serialize returns string, add "& save =" to it - zb '

2 answers 2

 $('#add_user').on('button', 'click', function(){ $(this).closest('form').preventDefault(); //Предотвращаем выполнение функций, предусмотренных стандартным поведением формы; var formData = $('#add_user input').serialize(); //Ваша переменная; $.post('file.php', {save: formData}); }); 

Do not forget that serialize () requires the name attribute for all enumerated form elements, in our case, input.

     $('#form').find('input, select, textarea').each(function() { var data = {}; if($(this).attr('type').toLowerCase() != 'checkbox') { data[$(this).attr('name')] = this.value; } else { data[$(this).attr('name')] = this.checked; } }); 

    I guess you mean it?

    • ckeckbox, replace with checkbox? =) - Indev
    • one
      @Indev yep, thanks) - lampa