如果可能的话,我如何从 ajax 调用中设置 php 数组?
好吧,我正在尝试从 ajax 请求中检索数据,如果可能的话,可能会设置一个 php 数组...我什至不确定这是否可能,考虑到一个是客户端,一个是服务器端...所以这是我的代码
在 html 页面上
<?php foreach ($products as $product): ?>
<li>
<h3><span><?php print $product["price"]; ?></span> (4) <?php print $product["name"]; ?> </h3>
<div class="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="42"><span class="qty"><?php print $product["quanitity"]; ?></span></td>
<td width="180">
........
,我想循环遍历这个数组中的所有产品,但为了获得我需要进行像这样的 ajax 调用的产品,
$.ajax({
type: 'post',
url: "/shop_systems/index.php?route=module/cart/get_all_products",
dataType: 'json',
data: {current : null, previous : these_ids, quantity : 1, multiple : true},
success: function (data) {
我在想是否有一种简单的方法可以做到这一点......我在想一种解决方案是将 html 写入success
是 ajax 调用的一部分,但我会有很多 append
语句...任何更简洁的方法将不胜感激
Ok so i am trying to retrieve data from an ajax request and possibly if possible set a php array ...I am not even sure if this is possible considering one is client side and one is server side...so here is my code
on the html page
<?php foreach ($products as $product): ?>
<li>
<h3><span><?php print $product["price"]; ?></span> (4) <?php print $product["name"]; ?> </h3>
<div class="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="42"><span class="qty"><?php print $product["quanitity"]; ?></span></td>
<td width="180">
........
and I want to loop through all the products in this array but to get the products I need to make an ajax call like this
$.ajax({
type: 'post',
url: "/shop_systems/index.php?route=module/cart/get_all_products",
dataType: 'json',
data: {current : null, previous : these_ids, quantity : 1, multiple : true},
success: function (data) {
I was thinking if there was an easy way to do this ....I was thinking that one solution would be to write the html in the success
part of the ajax call but I would have lots of append
statements...any cleaner way would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 cURL 与 PHP 结合使用
PS:我建议不要像现在这样混合使用 html 和 php。调试和维护是不必要的困难。
您可以使用此代码(未经测试但应该有效):
注意:URL必须是绝对URL,否则无效
Use cURL with PHP
PS: I recommend not mixing your html and php like you are. It is needlessly hard to debug and maintain.
You can use this code (not tested but should work):
Note: make the URL an absolute URL or it won't work
php 变量
$products
仅在页面加载/回发时设置(无论他们在 php 中如何称呼它)。您最初的评估是正确的,在success
回调中使用append
是一种方法。或者,您可以使用 jquery templates 等稍微简洁的方法。
The php variable
$products
is only set on page load/postback (whatever they call it in php). you're correct in your original assessment that usingappend
in thesuccess
callback was one way to do it.alternatively, you could use something like jquery templates for a slightly cleaner approach.
是否有机会在页面加载时进行调用,并在页面构建时使用 PHP 在服务器端回显数据?从 AJAX url 看来,它是硬编码/静态的,而不是作为事件处理程序的结果动态生成的。也许一开始就是一个 cURL 请求?它可能比在客户端修改 DOM 更有效。
Is there any chance to make the call on page load, and use PHP to echo out the data on the server side as the page is built up? It appears from the AJAX url that is hardcoded/static, as opposed to being dynamically generated as the result of an event handler. Maybe a cURL request in the beginning? It would probably be more efficient then modifying the DOM on the client side.
问题有点不清楚。但这是我最好的镜头:
不要在 php 中执行 foreach,而是在 javascript 中执行 foreach 的 success: function (data) {} function 就像您建议的那样。
你会做类似的事情:
编辑:
希望有帮助。
Question is sort of unclear. But here's my best shot:
Instead of doing the foreach in php do it in javascript in the success: function (data) {} function like you suggested.
You'd do something like:
EDIT:
Hope that helps.
以下形式的任何 url 都会自动创建一个 php 数组。
http://example.com/foo.php?a[]=hello&a[]=world
any url in the following form will automatically create a php array.
http://example.com/foo.php?a[]=hello&a[]=world