0

I know this is a newbie question, but I completely forgot how to do this. A specific SQL query should be selected whenever I change the dropdown selection. For example, when i select 'LOAN', all accounttitle row with 'LOAN' in it should be displayed in the table, and when I select 'ADVANCE', it should change all the data in the table and display only the data with 'ADVANCE' in it.

I tried to set it by refresh, unfortunately it won't switch to 2nd option.

This is my option

<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" name="select_account_title" style="text-transform:uppercase" required>
<?php 
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
    $_POST['accountcode']= $row['accountcode'];
    $_POST['accounttitle']= $row['accounttitle'];
    echo "<option value=".$_POST['accountcode'].">".$_POST['accounttitle']."</option>";
}
?>        
</select>
</div>

This is my query

$sql = "SELECT referenceno, employeeidno, accounttitle, 'ON PROGRESS' as debit, postedby, approvedby, notedby, credit FROM earningsamendment where accounttitle= '" . $_POST['accounttitle'] . "'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){

It runs with no error, unfortunately it only selects the last part of the dropdown, no changes is happening when i change the dropdown value.

EDIT: This is the the <form> and <table> is located.

        <div class="col-xs-12">
          <div class="box">
                <form class="form-inline">
            <div class="box-header with-border">
              <a href="#addnew" data-toggle="modal" class="btn btn-primary btn-sm btn-flat"><i class="fa fa-plus"></i> New</a>  
<div class="form-group">              
<?php
$sql = "SELECT accountcode, accounttitle, accounttype FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
?>
<label for="select_account_title" class="col-sm-3 control-label">Select Account Title</label>
<div class="col-sm-9">
<select class="form-control" id="select_account_title" name="select_account_title" style="text-transform:uppercase" onchange="this.form.submit()" required>
<?php 
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
    $value = $row['accountcode'];
    $value2 =$row['accounttitle'];

    $_POST['accountcode']= $value;
    $_POST['accounttitle']= $value2;

    echo "<option value=".$_POST['accountcode'].">".$_POST['accounttitle']."</option>";
}
?>        
</select>
</div>
                  </div>
                </form>
            <div class="box-body">
              <table id="example1" class="table table-bordered">
                <thead>
                  <th>Reference No.</th>
                  <th>Employee ID</th>
                  <th>Account Title</th>
                  <th>Amount</th>
                  <th>Activity</th>
                  <th>Posted By</th>
                  <th>Validated By</th>
                  <th>Noted By</th>
                  <th>Tools</th>
                </thead>
                <tbody>
                <?php

                    $sql = "SELECT referenceno, employeeidno, accounttitle, 'ON PROGRESS' as debit, postedby, approvedby, notedby, credit FROM earningsamendment where accounttitle= '" . $_POST['accounttitle'] . "'";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                        <tr>
                          <td>".$row['referenceno']."</td>
                          <td>".$row['employeeidno']."</td>
                          <td>".$row['accounttitle']."</td>
                          <td>".$row['credit']."</td>
                          <td>".$row['debit']."</td>
                          <td>".$row['postedby']."</td>
                          <td>".$row['approvedby']."</td>
                          <td>".$row['notedby']."</td>
                          <td>
                            <button class='btn btn-success btn-sm edit btn-flat' data-id='".$row['referenceno']."'><i class='fa fa-edit'></i> Edit</button>
                            <button class='btn btn-danger btn-sm delete btn-flat' data-id='".$row['referenceno']."'><i class='fa fa-trash'></i> Delete</button>

                        " ?>
                            <?php if (empty($row['approvedby'])) { echo " <button class='btn btn-warning btn-sm approve btn-flat' data-id='".$row['referenceno']."'><i class='fa fa-check-square-o'></i> Approve</button> "; } ?>

                            <?php "</td>
                        </tr>
                      ";
                    }

                  ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </section>   
  </div>
pjustindaryll
  • 377
  • 3
  • 14
  • I'm not sure what you mean by: "I tried to set it by refresh, unfortunately it won't switch to 2nd option." Also, I don't see any code that would refresh the data. Is it possible to help clarify what you've coded and what specifically goes wrong? One idea is to submit the form whenever the ` – showdev Jul 20 '19 at 05:01
  • I think you're asking how to execute the query and output some data when the dropdown changes. Is that right? Note that manually setting a `$_POST` value [will not repost the data to the page](https://stackoverflow.com/a/3418136/924299). You might want to include a `
    ` element and [submit the form when the `
    – showdev Jul 20 '19 at 05:58
  • @showdev yes, correct, I'm actually trying ````onchange="this.form.submit()"```` but it does not change the output. – pjustindaryll Jul 20 '19 at 06:29
  • @showdev just to clarify on the guide you gave, I should enclose the table with a ````
    ````, right?
    – pjustindaryll Jul 20 '19 at 06:37
  • I'm not sure where your `` is located, but enclose at least the `
    – showdev Jul 20 '19 at 06:48
  • @showdev I edited my post, I'm doing this also, but no luck. ```````` – pjustindaryll Jul 20 '19 at 06:52
  • Using that method, you'd get the value from the [`$_GET`](https://www.php.net/manual/en/reserved.variables.get.php) array. – showdev Jul 20 '19 at 07:03
  • @showdev Is my setup correct? In my edited post? – pjustindaryll Jul 20 '19 at 07:08

1 Answers1

0

all php is run on the server so a value set on the client (the chosen menu item) is run after the php and cannot be used in php. Rewrite your code to either get all possible values from php stored in javascript or use an asynchronous call to php from the javascript.

David
  • 65
  • 2
  • 8