tags:

views:

83

answers:

4
+1  Q: 

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

+1  A: 

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

Zak
A: 

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;
    });
});
TheMagician
Thing is that the part where you create two "a hrefs" is implemented inside my php code, so the "show private" or "show all" only displays for the user after php has been called. Please help?
Camran
So the links and ads are created on the fly using AJAX?You can change the .click(function() event to .live('click', function() so the links will work even if that's the case.You can also load the ads on the fly by adding the following code line inside the .ready() event: $('#ads').html("").load("ajax_ads.php?normal=1");
TheMagician
A: 

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.

grantwparks
but Im displaying the option to choose first after the search has been made the first time... So the HTML doesnt contain the option "private" or "all", but the php does... I dont think what I want is possible yet :)
Camran
It almost sounds like it will never be possible, if I'm understanding you. The displayed page contains the results of a search that the user never had any input on.
grantwparks
You're AJAX request is being invoked via javascript, right? onchange attribute of a <select> with <option>s of all or private, then build javascript request to PHP server side with the <select>'s value.
grantwparks
+1  A: 

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.