jquery post method to send javascript array to php doesn't work for me, why? -
i looking on web how send java script array, one:
var array = ["thing1", "thing2", "thing3"];" to php file on server.
then came across jquery .post() method, when use php file remains empty!
the code using is:
var array = ["thing1", "thing2", "thing3"]; $(document).ready(function() { $.post( 'hs.php', array, function(data, statustext) { alert("uploaded") }); }) but have no experience php , don't know should php empty or have code retrieve array! want store javascript array on server file can read array again on page load.
what doing wrong?
can show short example of working html , php code?
you can use json this. json encode javascript array , send php , decode json-decoded array on server side.
json standard notation converts objects (arrays, class objects etc) string notation, can passed via http.
post variables stored in data option of jquery post/ajax. lets want pass following array php:
var my_array = ["my", "name", "is", "foo", "bar"]; you can json encode using jquery's json encoding plugin.
var my_json_val = $.json.encode(my_array); then can pass value server using:
$.post({ url: 'my_url', data: { my_json_name: my_json_val} }); at server side can this:
$my_json_encoded = $_post['my_json_name']; $my_json_val = json_decode($my_json_encoded);
Comments
Post a Comment