PHP - Live Update on Select

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