rapidoodle
rapidoodle

Reputation: 350

Getting the option value without using submit button

Good day. Im trying to have some functionality on my project, but im stuck with this.

I want to display my database depending on what the user select

Here is my html code:

        <table>
        <tr><td>Select Request Status:</td>
        <td><select class="form-control" name="status" style="width:200px;">
        <option value="1">Pending Request</option>
        <option value ="2">Requests On-Process</option>
        <option value="3">Unassigned Requests</option>
        <option>All Requests</option>
        </select></td>
        </tr>
        </table>

And here is my php code:

     if ($status = $_SESSION['status'] == 'Pending Request')
{
    $query = "select * from tblrequest where status='Pending Request'";
        $request = mysql_query($query)or die(mysql_error());?>
        <table class="table">
        <tr>
                <th>Request ID</th>
                <th>Requestor</th>
                <th>Department</th>
                <th>Category</th>
                <th>Assigned to</th>
                <th>Status</th>
                <th>Created Date</th>
                <th>Created Time</th>
                </tr>
        <?php while($row = mysql_fetch_array($request)){
        ?>
        <tbody class="table table-hover">
            <tr>
    <td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
    <td style="width:100px;"><?php echo $row['Requestor']; ?></td>
    <td style="width:100px;"><?php echo $row['Department']; ?></td>
    <td style="width:50px;"><?php echo $row['category']; ?></td>
    <td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
    <td style="width:100px;"><?php echo $row['status']; ?></td>
    <td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
    <td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}

}?>

Im a newbie to php so please help me.

Upvotes: 0

Views: 2893

Answers (2)

Krish R
Krish R

Reputation: 22721

Can you try this,

HTML:

       <form method="post">
            <table>
            <tr><td>Select Request Status:</td>
            <td>
              <?php $Statuses = array("0"=>"All Requests", "1"=>"Pending Request", "2"=>"Requests On-Process", "3"=>"Unassigned Requests" );?>

             <select class="form-control" name="status" style="width:200px;" onchange="this.form.submit();">
                <?php foreach($Statuses as $key=>$status):?>
                     <?php 

                     $selected ="";
                      $statusPost = $_POST['status'];
                      if($key == $statusPost): $selected =" selected"; endif;
                     ?>                    
                  <option value="<?php echo $key;?>" <?php echo $selected;?>><?php echo $status;?></option>
                <?php endforeach;?>

            </select></td>
            </tr>
            </table>
        </form>

PHP:

  if (isset($_POST['status'])){       
       $status= $_POST['status'];
       $Where ='';
          if($status!='0'){         
             $Where =" where status='".$Statuses[$status]."' ;           
          }
    }
       $query = "select * from tblrequest  $Where";
       $request = mysql_query($query)or die(mysql_error());?> 
      <table class="table">
        <tr>
                <th>Request ID</th>
                <th>Requestor</th>
                <th>Department</th>
                <th>Category</th>
                <th>Assigned to</th>
                <th>Status</th>
                <th>Created Date</th>
                <th>Created Time</th>
                </tr>
        <?php while($row = mysql_fetch_array($request)){
        ?>
        <tbody class="table table-hover">
            <tr>
    <td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
    <td style="width:100px;"><?php echo $row['Requestor']; ?></td>
    <td style="width:100px;"><?php echo $row['Department']; ?></td>
    <td style="width:50px;"><?php echo $row['category']; ?></td>
    <td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
    <td style="width:100px;"><?php echo $row['status']; ?></td>
    <td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
    <td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
   ?>

Upvotes: 1

Aniket Sahasrabudhe
Aniket Sahasrabudhe

Reputation: 144

put select dropdown in a form with id,action,method etc. give id to a select element(say "mySelect")

Now, in jquery,

$("#mySelect").on("change",function(){
if($("#mySelect").val() != ""))
{
   $("#form1").submit();
}
})

Upvotes: 1

Related Questions