简体   繁体   中英

How to get a data from database where i using select option so it can be shown in inputbox

File for get data from database

public function getOneByName($name){
try{
    $link = DB_Helper::createMySQLConnection();
    $query = "Select * From vendor Where vendor_name=?";
    $stmt = $link->prepare($query);
    $stmt->bindValue(1,$name,PDO::PARAM_STR);
}catch(PDOException $e){
    $link->rollBack();
    $e->getMessage();
    die();
}
$link =null;
return $stmt;
}

when click select option, how to show the data into inputbox?

<label>ID</label><input type="text" id="id_vendor" name="id_vendor" /> <br/>
<label>Email</label><input type="text" id="email" name="email" /> <br/>
<label>Date</label><input type="date" id="start_date" name="start_date" /> <br/>

<select name="type" class="form-control">
<option  selected="type" disabled=""> </option>
<?php echo $vendor ?>
</select>

<label>Date</label><input type="date" id="date_done" name="date_done" />

example output

Load jquery in head:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

Take id in select field:

<select name="type" id="type" class="form-control">

Use ajax call to get and show data into input box:

$(document).on('change','#type',function(e) {
  e.preventDefault();
    $.ajax({
      url: '#Call funtion from here#',
      type: 'POST',
      dataType : 'json',
      data: {type:$('#type').val()},
      success: function(response) {
        if(response.status == true) {
          // Check data using console log for debug
          console.log(response.data);

          // Set data
          $('#id_vendor').val(response.data....);
        }
      }
    });
});

Php:

return json_encode([
  'status' => true
  'data' => $stmt
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM