Search with a dropdown menu by value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patrioticcow
    New Member
    • Sep 2010
    • 15

    #1

    Search with a dropdown menu by value

    hello,

    I've got this
    Code:
    <td><select name="req" id="req">
            <option value="1">posted trips</option>
            <option value="2">requested trips</option>
        </select></td>
    Code:
    if($type == 'like' && $status='1' && $req='1')
      	{
    		$sql = 'SELECT * FROM topic WHERE (dstate LIKE '."'%$search%') AND status='1' AND req='1'";
      	}
    what i want is, when posted trips option is selected to query just the posts that contain 1 in the "req" in the database.

    it looks like my script has an error somewhere
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Code:
    if($type == 'like' && $status='1' && $req='1')
    this whole line is buggy....

    $status='1'

    this will set value '1' to $status.

    $req='1'

    this will set value '1' to $req. none of them are condition.

    Comment

    • patrioticcow
      New Member
      • Sep 2010
      • 15

      #3
      thanks for reply.

      i can get rid of $status='1'

      but how do i set up the condition for the $req considering the 2 values?

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        I think you are missing some point.
        When you press submit button
        it hit an action page which is defined in form tag.

        if your method is GET then you can receive your data by
        $_GET['req']; // you will get the value

        if the method is POST then you can receive your data by
        $_POST['req'];

        in both case req is defined in the page from where you select the value.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          As was hinted at before, you're assigning data to a variable (=) instead of comparing it(==).

          Comment

          • patrioticcow
            New Member
            • Sep 2010
            • 15

            #6
            i might be missing the point :) but i still don't understand.
            can u point me toward an example?

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              try this small example and fix syntax by yourself

              sub.htm
              Code:
               <form action="test.php" method=get>
                <select name="req_get" id="req">
                       <option value="1">posted trips</option>
                       <option value="2">requested trips</option>
                </select>
                <input type="submit" name="button1_get" value="GET">
                
                <form action="test.php" method=post>
                <select name="req_post" id="req">
                       <option value="1">posted trips</option>
                       <option value="2">requested trips</option>
                </select>
                <input type="submit" name="button1_post" value="POST">
               </FORM>
               </form>
              test.php
              Code:
              <?php
               print_r($_GET);
               print_r($_POST);
              
               if(isset($_GET['button1_get'])==true)
               {
                 echo $_GET['button1_get'];
               }
               else if(isset($_POST['button1_post'])==true)
               {
                 echo $_POST['button1_post'];
               }
              ?>
              save those code two different files according to name. then execute the htm file from your browser and test

              Comment

              • patrioticcow
                New Member
                • Sep 2010
                • 15

                #8
                i've got a little problem integrating the code into my code

                Code:
                if($search1 == '')
                   {
                      $smarty->assign('Error','Please enter any destination!');
                      $z=1;
                   }
                   elseif($type == 'like' && $status='1')
                     {
                $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req";
                }
                my html is
                Code:
                <select name="req" id="req">
                           <option value="1">yes</option>
                           <option value="2">no</option>
                </select>
                i am already putting the values into the database using this:
                Code:
                $sql = 'INSERT INTO topic (req) VALUES '." ('$_SESSION[EmpId]', $req)";
                let me see if i got the right idea on how I should implement the code

                Code:
                if($search1 == '')
                   {
                      $smarty->assign('Error','Please enter any destination!');
                      $z=1;
                   }
                if( $_POST['req'] == 1 || $_POST['req'] == 2 ) {
                     $req = (int) $_POST['req'];
                }
                   elseif($type == 'like' && $status='1')
                     {
                $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req";
                $req = 1;
                }
                what u think?

                Comment

                • patrioticcow
                  New Member
                  • Sep 2010
                  • 15

                  #9
                  i found the answer

                  Code:
                  $req = intval ($_GET['req']);
                  if($type == 'like' && $status='1' && $req > 0 && $req < 3)
                  {
                  $sql = "SELECT * FROM topic WHERE (Title LIKE '%$search1%') AND req= $req"";
                  }

                  Comment

                  Working...