There is a form where from this field

<input type="text" class="form-control" id="fio"> 

need to send a separate first name, last name and middle name. json:

 name: 'username', surname: 'usersurname', lastname: 'userlastname' 

Tell me, please, how to do this? With separate fields for the name of the fam is clear, you can

 name: $('#name').val(), surname: $('#surname').val(), lastname: $('#lastname').val(), 

But how to send data from one field without a clue.

2 answers 2

If you can guarantee that the surname, first name and patronymic are entered through the space in the correct order, then the split function is enough:

 var fio = $('#fio').val().split(' '); var data = { lastname: fio[0].val(), name: fio[1].val(), surname: fio[2].val() }; 

If you can not guarantee, then you are asking for trouble, asking the user for a full name in one field, and storing them separately.

  • Thank you very much! - Vi Vi

For example, use var arr = $('#fio').val().split(' '); Hoping the user has entered the data in the correct order. And then send the values ​​to the usual ajax, using the index arr[0]..arr[2]