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

Filling the Twitter Bootstrap-Select Dynamically From MySQL Data Using PHP

Dogooo

Anime Convention Pro
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
45
Likes
84
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this lesson is a continuation of our previous tutorial called Creating and Populating table with MySQL Data using Twitter Bootstrap framework.
At this time, we’re going to use the same framework. But we will focus on how to populate the Twitter Bootstrap-Select dynamically from MySQL Data using PHP and display the corresponding value in a label form. And it looks like as shown below.

bootstrap-select_0_0.png


Then let’s create a new file and name it as “selectbox.php”. And the following code:
  1. <!

    DOCTYPE html>

  2. <

    html lang=

    "en"

    >
  3. <

    head>
  4. <

    meta charset=

    "utf-8"

    >
  5. <

    meta content=

    "width=device-width, initial-scale=1.0"

    name=

    "viewport"

    >
  6. <

    meta content=

    ""

    name=

    "description"

    >
  7. <

    meta content=

    ""

    name=

    "author"

    >
  8. <

    link

    href=

    ""

    rel=

    "shortcut icon"

    >

  9. <

    title>

    List

    of Student</

    title><!--

    Bootstrap core CSS -->
  10. <

    link

    href=

    "css/bootstrap.min.css"

    rel=

    "stylesheet"

    >
  11. </

    head>

  12. <

    body>
  13. <

    div class

    =

    "container"

    >
  14. <

    div class

    =

    "well"

    >
  15. <

    a class

    =

    "brand"

    href=

    "#"

    >
  16. <

    h2>

    Bootstrap-

    select</

    h2></

    a>
  17. </

    div>

  18. <

    div class

    =

    "well"

    >
  19. <!--

    This is the area where we are going to put our HTML form and PHP codes-->

  20. <

    p>&

    nbsp;</

    p>
  21. </

    div>
  22. </

    div><!--

    /

    container -->
  23. </

    body>
  24. </

    html>

The output of this code looks like as shown below.
bootstrap-select-o1.png


This time, we're going to edit our "selectbox.php" file. Then insert the following code inside the second "....

".

  1. <form action="selectbox.php" class="form-inline pull-left" method="post">
  2. <div class="form-group col-lg-4">
  3. <?php
  4. //set up mysql connection
  5. mysql_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  6. //select database
  7. mysql_select_db

    (

    "studentdb"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  8. // Retrieve all the data from the "tblstudent" table
  9. $result

    =

    mysql_query

    (

    "SELECT * FROM tblstudent"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  10. // store the record of the "tblstudent" table into $row
  11. ?>
  12. <select class="form-control" name="fname">
  13. <option>
  14. Select a Name
  15. </option>
  16. <?php
  17. //it fills the selectbox from mysql data
  18. while

    (

    $row

    =

    mysql_fetch_array

    (

    $result

    )

    )

    {
  19. //this the value that corrspond to the selected item of the user
  20. echo

    ' <option value="'

    .

    $row

    [

    'email'

    ]

    .

    '"> '

    ;
  21. //this line will be visible to the user
  22. echo

    $row

    [

    'firstname'

    ]

    .

    ' </option> '

    ;
  23. }
  24. ?>
  25. </select>
  26. </div><button class="btn btn-primary" name="submit" type=
  27. "submit">Go</button>
  28. <!--it check here fi the submit button is set then -->
  29. <?php
  30. if

    (

    isset

    (

    $_POST

    [

    'submit'

    ]

    )

    )

    {
  31. $fname

    =

    $_POST

    [

    'fname'

    ]

    ;
  32. //it display the value based on user selected item
  33. echo

    '<h3>'

    .

    "Your email address is: "

    .

    $fname

    .

    '</h3>'

    ;

  34. }

    else

    {
  35. //if submit button is not set then,
  36. //it notify the user that theres no data has been selected
  37. echo

    '<h3>'

    .

    "No data is Selected!"

    .

    '</h3>'

    ;
  38. }

  39. ?>
  40. </form>

In testing the application, the output should looks like as shown below.

bootstrap-select3.png

Make sure the file structure should look like as shown below:

bootstraptable
css
fonts
list.php
selectbox.php


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top