?
Solved

Using jQuery and ajax to work with PHP sorting results

Posted on 2007-11-14
10
Medium Priority
?
1,223 Views
Last Modified: 2008-02-01
I am wondering if it is possible and how I would go about doing it if I wanted to use jQuery to sort with my PHP results on my page, does jQuery handle that?

http://critterclassifieds.net/?a=pets for this page
http://critterclassifieds.net/pets.phps - PHP code
Using something like this: http://docs.jquery.com/Ajax


Thanks so much in advance,

Ryan

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20283028
What is your goal? You are wanting to sort your results dynamically?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20283043
I should have been more clear on my question, sorry. Yeah, sort them dynamically. I am using a small bit of ajax for my categories/subcategories for that page.
   Thank you,
     Ryan
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20283109
I am not sure if you can use jquery explicitely, but you can use ajax to do this, quite simply you have to create a php script (which gets called upon by ajax) which sorts and returns the results from the database based on some argument, and you will need an onchange event for your selectionbox to call the ajax function which in turn calls the php script and returns then displays the result in some innerHTML of span or div.

Hope this helps in some way.
0
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20283134
Alrighty, I see. Its more clear now. Makes much more sense. Kind of confused on how to start that up, let me try looking at some tutorials on google, I did that earlier and no go. I think you can do that with jQuery looking at the classes used, I think you are able to use them with that.

Ryan
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20283185
Here is a general ajax function, which should get you started, hope you can understand this ok.

You will need to write your php script to communicate with this ajax function, hope this helps!
Its only an example, so feel free to change any values and modify to suit.


<script language="javascript" type="text/javascript">
function callAjax()
{
      var url = "test.php?user=myself"; // dummy, change to whatever your url and argument s going to be...
      ajaxQuery(url);
}

function ajaxQuery(url)
{
      var page_request = false;
     
      // If  the browser is Mozilla, Safari etc
      if (window.XMLHttpRequest)
      {
            page_request = new XMLHttpRequest();
      }
 
      // If the Browser is Internet Explorer
      else if (window.ActiveXObject)
      {
            try
            {
                  page_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                  try
                  {
                        page_request = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                    catch (e){}
            }
      }
      else { return false; }
       
      page_request.open('GET', url, true);
     
      // When request is Ready
      page_request.onreadystatechange=function()
      {      
            if (page_request.readyState == 4 && page_request.status==200)
            {
                  document.getElementById('results').innerHTML = page_request.responseXML;
            }
      }
      if(window.XMLHttpRequest)
      {
            page_request.send(null);
      }
      else
      {
            page_request.send();
      }
}
</script>

<a href="#" onclick="callAjax();">click for Ajax</a><br><br>
<span id="results">This text is going to change after the ajax call...</span>
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20283280
Okay, nice, let me try this out and mess with it, I might have some questions!

Thanks!
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20283306
I am still kind of confused how I would work in my PHP to that? Working with the variables, etc.?
0
 
LVL 11

Accepted Solution

by:
MacAnthony earned 2000 total points
ID: 20283607
You don't need to use ajax and go back to the server. You could also dynamically sort the data in the table in javascript then you wouldn't need the php script. There is an article on how to do sorting here:

http://www.packtpub.com/article/jquery-table-manipulation-part1

Basically, it's a case of reading the data from each row you want to sort, sort that data and then rebuild the table rows based on the order determined by the data sort.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20283663
Okay nice, let me take a look at this and try it out. Do you think that that tutorial could work with what I am trying to do?
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 20283673
Yes, pretty close to what you need except since you only have 1 table cell, you will have to change the code for reading the values to sort.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question