javascript - Send array with alphanumeric keys with Ajax to PHP script -


i need receive names of keys , values alphanumeric array sended ajax php.

from this:

<input class="input" name="one" value="1" onchange="myfunction()">         <input class="input" name="two" value="2" onchange="myfunction()">    <input class="input" name="three" value="3" onchange="myfunction()">  

which parsed loop

{% x in y %}    <input class="input" name="{{ x.k }}" value="{{ x.v }}" onchange="myfunction()"> {% endfor %} 

i need print_r() this:

array (     ['one'] => 1,     ['two'] => 2,     ['three'] => 3, ) 

if i'm doing way:

 function myfunction() {     var elementy = document.getelementsbyclassname('input');     var data = {};         var key = elementy[0].name;     var value = elementy[0].value;     data = { key: value};      $.ajax({            url: "{{ path('test') }}",            type: "post",            data: {data:data} ,     });    } 

print_r($data) return:

array (     [key] => 1 ) 

if i'm doing way:

function myfunction() {     var elementy = document.getelementsbyclassname('input');     var data = {};         data = {elementy[0].name : elementy[0].value};      $.ajax({            url: "{{ path('test') }}",            type: "post",            data: {data:data} ,     });    } 

there uncaught syntaxerror: unexpected token [ in line data = {elementy[0].name : elementy[0].value};

i need this:

function myfunction() {     var data = [];      var elementy = document.getelementsbyclassname('input');      (var = 0; < elementy.length; i++){          data[elementy[i].name] = elementy[i].value;     }      $.ajax({       url: "{{ path('test') }}",       type: "post",       data: {data:data}     });    } 

but without syntax error uncaught syntaxerror: unexpected token [

if have form element surrounding fields, easiest way serialize form:

data: $('form').serialize()          ^^^^ if have more forms, address id or similar 

if don't have form, can use:

data: $('.input').serialize() 

as serialize can used address selections of individual form controls.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -