I am trying to change my code to use json on recommendation from a previous question to simplify things a bit…
On client side:
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.tablednd_0_5.js"></script>
<script type="text/javascript" src="../js/jquery.json-2.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#table').tableDnD();
});
function sendData() {
data = $('#table').tableDnDSerialize();
alert(data); // shows expected data
document.dataform.data.value = $.toJson(data);
document.data.submit();
}
</script>
<form action="$php_page_name" method="post" name="dataform" onSubmit="sendData()">
<input type="hidden" name="data" />
<input type="submit" value="Submit" />
</form>
The js alert outputs the expected array, which i think is converted to a string by this point. But when I submit form.data, my php:
$data = json_decode($_POST['data']);
print_r($data);
print_r($_POST);
returns only:
Array ( [data] => )
Any ideas why nothing is being passed ?
Cheers,
Andy
You need to wait for the return of the function:
Otherwise the form will be submitted immediatly and does’nt wait till data is changed.
inside the function replace this
with this:
Furthermore: assuming you use this as jquery.json-2.2.js :
http://code.google.com/p/jquery-json/downloads/detail?name=jquery.json-2.2.js&can=2&q=
The method-name is
$.toJSONinstead of$.toJson