user188962
user188962

Reputation:

Ajax and PHP help

Im currently calling a php code that displays results from a mysql db by using AJAX so that the page doesnt reload!

Inside the php code, I am creating a menu where the users can chose to display only "private ads" or "all ads".

Currently, "all ads" are displayed in a div container using ajax as mentioned because I havent implemented the "show private only", which is where I need your help...

Is it possible to use AJAX to check if the user clicks on the "show private only" tab in the php code displayed, and then setting a variable to something and sending it to the SAME php code which then uses the variable to display"private ads" only, WITHOUT making a new query to mysql?

If you need more input just tell me... SOME MORE INPUT:

This is what I want:

AJAX is used to check search criteria... AJAX sends criteria to PHP... PHP Checks mysql db for criteria and displays in tables, also creates two links, one for "all ads" and one for "private only"... PHP echoes the display tables... AJAX DISPLAYS the tables in a DIV container in the HTML page (innerhtml=blabla)

UPDATE HERE COMES THE ADDITION I WANT: the users click on one of the links provided by the PHP code, lets say "private only", AJAX reacts and calls PHP code again ... PHP code this time displays the tables differently, filtering out all non-private ads... AJAX displays in the div container...

Is this possible, if so could you point me in the right direction please!

Thanks

Upvotes: 0

Views: 157

Answers (4)

Kon Pal
Kon Pal

Reputation: 574

If I understood correctly you want do filtering on your ads by a criteria. This could easily be done without a second query in php code. Just change your html code to add a class in advertisement entry that describes the category. Then add the buttons that will filter out the unwanted.

HTML:

<a href="#" id="all_ads">Display all ads</a>
<a href="#" id="normal_ads">Display normal ads</a>
<a href="#" id="private_ads">Display private ads</a>
<div id="ads">
<ul>
   <li class="normal">Advertisment 1</li>
   <li class="normal">Advertisment 2</li>
   <li class="private">PRIVATE Advertisment 1</li>
   <li class="normal">Advertisment 3</li>
</ul>
</div>

<!-- Then add the following code to capture click events -->
<script type="text/javascript">
$(document).ready(function() 
{
    $('#normal_ads').click(function() 
    {
        $('#ads li:not(.normal)').hide();
        $('#ads li.normal').show();
        return false;
    });

    $('#private_ads').click(function() 
    {
        $('#ads li:not(.private)').hide();
        $('#ads li.private').show();
        return false;
    });
    $('#all_ads').click(function() 
    {
        $('#ads li').show();
        return false;
    });
});
</script>

This was written from mind, I will cross-check it right away. OK it works.

The advantage of this is that you want have to re-query for every click of the user as all advertisements will sent the first time and JavaScript will filter out the unwanted. You can also add some effects in the show/hide through jquery's effects.

Upvotes: 0

grantwparks
grantwparks

Reputation: 1153

Just add a token to the URL string which the XmlHTTP request goes to?

In other words "my.server.script.php?ads=all" or "my.server.script.php?ads=private" and examine your request variables in the PHP script to determine what to return.

Upvotes: 0

TheMagician
TheMagician

Reputation: 1856

If I understood your question correctly, you could implement it with JQuery with ease.

HTML:
<a href="#" id="normal_ads">Display normal ads</a>
<a href="#" id="private_ads">Display private ads</a>
<div id="ads"></div>

JQuery:
$(document).ready(function() 
{
    $('#normal_ads').click(function() 
    {
        $('#ads').html("").load("ajax_ads.php?normal=1");
        return false;
    });

    $('#private_ads').click(function() 
    {
        $('#ads').html("").load("ajax_ads.php?private=1");
        return false;
    });
});

Upvotes: 0

Zak
Zak

Reputation: 25205

The php should just be outputting the form, so the javascript can definitely check what the form value is before sending and/or displaying the ads and do filtering based on the form value (or letting the server side of the AJAX request do the filtering).

Upvotes: 0

Related Questions