PHP - Live Update on Select

Submitted by: 
Language: 
Visitors have accessed this post 839 times.

In this tutorial we will create a Live Update on Select using PHP. This code will allow the user to update the value of the HTML select tag. The code use MySQLi UPDATE query to change the value of the current 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_select, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

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_select");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Live Update on Select</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="add.php">
  19. <div class="form-inline">
  20. <label>Country</label>
  21. <input type="text" name="country" class="form-control" required="rqeuired"/>
  22. <button class="btn btn-success" name="add"><span class="glyphicon glyphicon-plus"></span> Add</button>
  23. </div>
  24. </form>
  25. <br /><br />
  26. <div class="col-md-8">
  27. <form method="POST" action="update.php">
  28. <div class="col-md-6">
  29. <div class="form-inline">
  30. <?php
  31. require'conn.php';
  32. $query = mysqli_query($conn, "SELECT * FROM `country` ORDER BY `name` ASC") or die(mysqli_error());
  33. while($fetch = mysqli_fetch_array($query)){
  34. echo "<input type='hidden' name='id[]' class='form-control' value='".$fetch['country_id']."'/><input type='text' name='country[]' class='form-control' value='".$fetch['name']."' required='required'/>";
  35. }
  36. ?>
  37. </div>
  38. </div>
  39. <div class="col-md-6">
  40. <button class="btn btn-warning" name="update"><span class="glyphicon glyphicon-edit"></span> Update</buttpn>
  41. </div>
  42. </form>
  43. </div>
  44. <div class="col-md-4">
  45. <div class="form-inline">
  46. <label>Country</label>
  47. <select class="form-control" name="country">
  48. <?php
  49. require'conn.php';
  50. $query = mysqli_query($conn, "SELECT * FROM `country` ORDER BY `name` ASC") or die(mysqli_error());
  51. while($fetch = mysqli_fetch_array($query)){
  52. ?>
  53. <option value="<?php echo $fetch['country_id']?>"><?php echo $fetch['name']?></option>
  54. <?php
  55. }
  56. ?>
  57. </select>
  58. </div>
  59. </div>
  60. </div>
  61. </body>
  62. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do this just copy and write these block of codes below inside the text editor, then save it as add.php.

  1. <?php
  2. require_once'conn.php';
  3.  
  4. if(ISSET($_POST['add'])){
  5. $country = $_POST['country'];
  6.  
  7. mysqli_query($conn, "INSERT INTO `country` VALUES('', '$country')") or die(mysqli_error());
  8. header('location: index.php');
  9. }
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will update the 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 update.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['update'])){
  5. foreach($_POST['id'] as $key => $value){
  6. $country = $_POST['country'][$key];
  7. mysqli_query($conn, "UPDATE `country` SET `name` = '$country' WHERE `country_id` = '$value'") or die(mysqli_error());
  8. }
  9.  
  10. header("location:index.php");
  11. }
  12. ?>

There you have it we successfully created Live Update on Select using jQuery. 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 new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.