views:

149

answers:

5

I am currently sending a query variable to a php file like this:

      query = "?price_from=" +price_from+ "&price_to=" + price_to etc etc...

I want to populate this query variable by using a loop to check if the user has chosen any search criteria in dropdown lists that exists on the page... It is only optional to chose the search criterias...

How do i do this?

Remember if the drop lists have the default value of 001, then the user havent entered any criteria (ex price range). So then i want the loop to skip this drop list, and move to the next. And if it finds any value other than 001, I want it to populate the query variable.

The main problem is that the php file uses var names such as "price from" and "price to" from the query variable, and if nothing is set in the variable "price to" then it will just print "undefined" or something...

Help!

A: 

for the html why dont you put the form method to GET .. and submit the form with AJAX. Why does it bother you the default values ?

You can do verifications on the php side and decide there witch filters are active

$price = (isset($_GET['price'])) ? $_GET['price'] : false;
solomongaby
i am submitting my form with ajax get method, but there are many forms in one page, so thats why i want to use a loop to get the element ids and then if the values are NOT 1, then add them to an array with which I then send to the php file with GET...
pesar
currently i have thought of this...Elem=getelementbyid(FORM).elements...For (i=0; i<Elem; i++) do something, which i dont know what!
pesar
A: 
<html>
 <?php
     $where = '';
     foreach($_GET as $key => $value) {
        if($value!='001') {
            $where .= ' AND ' . $key . '="' . $value . '"';
        }
     }
     $where = substr($where,4);//get rid of left 'AND'
     $sql = 'SELECT * FROM my_table WHERE ' . $where . ' ORDER BY id ASC';
 ?>
</html>
Cory Dee
this makes sense... but how would you insert each key value to a query which check a mysql database for it?
pesar
Gimme a sec to edit it.
Cory Dee
Please don't forget to escape the values submitted (never trust a user)! Also having the name of the database field as the key for the `$_GET` might turn out to be a security risk.And remember that the `$_GET`-variable might contain other keys, for example a session id or something else. This would then be sent to the database and your results would be erroneous at least.
Cassy
I agree, I was just writing the basics and letting him fill in the blanks. I would assume there would be error checking on at least some of the fields to double check for SQL injection and valid variable types, as well as the every trusty mysql_real_escape_string (and trim).
Cory Dee
Pesar: Any luck?
Cory Dee
A: 

This is a javascript question (you will need to grab the form values and determine if it is set via javascript). Using a js library like jQuery would make this process quite easy. That said, I will answer your question using PHP:

foreach ($_GET as $k => $v) {

 if ($v) {

  $uri[] = $k.'='.$v;

 }

}

$url = 'http://url.com/submit.php?'.implode('+', $uri);
jusunlee
A: 
$url = $_SERVER['SCRIPT_NAME'] . '?';

foreach ($_GET as $key => $value)
{
    $url .= $key . '=' . $value . '&';
}
Cesar
A: 
     <html> 
     <?php 

      foreach($_GET as $key => $value) { 
      if($value!='001') { 
            echo $key . ' has a value of ' . $value . '<br>'; 
      } 
      } 

       ?> 
      </html>

how can i implement a loop into this that takes all values of keys and adds to a query and then checks mysql database for it?

pesar
The commend on this post was fine, you don't need to repost the question :D
Cory Dee