How can I pre-select a dropdown option using previously submitted data from database, MySQLI

Paul Ruocco

I have two tables: orders and companies. Note that both tables has a company_id row.

Orders Table:
order_id | order_name | company_id | .....

Companies Table:
company_id | company_name | .....

I have a page with a form that INSERTS data for an order into the orders table in my database. Part of the order is selecting a company from the companies table to go with it, so I use a dropdown for this.

I also have another page with a form that UPDATES that data (an edit page). When I click on the button to bring up the edit page, I'd like to have the previously submitted data in the input fields. With text fields, I just use value="<?php echo $company['whatever']; ?>". However, with dropdown menu's I can't figure out how to pre-select the correct option.

Here is my URL structure:

http://website.com/orders.php?page=edit&order_id=1

Here is what I have to create my dropdown menu:

$getOrder   = mysqli_query($db, "SELECT * FROM orders WHERE order_id = ".$_GET['order_id']);
$getCompany = mysqli_query($db, "SELECT * FROM companies ORDER BY company_name ASC");

....

<select class="form-control" name="company_id" id="company_id">
<?php
    if($getCompany) {
        while($company = mysqli_fetch_assoc($getCompany)) { ?>
            <option value="<?php echo $company['company_id']; ?>">
                <?php echo $company['company_name']; ?>
            </option>
        <?php }
    } 
?>

How can I get selected on the option that's been previously selected?

Ben

If $getOrder has the order details (including company_id), and $getCompany has the company details (including company_id), then you can compare the two.

If they are equal, echo out selected as an attribute in the option, like so:

<select class="form-control" name="company_id" id="company_id">
<?php
    if($getCompany) {

        //Get company ID from Order
        $orderID = $getOrder["company_id"];

        while($company = mysqli_fetch_assoc($getCompany)) { ?>
            <option value="<?php echo $company['company_id']; ?>"
            <?php 
                //Compare and echo `selected` if they are equal
                if($orderId==$company["company_id"]) echo "selected";
            ?>>
                <?php echo $company['company_name']; ?>
            </option>
        <?php }
    } 
?>

That code can be cleaned up, however:

<select class="form-control" name="company_id" id="company_id">
<?php
    if($getCompany) {
        $orderID = $getOrder["company_id"];
        while($company = mysqli_fetch_assoc($getCompany)) {
            echo "<option value='{$company['company_id']}".($getOrder["company_id"]==$getCompany["company_id"] ? " selected" : null).">{$company['company_name']}</option>";
        }
    } 
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I choose an option from a select dropdown using Selenium 2/Web Driver javascript API?

From Dev

How can i get text option from AJAX generated select dropdown using jQuery?

From Dev

How can I pre-select an option in a select menu based on a value stored in the database?

From Dev

How can I customize a select dropdown option

From Dev

How can I pre-select a value from a dropdown list with meteor and autoform?

From Dev

How to select and option from a select dropdown using JS

From Dev

How can I disable to select the particular option from Angular JS dropdown?

From Dev

how can i get alert of data submitted successfully from using servlet,js,html,mysql remote server?

From Dev

How to select an option from a dynamic dropdown using Selenium?

From Java

How to select an option from dropdown select

From Dev

How can I fill up select option menu from database with more fields?

From Dev

Update multiple data from <select> <option> in database using laravel 5.1

From Dev

How to insert data to database from multiple select dropdown in codeigniter?

From Dev

How can I read and bind data from this JSON to a Select dropdown in Angular 2+

From Dev

How can I disabled select option(s) base on the JSON data got from AJAX?

From Dev

(Vue.js) How can I populate the select tag with option tag from array of number values in data?

From Dev

How can I disabled select option(s) base on the JSON data got from AJAX?

From Dev

How can i arrange data array from API request into HTML SELECT OPTION LIST?

From Dev

How can I create a simple <select> in AngularJS with an option pre-selected?

From Dev

AngularJS How do I pre select the value of a dropdown when the value is retrieved from a webservice?

From Dev

pre-select value in dropdown list from mysql data

From Dev

How can I select a dropdown item using javascript

From Dev

How can I select users who have not submitted records using php and mysql?

From Dev

How to split data loaded to a dropdown dynamically from database using jquery?

From Dev

How can I select random value that previously selected value are excluded from the selection

From Dev

How can I select random value that previously selected value are excluded from the selection

From Dev

validation for data (select option) from database

From Dev

How can I set a select option value without using jQuery?

From Dev

How can I add a <select> option using jquery?

Related Related

  1. 1

    How can I choose an option from a select dropdown using Selenium 2/Web Driver javascript API?

  2. 2

    How can i get text option from AJAX generated select dropdown using jQuery?

  3. 3

    How can I pre-select an option in a select menu based on a value stored in the database?

  4. 4

    How can I customize a select dropdown option

  5. 5

    How can I pre-select a value from a dropdown list with meteor and autoform?

  6. 6

    How to select and option from a select dropdown using JS

  7. 7

    How can I disable to select the particular option from Angular JS dropdown?

  8. 8

    how can i get alert of data submitted successfully from using servlet,js,html,mysql remote server?

  9. 9

    How to select an option from a dynamic dropdown using Selenium?

  10. 10

    How to select an option from dropdown select

  11. 11

    How can I fill up select option menu from database with more fields?

  12. 12

    Update multiple data from <select> <option> in database using laravel 5.1

  13. 13

    How to insert data to database from multiple select dropdown in codeigniter?

  14. 14

    How can I read and bind data from this JSON to a Select dropdown in Angular 2+

  15. 15

    How can I disabled select option(s) base on the JSON data got from AJAX?

  16. 16

    (Vue.js) How can I populate the select tag with option tag from array of number values in data?

  17. 17

    How can I disabled select option(s) base on the JSON data got from AJAX?

  18. 18

    How can i arrange data array from API request into HTML SELECT OPTION LIST?

  19. 19

    How can I create a simple <select> in AngularJS with an option pre-selected?

  20. 20

    AngularJS How do I pre select the value of a dropdown when the value is retrieved from a webservice?

  21. 21

    pre-select value in dropdown list from mysql data

  22. 22

    How can I select a dropdown item using javascript

  23. 23

    How can I select users who have not submitted records using php and mysql?

  24. 24

    How to split data loaded to a dropdown dynamically from database using jquery?

  25. 25

    How can I select random value that previously selected value are excluded from the selection

  26. 26

    How can I select random value that previously selected value are excluded from the selection

  27. 27

    validation for data (select option) from database

  28. 28

    How can I set a select option value without using jQuery?

  29. 29

    How can I add a <select> option using jquery?

HotTag

Archive