0

I made this work already but somehow it stopped working. I want to display informations from database related to the chosen data from the select box without clicking a button so I used ajax.

The button "Approve Clearance" is used for another action which is I wished to used after the display of data.

There is a div with id #records which is hidden and will show upon display of data.

<div class="card-body">
  <form id="gform" action="action/actionclearance.php" method="POST">
     <label>Student ID</label>
     <select id="stud_id" name="stud_id" class="form-control col-md-6" required>
     <option selected="selected">Select Student ID</option>
     <?php
         $mysqli = new mysqli ('localhost', 'u220931635_arug', 'Smarvcdsl2019', 'u220931635_arug') or die (mysqli_error($mysqli));
         $resultset = mysqli_query($mysqli, "SELECT * FROM tbl_student where delete_flag = 0");
         while( $rows = mysqli_fetch_assoc($resultset) ) {
      ?>
      <option><?php echo $rows["stud_id"]; ?></option>
      <?php } ?>
      </select>
  <br>
  <a href="action/actionclearance.php?gclear=<?php echo $rows["stud_id"]; ?>"><button type="submit" class="btn btn-primary btn-md" name="gclear" id="gclear" class="gclear"><i class="fas fa-check-double"></i> Approve Clearance</button></a>
   </form>
</div>
<div class="card-footer">
      <div id="display">
         <div class="row" id="heading" style="display:none;">
            <tr>
            <th class="text-center">
               <div class="col-sm-6">
                   <strong>Student</strong>
               </div>
            </th>
            </tr>
         </div>
         <div class="row" id="records">
            <tr>
            <td>
               <div class="col-sm-6">
                  <span id="stud_fname"></span>&nbsp;
                  <span id="stud_mname"></span>&nbsp; 
                  <span id="stud_lname"></span>&nbsp;
                  <span id="stud_suffix"></span>&nbsp;
               </div>
             </td>
             </tr>
          </div>         
          <div class="row" id="no_records">
            <div class="col-lg-12 text-center">
                Plese select Student ID to view details</div>
            </div>
          </div>
     </div>
</div>

Below is the ajax code I used to display data without clicking a button.

<script>
    $(document).ready(function(){
        // code to get all records from table via select box
        $("#stud_id").change(function() {
            var id = $(this).find(":selected").val();
            var dataString = 'stud_id='+ id;

            $.ajax({
                url: 'getstudent.php',
                dataType: "json",
                data: dataString,
                cache: false,
                success: function(studentData) {
                    if(studentData) {
                    $("#heading").show();
                    $("#no_records").hide();
                    $("#stud_fname").text(studentData.stud_fname);
                    $("#stud_mname").text(studentData.stud_mname);
                    $("#stud_lname").text(studentData.stud_lname);
                    $("#stud_suffix").text(studentData.stud_suffix);
                    $("#records").show();
                    } else {
                    $("#heading").hide();
                    $("#records").hide();
                    $("#no_records").show();
                    }
                }
            });
        });
    });
</script>

This is the getstudent.php which is connected to the database where ajax gets datas to display.

<?php
    include("../configAdmin.php");

    if($_REQUEST['stud_id']) {
        $mysqli = new mysqli ('localhost', 'u220931635_arug', 'Smarvcdsl2019', 'u220931635_arug') or die (mysqli_error($mysqli));
        $resultset = mysqli_query($mysqli, "SELECT * FROM tbl_student where delete_flag = 0 AND stud_id ='".$_REQUEST['stud_id']."'");

            while( $rows = mysqli_fetch_assoc($resultset) ) {
                $data = $rows;
            }
            echo json_encode($data);
    } else {
        echo 0;
    }
?>
MeriiDiCii
  • 11
  • 5
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 22 '19 at 17:54
  • [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Sep 22 '19 at 17:55

1 Answers1

-1

without using ajax you can do this

<select id="stud_id" name="stud_id" class="form-control col-md-6" required>
     <option selected="selected">Select Student ID</option>
     <?php
         $mysqli = new mysqli ('localhost', 'u220931635_arug', 'Smarvcdsl2019', 'u220931635_arug') or die (mysqli_error($mysqli));
         $resultset = mysqli_query($mysqli, "SELECT * FROM tbl_student where delete_flag = 0");
         while( $rows = mysqli_fetch_assoc($resultset) ) {
      ?>

<option value="<?php echo $rows["stud_id"]; ?>"><?php echo $rows["stud_id"]; ?></option>

      <?php } ?>
      </select>
Samuel
  • 107
  • 1
  • 1
  • 9