friends! To send an ajax request, I collect data from the form using the serializeArray method. How can I add a value from a variable that is not associated with a form in the data ajax of the request? And also, how can I see what is in the data? If using, then displays only [object Object]. Thanks in advance!

  • Show your code. - Sasha Omelchenko 1:51 pm
  • You cannot view the form data via alert() if you used serializeArray() , in this case, you need to display the data in the browser console via the command console.log() - RifmaMan

1 answer 1

If you are doing well with transferring form data in an AJAX, then you can add a variable to the end of the array and also pass it as before:

 $(document).on('submit', '#example', function(e) { e.preventDefault(); // отключаем стандартное поведение отправки формы var form_data = $(this).serializeArray(); // формируем массив данных формы form_data[form_data.length] = { // добавляем последний элемент как объект с именем и значением 'name': 'user_id', 'value': '1' } console.log(form_data); // смотрим что теперь у нас лежит в массиве }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="example"> ФИО&nbsp;<input type="text" name="name" /><br/> Телефон&nbsp;<input type="number" name="phone" /><br/> Адрес&nbsp;<input type="text" name="address" /><br/> <button type="submit">Отправить</button </form>