Dynamically Add Select Options in PHP

June 20, 2020
PHP
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.

<!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!

https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

    , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

    Leave a Reply

    Your email address will not be published. Required fields are marked *