article

Sunday, February 21, 2021

Editable Select Box using PHP Mysql database and jQuery Ajax

Editable Select Box using PHP Mysql database and jQuery Ajax

jQuery Editable Select is a jQuery plugin that transforms a select into an input field where single elements are shown in real-time according to the entered characters. It scales down to a real select list when javascript is not available.

https://github.com/indrimuska/jquery-editable-select

See demos here: http://indrimuska.github.io/jquery-editable-select/

CREATE TABLE `country` (
  `id` int(6) NOT NULL,
  `country` varchar(250) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `tbl_user` (
  `userid` int(11) NOT NULL,
  `fullname` varchar(50) NOT NULL,
  `country` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//index.php
<!DOCTYPE html>
<html>
<head>
<title>Editable Select Box using PHP Mysql database and jQuery Ajax</title>
 
</head> 
<body> 
<?php
include('dbcon.php');
$result = $conn->query("SELECT * FROM country ORDER BY country ASC");
?>
<div class="container"
   <br />
   <h2 align="center">Editable Select Box using PHP Mysql database and jQuery Ajax</h2><br />
   <div class="row">
    <div class="col-md-3"></div>
    <div class="col-md-6">
     <form method="post" id="sample_form">
      <div class="form-group">
       <label>Enter Name</label>
       <input type="text" name="name" id="name" class="form-control">
      </div>
      <div class="form-group">
       <label>Select Country</label>
       <select name="country" id="country" class="form-control">
       <?php
       foreach($result as $row)
       {
        echo '<option value="'.$row["country"].'">'.$row["country"].'</option>';
       }
       ?>
       </select>
      </div>
      <div class="form-group">
       <input type="submit" name="Save" id="save" class="btn btn-success" value="Save" />
      </div>
     </form>
    </div>
   </div>
  </div>
  <script> 
$(document).ready(function(){
    $('#country').editableSelect();
  
    $('#sample_form').on('submit', function(event){
        event.preventDefault();
        if($('#name').val() == '') {
           alert("Enter Name");
           return false;
        }else if($('#country').val() == ''){
           alert("Select Country");
           return false;
        }else{
           $.ajax({
                url:"insert.php",
                method:"POST",
                data:$(this).serialize(),
                success:function(data)
                {
                 alert(data);
                 $('#sample_form')[0].reset();
                }
           });
        }
    });
}); 
</script>
</body> 
</html>
insert.php
1
2
3
4
5
6
7
8
9
//insert.php
<?php
include('dbcon.php');
$name = $_POST["name"];
$country = $_POST["country"];
$sql = "INSERT INTO tbl_user(fullname, country) VALUES ('$name', '$country')";
$conn->query($sql);
echo "Record Successfully added";
?>
insert.php
1
2
3
4
5
6
7
//dbcon.php
<?php
$conn = new mysqli('localhost','root','','testingdb');
if ($conn->connect_error) {
    die('Error : ('. $conn->connect_errno .') '. $conn->connect_error);
}
?>

Related Post