skip to Main Content

I have two tables owner and veh. In veh (vehicle) table I have a linked field (owner) with owner table id (ID_O). In my veh form (veh.php) I am displaying existing data in a table but I am not able to populate existing owner names in dropdown list from where I can change name of owner.

Java script code is required to populate names of owners against every record.

2

Answers


  1. If you are a beginner you can follow this.

    <?php 
    
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, regno FROM vehicle";
    $result = $conn->query($sql);
    
    
    $ownerid= (!empty($_REQUEST['owner'])) ? $_REQUEST['owner'] : '';
    
     ?>
    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    </head>
    <script type="text/javascript">
        function loadOwner(){
            $('#frm').submit()
        }
        
    </script>
    <body>
        <form method="post" name="frm" id="frm">
            <select name="owner" id="owner"  onChange="loadOwner()">
                <?php 
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) { ?>
               <option value="<?=$row['id']?>"><?=$row['regno']?></option>
            <?php  }
            }
                 ?>
            </select>
    
    <?php 
    
    if (!empty($ownerid)) {
        
        $sqlowner = "SELECT id, name,address FROM owner where id=".$ownerid;
        $resultowner = $conn->query($sqlowner);
    
        if ($resultowner->num_rows > 0) {
          // output data of each row
          while($rownew = $resultowner->fetch_assoc()) {
            echo "id: " . $rownew["id"]. " - Name: " . $rownew["name"]. "  Address: " . $rownew["address"]. "<br>";
          }
        } else {
          echo "0 results";
        }
    }
     ?>
    </form>
    </body>
    </html>
    Login or Signup to reply.
  2. Sounds like you’re aiming to dynamically populate a dropdown in your veh.php form with owner names based on the linked field. You’ll need a little AJAX magic. Basically, when your veh form loads or when a certain action triggers it, make an AJAX call to a script that fetches the owner names from your database (based on ID_O). Then, use javascript to populate those names into your dropdown. Don’t forget to handle cases where the AJAX call fails or returns no data! Simple but effective, this approach will get those owner names into your dropdown.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search