I am trying to minimise postback in my web app. I have a number of inter-dependent dropdowns, an onselectedindexchange event fires for each one which obviously causes a postback. Is there a way I can get all my data from a dataset\datareader into some javascript arrays and开发者_运维问答 then use client side events to handle the inter-dependent dropdowns? Any code examples?
Sure, just echo the JSON encoded data inside a script
tag:
<head>
<script type="text/javascript">
var dataset = <?php echo json_encode($dataSetAsPhpArray); ?>
</script>
</head>
dataset
will be a JS array or object depending on what kind of PHP array $dataSetAsPhpArray
was. Numerically indexed array results in JS array, while assoc array results in JS object.
Use Javascript Object Notation. JSON is a method for other languages to pass information to javascript. PHP has a function encode_json( $array_of_data ); You should be able to do something like:
Header( 'Content-type:json' );
//gather data
echo json_encode( $your_data );
If you are using jQuery the Javascript code would look like
$.ajax({
url: your_url,
type: GET or POST,
dateType: json,
success: function(){
//manipulate dom here
}
});
精彩评论