jquery - how to pass array from html to php using ajax -
i try pass array using jquery ajax , code not working well. time change not working. please advise do. think jquery code ok php code not replay. thx.
html code.
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1255"> <script type="text/javascript" src="http://ajax.googleapis.com/ ajax/libs/jquery/1.5/jquery.min.js"></script> <script type="text/javascript" > $(document).ready(function(){ var allvals = { 'a': '1', 'b': '2', 'c': '3' }; //$.each(allvals, function(key, value) { //alert(key + ': ' + value); // }); }); $.ajax({ type: "post", datatype: 'html', url: "test4.php", data: 'allvals=' + allvals, cache: false, success: function(data) { alert(data); $('#test').html(data); } }); </script> <title>insert title here</title> </head> <body> <div id="test">##</div> </body> </html> php code test4.php
<?php $allvals = $_post['allvals']; if ($allvals != ""){ foreach ($allvals $key => $value) { echo ($key.' '.$value."<br />"); } } ?>
you can send array php jquery this. in example sending object:
<?php if($_server['request_method']=='post'){ header('content-type: text/html'); print_r($_post['allvals']); /* array ( [0] => 1 [1] => 2 [2] => 3 ) */ die; } ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { var myjavascriptarray = new array('1', '2', '3'); //or var myjavascriptarray=["1","2","3"]; $.ajax({ type: "post", datatype: 'html', url: "test.php", data: {'allvals[]=' : myjavascriptarray}, cache: false, success: function(data){ $('#test').html(data); } }); }); </script> </head> <body> <div id="test"></div> </body> </html>
Comments
Post a Comment