admin管理员组文章数量:1333451
Here is my code,
<?php
$answers = Array( [0] => 13 [1] => 5 [2] => 6 [3] => 11 );
?>
<script>
function loadData1(val) {
var dataToSend = {
'name': val,
'ans[]': <? php echo $answers; ?>
};
$.ajax({
type: 'POST',
data: dataToSend,
url: '<?php echo JURI::ROOT();?>classfiles/sample.php',
success: function (data) {
$('#questions').html(data);
}
});
}
</script>
I want the array values in sample.php
file, but I don't get any output.
Any useful answers are really appreciated.
Here is my code,
<?php
$answers = Array( [0] => 13 [1] => 5 [2] => 6 [3] => 11 );
?>
<script>
function loadData1(val) {
var dataToSend = {
'name': val,
'ans[]': <? php echo $answers; ?>
};
$.ajax({
type: 'POST',
data: dataToSend,
url: '<?php echo JURI::ROOT();?>classfiles/sample.php',
success: function (data) {
$('#questions').html(data);
}
});
}
</script>
I want the array values in sample.php
file, but I don't get any output.
Any useful answers are really appreciated.
Share Improve this question edited Mar 26, 2013 at 6:53 Maehler 6,3411 gold badge43 silver badges48 bronze badges asked Jan 24, 2013 at 9:58 BasithBasith 1,0757 silver badges22 bronze badges 2-
sample.php just contains
<?php $answers = Array ( [0] => 13 [1] => 5 [2] => 6 [3] => 11 ); ?>
? – user1157393 Commented Jan 24, 2013 at 10:01 - 1 have a look at turning your array into a JSON string. – azzy81 Commented Jan 24, 2013 at 10:03
1 Answer
Reset to default 7The line:
var dataToSend = {'name' : val, 'ans[]' : <?php echo $answers; ?> } ;
will print:
var dataToSend = {'name' : val, 'ans[]' : Array } ;
which creates a javascript syntax semantic error (ans = empty string will be posted). Change to:
var dataToSend = {'name' : val, 'ans[]' : <?php echo json_encode($answers); ?> } ;
which prints:
var dataToSend = {'name' : val, 'ans[]' : [13,5,6,11] } ;
本文标签: javascriptHow to pass PHP array values to another file using jQuery AjaxStack Overflow
版权声明:本文标题:javascript - How to pass PHP array values to another file using jQuery Ajax? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340394a2456484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论