admin管理员组文章数量:1026989
foreach($data['data'] as $data){
$count = $data['number'];
}
// $data['number']; will return some number like:
159809
359107
249178
... //10+ numbers
Then, how to put each php foreach value into each jquery ajax
? (put every number via a jquery ajax, there will be 10+ ajax calls, and return all the data in one div). Thanks.
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $count; ?>", //each $count value in each ajax
success: function(data){
$("#result").html(data);
}
});
});
</script>
<div id="result"></div>
foreach($data['data'] as $data){
$count = $data['number'];
}
// $data['number']; will return some number like:
159809
359107
249178
... //10+ numbers
Then, how to put each php foreach value into each jquery ajax
? (put every number via a jquery ajax, there will be 10+ ajax calls, and return all the data in one div). Thanks.
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $count; ?>", //each $count value in each ajax
success: function(data){
$("#result").html(data);
}
});
});
</script>
<div id="result"></div>
Share
Improve this question
asked Jun 4, 2011 at 21:24
cj333cj333
2,61921 gold badges70 silver badges111 bronze badges
3
- There's a solution to do what you want, but you should know this is definitely the wrong way to do it. You should pass all 10 items in ONE ajax call, at the least... – Fosco Commented Jun 4, 2011 at 21:27
-
@Fosco, if I make all items in ONE ajax call, like
$count = $data['number'].'a';
then explorea
to get each number inprocess.php
, but howprocess.php
can know how many explored numbers it will have? it also will make a mistake like$explored[0]
,explored[1]
... what is the last one? – cj333 Commented Jun 4, 2011 at 21:32 - you can pass it like an array and then intelligently parse the array on the other side.. You've got a knowledge hole somewhere. – Fosco Commented Jun 4, 2011 at 21:33
2 Answers
Reset to default 2put the 10 values from php array to javascript array, make a javascript loop for 10 times and call ajax function with data from javascript array.
If you really want a unique ajax call for each $count value, with the data returning in the #result div:
<script type="text/javascript">
$(document).ready(function(){
<?php
foreach($data['data'] as $data){
$count = $data['number'];
?>
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $count; ?>",
success: function(data){
$("#result").append(data);
}
});
<?php
}
?>
});
</script>
<div id="result"></div>
However, I would highly remend passing the values as an array and having only one ajax call:
$count = array();
foreach($data['data'] as $data){
$count[] = $data['number'];
}
$datacount = implode('-',$count);
?>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $datacount; ?>",
success: function(data){
$("#result").append(data);
}
});
});
</script>
<div id="result"></div>
On the server side in process.php, you can explode('-',$_POST['items'])
and then foreach through them.
This is just one other way to acplish it... It could be json_encoded or many other ways.
foreach($data['data'] as $data){
$count = $data['number'];
}
// $data['number']; will return some number like:
159809
359107
249178
... //10+ numbers
Then, how to put each php foreach value into each jquery ajax
? (put every number via a jquery ajax, there will be 10+ ajax calls, and return all the data in one div). Thanks.
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $count; ?>", //each $count value in each ajax
success: function(data){
$("#result").html(data);
}
});
});
</script>
<div id="result"></div>
foreach($data['data'] as $data){
$count = $data['number'];
}
// $data['number']; will return some number like:
159809
359107
249178
... //10+ numbers
Then, how to put each php foreach value into each jquery ajax
? (put every number via a jquery ajax, there will be 10+ ajax calls, and return all the data in one div). Thanks.
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $count; ?>", //each $count value in each ajax
success: function(data){
$("#result").html(data);
}
});
});
</script>
<div id="result"></div>
Share
Improve this question
asked Jun 4, 2011 at 21:24
cj333cj333
2,61921 gold badges70 silver badges111 bronze badges
3
- There's a solution to do what you want, but you should know this is definitely the wrong way to do it. You should pass all 10 items in ONE ajax call, at the least... – Fosco Commented Jun 4, 2011 at 21:27
-
@Fosco, if I make all items in ONE ajax call, like
$count = $data['number'].'a';
then explorea
to get each number inprocess.php
, but howprocess.php
can know how many explored numbers it will have? it also will make a mistake like$explored[0]
,explored[1]
... what is the last one? – cj333 Commented Jun 4, 2011 at 21:32 - you can pass it like an array and then intelligently parse the array on the other side.. You've got a knowledge hole somewhere. – Fosco Commented Jun 4, 2011 at 21:33
2 Answers
Reset to default 2put the 10 values from php array to javascript array, make a javascript loop for 10 times and call ajax function with data from javascript array.
If you really want a unique ajax call for each $count value, with the data returning in the #result div:
<script type="text/javascript">
$(document).ready(function(){
<?php
foreach($data['data'] as $data){
$count = $data['number'];
?>
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $count; ?>",
success: function(data){
$("#result").append(data);
}
});
<?php
}
?>
});
</script>
<div id="result"></div>
However, I would highly remend passing the values as an array and having only one ajax call:
$count = array();
foreach($data['data'] as $data){
$count[] = $data['number'];
}
$datacount = implode('-',$count);
?>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "process.php",
dataType: "html",
type: 'POST',
data: "items=<?php echo $datacount; ?>",
success: function(data){
$("#result").append(data);
}
});
});
</script>
<div id="result"></div>
On the server side in process.php, you can explode('-',$_POST['items'])
and then foreach through them.
This is just one other way to acplish it... It could be json_encoded or many other ways.
本文标签: javascripthow to put each php foreach value into each jquery ajaxStack Overflow
版权声明:本文标题:javascript - how to put each php foreach value into each jquery ajax - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661888a2161940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论