3

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

6
  • You might want to consider writing the json object directly into the javascript using json encode. Then you wouldnt have to do an ajax call. Commented Jun 21, 2011 at 20:37
  • what do you mean, if the data is on another server how can i add it with json encode Commented Jun 21, 2011 at 20:41
  • When you generate the page with PHP, you can json_encode the products array into the page so that its accessible with javascript. <script>var products = <?php echo json_encode($products); ?>;</script> Commented Jun 21, 2011 at 20:44
  • can you explain how the two code snippets are related. Where are $products coming from and where do you handle the data you post back to the server? Commented Jun 21, 2011 at 20:46
  • I dont have the products array until i make the ajax request though... Commented Jun 21, 2011 at 20:47

5 Answers 5

2

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;
}

?>
Sign up to request clarification or add additional context in comments.

Comments

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.

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

Comments

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.

Comments

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.

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.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.