javascript - pass different types of variable with ajax call -


i going post values of mutiple checkboxes , textboxes php page using click button aid of ajax call. in html have:

  <input type="text" name="field1" id="field1" />   <input type="text" name="field2" id="field2" />   <input type="checkbox" name="params[]" id="params" class="params" value="value1">value1<br>   <input type="checkbox" name="params[]" id="params" class="params" value="value2">value2<br>  <input type="button" value="get parameters" id="getparams" name="getparams"/><p> <div id="response"></div> 

in jscript have:

$(document).ready(function(){     $("#getparams").click(function(){             var field1 = $("#field1").val();            var field2 = $("#field2").val();             var params = {'params[]' : []};            $(":checked").each(function() {              params['params[]'].push($(this).val());            });            var datastr = '&field1=' + field1 + '&field2=' + field2 + '&params=' + params;             $.ajax({            type: "post",            url: "page.php",            data: datastr,            cache: false,            success: function(html){            $("#response").fadein("slow");            $("#response").html(html);            }    });            return false;    }); }); 

so push checkbox values array (params) , textbox values separate variables (field1 ,field2). in php can access field1,field2 can't access array params.

echo $_post['field1'];// can access.. echo $_post['params']; // [object object] response 

what problem here ?

your params array ( considered object ) needs serialized before adding datastr variable.

an easier way use .submit function, heavy lifting you: http://api.jquery.com/submit/


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -