SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using JS to Modify PHP Database Queries

    I've been working on a PHP script that allows visitors to choose the information they display by modifying the WHERE clause in PHP database table queries at http://www.geozoo.org/Search/Octopus/ I'd like to do something similar - though simpler - with my table at http://cia.geoworld.org/Topics/People-2

    I'm currently using CSS to determine which rows display. (See the "Control Panel" on the right side of the page, just above the table.) I simply zap table rows with display: none.

    But I'd like to get more precise results by modifying my database queries. And rather than use PHP, I thought it would be nice to try JavaScript, so visitors won't have to refresh the page. Then it occurred to me that they would STILL have to refresh the page, since the query they're modifying is PHP - right?

    So that's my basic question. Is it possible to design a page that allows visitors to control a PHP query with JavaScript, displaying a different set of table rows without refreshing the page? And if the answer is yes, are you aware of any working models that I can see as an example?

    Thanks.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The answer is Ajax.
    Saul

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,863
    Mentioned
    5 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by geosite View Post
    So that's my basic question. Is it possible to design a page that allows visitors to control a PHP query with JavaScript, displaying a different set of table rows without refreshing the page?
    You could use AJAX to send values back to the server to be used in the WHERE clause of the query. If someone clicks on something, the value is stored in an array or something until it is time to retrieve information from the server?

    It would seem to me to be a huge security risk if you executed queries that remote users could directly manipulate.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought it would be nice to try JavaScript, so visitors won't have to refresh the page
    That's all well and good, but when programming with javascript, you also have to keep in mind that you need to provide some avenue for people who have javascript disabled to use your site.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    You could use AJAX to send values back to the server to be used in the WHERE clause of the query. If someone clicks on something, the value is stored in an array or something until it is time to retrieve information from the server?

    It would seem to me to be a huge security risk if you executed queries that remote users could directly manipulate.
    Do you think my page at http://www.geozoo.org/Search/Octopus/ is a security risk? If so, what would I have to do to fix it?

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,863
    Mentioned
    5 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by geosite View Post
    Do you think my page at http://www.geozoo.org/Search/Octopus/ is a security risk? If so, what would I have to do to fix it?
    Bandwidth exceeded error... I can't check.

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip; I just increased the bandwidth. That's WAY over specs, because this site doesn't get nearly as much traffic as some of my other sites that generate less than 10% as much bandwidth. GeoZoo just has some huge database tables.

    In the meantime, I just found a tutorial on using AJAX to query a MySQL database. I'd like to eventually figure out how to replace my GeoZoo "Octopus" search function with an AJAX solution.

    Thanks for all the tips.

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,863
    Mentioned
    5 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by geosite View Post
    Thanks for the tip; I just increased the bandwidth. That's WAY over specs, because this site doesn't get nearly as much traffic as some of my other sites that generate less than 10% as much bandwidth. GeoZoo just has some huge database tables.

    In the meantime, I just found a tutorial on using AJAX to query a MySQL database. I'd like to eventually figure out how to replace my GeoZoo "Octopus" search function with an AJAX solution.

    Thanks for all the tips.
    Using AJAX is quite easy once you get a working AJAX function. Let us know if you have any problems.

    One suggestion: Instead of dumping out all the Vertebrates which takes a long time, consumes a lot of server resources, and eats up a lot of bandwidth, you may want to consider having headers for each letter the animal starts with. A, B, C, D, etc.

    Also, if your data changes infrequently, you may want to consider making static copies of your dynamic pages, as discussed in this thread:

    http://www.sitepoint.com/forums/showthread.php?t=460083

    But I agree that AJAX would be a good idea for your site.


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
  •