Dynamically Add Select Options in PHP

By CampCodes Administrator

Updated on:

dynamically add select options in php

In this tutorial, we will create a Dynamically Add Select Options using PHP. This code will allow the user to add value to the HTML select tag. The system uses the MySQLi INSERT query to add value in the choose card without doing it manually. This a user-friendly program. Feel free to modify and use it for your system.

We will be using PHP as a scripting language and interpreter that is mainly used on any web server, including xamp, wamp, etc. It is being applied to any popular websites because of the modern approach as its today.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_subject, after that, click Import then locates the database file inside the folder of the application then click ok.

dynamically add select options in php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

<?php
    $conn = mysqli_connect("localhost", "root", "", "db_subject");
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

READ ALSO:   How to Create a Login Attempt Validation using PHP

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Dynamically Add Select Options</h3>
        <hr style="border-top:1px dotted #ccc;"/>
 
        <div class="col-md-3"></div>
        <div class="col-md-6">
            <form method="POST" action="add.php">
                <div class="form-inline">
                    <label>Enter here</label>
                    <br />
                    <input type="text" name="subject" size="16" class="form-control" required="required"/>
                    <button class="btn btn-success" name="add"><span class="glyphicon glyphicon-plus"></span> Add</button>
                </div>
            </form>
            <br />
            <form method="POST" action="submit.php">
                <div class="form-inline">
                    <label>Select a subject</label>
                    <select class="form-control" name="subject">
                        <?php
                            require'conn.php';
                            $query = mysqli_query($conn, "SELECT * FROM `subject` ORDER BY `name` ASC") or die(mysqli_error());
                            while($fetch = mysqli_fetch_array($query)){
                        ?>
                        <option value="<?php echo $fetch['sub_id']?>"><?php echo $fetch['name']?></option>
                        <?php
                            }
                        ?>
                    </select>
                </div>
                <br />
                <center><button class="btn btn-primary" name="submit">Submit</button></center>
            </form>
        </div>
    </div>
</body>	
</html>

Creating the Main Function

This code contains the main function of the application. This code will add a list of select option when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
add.php.

<?php
    require_once'conn.php';
 
    if(ISSET($_POST['add'])){
        $subject = $_POST['subject'];
 
        mysqli_query($conn, "INSERT INTO `subject` VALUES('', '$subject')") or die(mysqli_error());
        header('location: index.php');
    }
?>

submit.php

<?php
    require_once'conn.php';
 
    if(ISSET($_POST['submit'])){
        $subject = $_POST['subject'];
 
        $query=mysqli_query($conn, "SELECT * FROM `subject` WHERE `sub_id`='$subject'") or die(mysqli_error());
        $fetch=mysqli_fetch_array($query);
        echo"<script>alert('You have selected ".$fetch['name']."')</script>";
        echo"<script>window.location='index.php'</script>";
    }
?>

There you have it we successfully created Dynamically Add Select Options using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

add field dynamically to form in jquery and php add remove select box fields dynamically using jquery ajax in php ajax and php ajax crud on dynamically add remove input fields in php ajax dropdown example in php capture array values from dynamic input fields using php cascading dropdown php mysql change drop down options based on another dropdown javascript change select options based on another select jquery change select options based on another select jsp change select options based on another select php combobox in php with database country state city drop down list in php mysql country state city drop down list using ajax in php country state city drop down list using ajax in php demo country state city dropdown using ajax in php create dynamic input fields in php dependent drop down list php mysql dependent dropdown in php using mysql data drop down list in php form drop down list in php from a database drop down list php mysql drop down list php mysql example drop down list php mysql tutorial dynamic dependent select box using jquery dynamic drop down list in php dynamic drop down list in php from mysql dynamic drop down list in php using ajax dynamic drop down list php dynamic drop down list php mysqli tutorial dynamic drop down menu in php mysql dynamic drop down menu php dynamic dropdown in php dynamic select box in php dynamic select query in php dynamically add select options in php echo option value php echo selected option value php fetching data from mysql database to html dropdown list first drop down menu to auto change the options of a second dropdown in php how do i change selection options based on another select option selected? how do i change the options in a dropdown list dynamically? how do i create a dynamic select list in html? how fetch data from database in php and display in select option? how to create drop down list in php with mysql how to edit drop down list in php how to fetch data from database in php and display in select option how to get select option value from database in php how to get select option value from mysql database using php how to get selected option value in php how to get selected text of dropdown in php how to get values in dropdown from database in php how to insert select option value in database using php how to pass dropdown selected value in php how to populate dropdownlist in php from database how to set selected value in dropdown using php how to show data in dropdownlist from database in php how to update dropdown value in php how to update select option in php html drop down list based on previous selection html dropdown select value depends on another drop if condition for select option in php javascript dropdown onchange another dropdown javascript dynamic drop down list from database jquery add option to select jquery ajax dropdown onchange example in php multiple dependent drop down list in php multiple drop down list in php using ajax onchange event in php for dropdown example php change dropdown based on another dropdown php code for drop down list from database php code for dropdownlist from database php drop down list selected value from database php drop down menu php drop down menu from database php dropdown from mysql php dropdown selected value php dropdown selected value from database php dropdown selected value from database tutorial php dynamic drop down list onchange php dynamic form fields mysql php dynamic input fields php dynamic select option value php dynamic select/option from mysql table php form select php get selected option value onchange php mysql drop down menu to select data to display php mysql select option dropdown php onchange php option selected value from database php populate dropdown based on another dropdown php post select option id php retrieve data from mysql using drop down menu php select box php select option php select option selected php select option selected from database php select option value php selected php tutorials populate a dropdown and display a related record in php populate second dropdown based on first php populate second dropdown based on first php mysql populate select from database select dropdown value from database in php select option in php mysql select option php select option php mysql select option validation in php selected value of dropdown in php from database three dependent dropdown in php two drop down list in php update drop down list in php mysql use javascript to change a second select list based on the first select list option use jquery to change a second select list based on the first select list option

Leave a Comment