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!
1 answer
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"> ФИО <input type="text" name="name" /><br/> Телефон <input type="number" name="phone" /><br/> Адрес <input type="text" name="address" /><br/> <button type="submit">Отправить</button </form> |
alert()if you usedserializeArray(), in this case, you need to display the data in the browser console via the commandconsole.log()- RifmaMan