How would I, if possible set a php array from an ajax call?
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
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;
}
?>
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.
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.
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.
any url in the following form will automatically create a php array.
http://example.com/foo.php?a[]=hello&a[]=world
精彩评论