• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Dynamically Add Select Options

ShiZZeK

Master
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
180
Likes
56
Bits
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.
2019-07-23_23_31_50-localhost_127.0.0.1_db_subject_phpmyadmin_4.8.3.png

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.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_subject"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Dynamically Add Select Options</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>

  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <form method="POST" action="add.php">
  20. <div class="form-inline">
  21. <label>Enter here</label>
  22. <br />
  23. <input type="text" name="subject" size="16" class="form-control" required="required"/>
  24. <button class="btn btn-success" name="add"><span class="glyphicon glyphicon-plus"></span> Add</button>
  25. </div>
  26. </form>
  27. <br />
  28. <form method="POST" action="submit.php">
  29. <div class="form-inline">
  30. <label>Select a subject</label>
  31. <select class="form-control" name="subject">
  32. <?php
  33. require

    'conn.php'

    ;
  34. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `subject` ORDER BY `name` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  35. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  36. ?>
  37. <option value="<?php

    echo

    $fetch

    [

    'sub_id'

    ]

    ?>

    "><?php

    echo

    $fetch

    [

    'name'

    ]

    ?>

    </option>
  38. <?php
  39. }
  40. ?>
  41. </select>
  42. </div>
  43. <br />
  44. <center><button class="btn btn-primary" name="submit">Submit</button></center>
  45. </form>
  46. </div>
  47. </div>
  48. </body>
  49. </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.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'add'

    ]

    )

    )

    {
  4. $subject

    =

    $_POST

    [

    'subject'

    ]

    ;

  5. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `subject` VALUES('', '$subject

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  6. header

    (

    'location: index.php'

    )

    ;
  7. }
  8. ?>

submit.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'submit'

    ]

    )

    )

    {
  4. $subject

    =

    $_POST

    [

    'subject'

    ]

    ;

  5. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `subject` WHERE `sub_id`='$subject

    '"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  6. $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    ;
  7. echo

    "<script>alert('You have selected "

    .

    $fetch

    [

    'name'

    ]

    .

    "')</script>"

    ;
  8. echo

    "<script>window.location='index.php'</script>"

    ;
  9. }
  10. ?>

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.
 

452,292

323,692

323,701

Top