SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Location
    Kansas City
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help - href in PHP to pass two array variables to AJAX and return new PHP array

    PHP code below. As you know onClick doesn't work, client side. I would like to use AJAX but not familiar with to display a "div" below list of images

    <ul>
    <?php


    /*************************************
    * Get company items
    *************************************/

    $company_items_array = $hd->view_items_by_company($company_id,$db);
    //var_dump($company_items_array);

    $count = count($company_items_array);
    for($i = 0; $i < $count ; $i++) {


    echo "<li><a class='view_item' href='#' ".$company_items_array[$i]['item_number']."><img src='images/".$company_items_array[$i]['item_number']."-1_nav.jpg' alt='".$company_items_array[$i]['title']."' width='96' height='86' class='pic-fix'/></a></li>";


    } ?>

    </ul>

    Here's the PHP array to return:

    $company_item_array = $hd->view_item_by_company($item_number,$company_name,$db);

    To populate "div"

    Does anyone have a suggestion or code examples? Perhaps a good book on AJAX, too? Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Probably the best way to do it in this circumstance would be to have your href element link to a working page. Doing this would make the page and links works correctly for visitors without javascript enabled as well. You could then use jQuery's .load() function to cherry pick the content for users who have javascript enabled.

    Assuming you want to load the result into a div with the class of 'item_data'.
    Code JavaScript:
    // this will capture any click event on an item with the class
    // of view_item
    $('.view_item').click(function(e) {
     
        // 'e' refers the click event itself and peventDefault() will
        // stop the browser executing it's default action and loading the page
        e.preventDefault();
     
        // 'this' refers to the element which was clicked.
        // The .attr() method will retrieve the value of the 'href' element.
        var url = $(this).attr('href');
     
     
        // fetch all of the html content from your target url that is within 
        // the element with id item_information and load it into your
        // item_data container on the current page.
        $('#item_data').load(url + ' #item_information');
    });

    If your interested in learning jQuery/javascript I highly recommend Codecademy. If you want to learn to make an actual Ajax call to retrieve json or xml you could start here, and there are a thousand other resources out there.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •