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

时间:2022-01-07 01:22:23

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

好的,所以我试图从ajax请求中检索数据,如果可能的话,可能设置一个php数组...我甚至不确定这是否可能,因为一个是客户端,一个是服务器端...所以这是我的代码

on the html page

在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">
 ........  

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调用

$.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

我在想是否有一种简单的方法可以做到这一点....我认为一个解决方案是在ajax调用的成功部分编写html但是我会有很多追加语句...任何更清洁的方式不胜感激

5 个解决方案

#1


3  

Use cURL with PHP

PS: I recommend not mixing your html and php like you are. It is needlessly hard to debug and maintain.

PS:我建议不要像你一样混合你的HTML和PHP。调试和维护是不必要的。

You can use this code (not tested but should work):
Note: make the URL an absolute URL or it won't work

您可以使用此代码(未经过测试但应该可以使用):注意:将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;
}

?>

#2


0  

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.

php变量$ products仅在页面加载/回发时设置(无论他们在php中调用它)。你在原始评估中是正确的,在成功回调中使用追加是一种方法。

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

或者,您可以使用类似jquery模板的方法来获得更清晰的方法。

#3


0  

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回显服务器端的数据?从AJAX url看来它是硬编码/静态的,而不是由于事件处理程序的动态生成。也许一开始是cURL请求?在客户端修改DOM可能会更有效率。

#4


0  

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.

而不是在PHP中做foreach在成功的javascript中执行:函数(数据){}函数就像你建议的那样。

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.

希望有所帮助。

#5


0  

any url in the following form will automatically create a php array.

以下表单中的任何url都将自动创建一个php数组。

http://example.com/foo.php?a[]=hello&a[]=world

#1


3  

Use cURL with PHP

PS: I recommend not mixing your html and php like you are. It is needlessly hard to debug and maintain.

PS:我建议不要像你一样混合你的HTML和PHP。调试和维护是不必要的。

You can use this code (not tested but should work):
Note: make the URL an absolute URL or it won't work

您可以使用此代码(未经过测试但应该可以使用):注意:将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;
}

?>

#2


0  

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.

php变量$ products仅在页面加载/回发时设置(无论他们在php中调用它)。你在原始评估中是正确的,在成功回调中使用追加是一种方法。

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

或者,您可以使用类似jquery模板的方法来获得更清晰的方法。

#3


0  

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回显服务器端的数据?从AJAX url看来它是硬编码/静态的,而不是由于事件处理程序的动态生成。也许一开始是cURL请求?在客户端修改DOM可能会更有效率。

#4


0  

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.

而不是在PHP中做foreach在成功的javascript中执行:函数(数据){}函数就像你建议的那样。

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.

希望有所帮助。

#5


0  

any url in the following form will automatically create a php array.

以下表单中的任何url都将自动创建一个php数组。

http://example.com/foo.php?a[]=hello&a[]=world