如果可能的话,我如何从 ajax 调用中设置 php 数组?

发布于 2024-11-16 10:18:07 字数 1066 浏览 0 评论 0原文

好吧,我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无风消散 2024-11-23 10:18:07

将 cURL 与 PHP 结合使用

PS:我建议不要像现在这样混合使用 html 和 php。调试和维护是不必要的困难。

您可以使用此代码(未经测试但应该有效):
注意:URL必须是绝对URL,否则无效

$post_data = array(
    'current' => 'null',
    'previous' => 'null',
    'quantity' => '1',
    'multiple' => 'true'
);

// Init curl thing
$ch = curl_init();

// Setup curl options
curl_setopt_array($ch, array(
    CURLOPT_URL => '/shop_systems/index.php?route=module/cart/get_all_products',
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE, // Return content as string
    CURLOPT_FOLLOWLOCATION => TRUE, // Follow redirects
    CURLOPT_AUTOREFERER => TRUE, // Set referer field on redirect
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_COOKIEJAR => '/tmp/curl_php_cookies',
    CURLOPT_COOKIEFILE => '/tmp/curl_php_cookies',
    CURLOPT_STDERR => fopen('/tmp/curl_php_log', 'w'),
    CURLOPT_POSTFIELDS => $post_data
));

// Excecute request
$product = json_decode(curl_exec($ch));

$html = '';

foreach ($products as $product)
{
    $html .= <<< HTML
    <li>
        <h3>
            <span>{$product["price"]}</span> 
            (4)  {$product["name"]}
        </h3>
        <div class="container">
            <table  width="100%" border="0" cellspacing="0" cellpadding="0">
     <tr>
     <td valign="top" width="42"><span class="qty">{$product["quanitity"]}</span></td>
     <td width="180">
HTML;
}

?>

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

$post_data = array(
    'current' => 'null',
    'previous' => 'null',
    'quantity' => '1',
    'multiple' => 'true'
);

// Init curl thing
$ch = curl_init();

// Setup curl options
curl_setopt_array($ch, array(
    CURLOPT_URL => '/shop_systems/index.php?route=module/cart/get_all_products',
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE, // Return content as string
    CURLOPT_FOLLOWLOCATION => TRUE, // Follow redirects
    CURLOPT_AUTOREFERER => TRUE, // Set referer field on redirect
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_COOKIEJAR => '/tmp/curl_php_cookies',
    CURLOPT_COOKIEFILE => '/tmp/curl_php_cookies',
    CURLOPT_STDERR => fopen('/tmp/curl_php_log', 'w'),
    CURLOPT_POSTFIELDS => $post_data
));

// Excecute request
$product = json_decode(curl_exec($ch));

$html = '';

foreach ($products as $product)
{
    $html .= <<< HTML
    <li>
        <h3>
            <span>{$product["price"]}</span> 
            (4)  {$product["name"]}
        </h3>
        <div class="container">
            <table  width="100%" border="0" cellspacing="0" cellpadding="0">
     <tr>
     <td valign="top" width="42"><span class="qty">{$product["quanitity"]}</span></td>
     <td width="180">
HTML;
}

?>
沉鱼一梦 2024-11-23 10:18:07

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 using append in the success callback was one way to do it.

alternatively, you could use something like jquery templates for a slightly cleaner approach.

魂归处 2024-11-23 10:18:07

是否有机会在页面加载时进行调用,并在页面构建时使用 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.

只涨不跌 2024-11-23 10:18:07

问题有点不清楚。但这是我最好的镜头:

不要在 php 中执行 foreach,而是在 javascript 中执行 foreach 的 success: function (data) {} function 就像您建议的那样。

你会做类似的事情:

编辑:

var container_node = $('#your_container_id');
 $.each(data, function() { this['price'] ... document.createElement ... container_node.append ... do what you did in php but manipulating the DOM ... }

希望有帮助。

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:

var container_node = $('#your_container_id');
 $.each(data, function() { this['price'] ... document.createElement ... container_node.append ... do what you did in php but manipulating the DOM ... }

Hope that helps.

反差帅 2024-11-23 10:18:07

以下形式的任何 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文