Page 1 of 1

Submit and links canceling the search query

Posted: Fri Nov 25, 2011 3:58 pm
by MikeSpider
Hi guys,

My project has a script that allows users/admins to make search and fetch queries with some parameter they can choose.
For example: Search for people that work night shift under 21 age. The way I made it is:
In the page there are some default items, like a welcome page, some items from database as well. When the user/admin
selects the parameter in the dropbox and clicks "search", the query is executed and results are returned and dysplayed on same page.
the welcome message now disappears. Everything is fine to this point.

However, problem starts when I found the need to submit something that is in the search query itself.
For example, if I want to send the search results by email. Or worst, found a big problem when using a pagination system.
It seems that everytime a button is clicked, all the display from the search query vanishes, and returns to the initial page with the welcome message.
Same problem with the pagination, when a page number is clicked, it "cancels" all the search query as if it looses the query memory.

I think my problem could be in the conditionals I use for the search.:
Which is something like:

Code: Select all

....
<?php 

if("Search" == $_POST['submit']){

       $_age = FilterData($_POST['age']);
        $_period = FilterData($_POST['period']);

if("" != $_age && "" != $_period){

             $search=true;
             $members = new Search($_age,$_period);
             $members->AdminViewMembers();
        $message = "The table is organized by age  ". $_age . " and working period " . $_period;

?>
<?php AdminHeader()?>

<div id="Admin_maincol">
  
   <h1>Manage Users</h1>
<br/>

<?php if(true == $search){
                 
         ?>

   <table>
     <form method="post" action="<?php echo($_SERVER['PHP_SELF'])?>">
<tr>
        <td> <input type="submit"  class="button" id="submit" name="submit" value="Send"></td>
          
     </form>
</tr>
<?php
            //loop through data set
          if($message) echo $message;
            for($counter = 0;$counter < count($members->data);$counter++){ ?>
     <tr>
         
         <td>             
                 <?php echo($members->data[$counter]['user']);?>
                             
               </td>
           
         <td><?php echo($members->data[$counter]['name']);?></td>
         <td><?php echo($members->data[$counter]['region']);?></td>
         <td width="20px"><?php echo($members->data[$counter]['age']);?></td>

 </tr>
</table>

<?php } ?>

<?php }else{ ?>

<div id="welc_msg">
WELCOME .... BLAH BLAH BLAH

</div>

<br>

<form method="post" action="<?php echo($_SERVER['PHP_SELF']);?>#users">

 <table>
<tr>
              <td><label for="age" class="form_txt">User Age</label></td>
            <td><?php if($message_age){echo("<p class='error_msg'>{$message_age}</p>");}?>
                        <select name="age">
                    <option value="">Select age</option>
                       <option value="15">10-15 years </option>
                      <option value="18">15-18 years </option>
                        <option value="25">18-25 years </option>
                          <option value="35">25-35 years</option>
                          <option value="40">35-40 years </option>
                          <option value="50">40-50 years</option>
                          <option value="50+">More than 50 years</option>
                          </select>

            </td>
</tr>
      </table>
</form>
<?php AdminFooter(); ?>
          
I think the problem might be that when I submit anything or press a pagination link while displaying the search results
the "$search" becomes " = false"; I dont know if i'm doing it the wrong way as I only have just a year of PHP .

Any help or clue will be much welcomed and appreciated,
Thanks in advance,
Mike Spider

Re: Submit and links canceling the search query

Posted: Fri Nov 25, 2011 5:13 pm
by manohoo
Hi Mike, your code is very difficult to read, as you embed html into php, and viceversa, without a clear indentation structure. I recommend that you stay in PHP and echo any HTML statements. Here is an example. Instead of:

Code: Select all

     <tr>
         
         <td>             
                 <?php echo($members->data[$counter]['user']);?>
                             
               </td>
           
         <td><?php echo($members->data[$counter]['name']);?></td>
         <td><?php echo($members->data[$counter]['region']);?></td>
         <td width="20px"><?php echo($members->data[$counter]['age']);?></td>

 </tr>
why not do this:

Code: Select all

echo "<tr>";
   echo "<td> {$members->data[$counter]['user'])} </td>";
   echo "<td> {$members->data[$counter]['name'])} </td>"
   echo "<td> {$members->data[$counter]['region'])} </td>";
   echo "<td width='20px'> {$members->data[$counter]['age'])} </td>";
echo "</tr>";
Anyway, I found a few things:
1. <?php AdminHeader()?> should be <?php AdminHeader(); ?> (missing ";")
2. You define $search=true, but a few lines later you have: if(true == $search)...
3. If the code inside the "if" statements get too long, then create functions to improve readability. It's hard for me to figure out the end of each one of your "if" structures.

Re: Submit and links canceling the search query

Posted: Fri Nov 25, 2011 7:16 pm
by MikeSpider
OK, right,
1 - <?php AdminHeader(); ?> was a mistake I made writing in here, but in my actual code is right.
2 - I defined $search=true , so that I can conditioned the display, that's why I later say, if(true == $search) ,i.e. if a search is made, display search results.
else, display welcome message.

I dont understand why when I click a link or a submit buttom, it cancels the search.

Re: Submit and links canceling the search query

Posted: Fri Nov 25, 2011 9:17 pm
by thinsoldier
MikeSpider wrote:I dont understand why when I click a link or a submit buttom, it cancels the search.
It looks like in order for your search code to be triggered the request must contain both
$_POST['age'] and $_POST['period'] and both values must not be empty AND it must contain $_POST['submit'].

When you click a link, only the query string defined on the link go with the request and they go with $_GET and not $_POST.

So even if you had a link like <a href="thispage.php?submit=Search&age=11&period=week">click here</a>
it would not work because your query string is going to wind up in $_GET and not $_POST. So your if("Search" == $_POST['submit']) would never be true with a link.

The first form in your code is sending "Send" as the value of $_POST['submit'] but your main IF statement is looking for a value of "Search". Obviously "Search" != "Send".

I suggest you collab with another developer to do regular code reviews. Once you get this working you should ask someone else to just re-arrange your working code to their liking and see what changes they made and why. Meanwhile you rearrange something they wrote.