ShiZZeK
Master
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
In this tutorial we will create a Dynamically Add Select Options using PHP. This code will allow the user to add a value for the HTML select tag. The code use MySQLi INSERT query to add a value in the select tag without doing it manually. This a user-friendly program feel free to modify and use it to your system.
We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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 locate the database file inside the folder of the application then click ok.
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.
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.
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.
submit.php
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!
Download
We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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 locate the database file inside the folder of the application then click ok.

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!
Download
You must upgrade your account or reply in the thread to view hidden text.