Guest User

Untitled

a guest
Jun 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. <html>
  2. <head>
  3. <!-- link to jquery lib -->
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  5. </head>
  6. <body>
  7. <form action="" method="POST">
  8.  
  9. <h4 id="result"></h4>
  10.  
  11. <div class="container">
  12. <h2 align="center">Table App Feature</h2>
  13. <label> Name </label>
  14. <input type='text' name='name' id='name' value=''/>
  15. <table id="appFeature" class="table table-hover" align="center" style="width:500px;margin:auto;">
  16. <thead>
  17. <tr>
  18. <th>Firstname</th>
  19. <th>Please check to enable the features</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr>
  24. <td>Smarthome</td>
  25. <td>
  26. <!-- checkbox for smarthome value -->
  27. <input type="checkbox" class="get_value" id='smarthome'/>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td>Intercom</td>
  32. <td>
  33. <input type="checkbox" class="get_value" id='intercom'/>
  34. </td>
  35. </tr>
  36. </tbody>
  37. </table><br />
  38. <div align="center">
  39. <label>check if you want to update, unckeck if you want to insert</label>
  40. <input type="checkbox" class="get_value" id='update'/>
  41. <br>
  42. <!-- button name -->
  43. <button type="button" name="submit" id="submit">Update or Insert</button>
  44. </div>
  45. </div>
  46. </form>
  47. </body>
  48. <script type="text/javascript">
  49. $(document).ready(function(){
  50. $('#submit').click(function(){
  51. // get the value is checked from the form
  52. $.ajax({
  53. url: "insert.php",
  54. method: "POST",
  55. data:{intercom: $('#intercom').is(':checked'),smarthome: $('#smarthome').is(':checked'), name: $('#name').val(), update: $('#update').is(':checked')},
  56. success:function(data){
  57. $('#result').html(data);
  58. }
  59. });
  60. });
  61. });
  62. </script>
  63. </html>
  64.  
  65. <?php
  66. $servername = "localhost";
  67. $username = "YOURDBUSER";
  68. $password = "YOURDBPASSWORD";
  69. $dbname = "databaseappfeature"; // db name
  70.  
  71. // Create connection
  72. $conn = new mysqli($servername, $username, $password, $dbname);
  73. // Check connection
  74. if ($conn->connect_error) {
  75. die("Connection failed: " . $conn->connect_error);
  76. }
  77. $sql ;
  78.  
  79. if($_POST['update']){
  80. $sql = "UPDATE appfeature SET smarthome=".$_POST['smarthome'].", intercom=".$_POST['intercom']." WHERE name='".$_POST['name']."'";
  81. }else{
  82. $sql = "INSERT INTO appfeature (name, smarthome, intercom) VALUES ('".$_POST['name']."',".$_POST['smarthome'].",".$_POST['intercom'].")";
  83. }
  84.  
  85. if ($conn->query($sql) === TRUE && !$_POST['update']) {
  86. echo "New record created successfully";
  87. }else if($conn->query($sql) === TRUE && $_POST['update']){
  88. echo "record updated";
  89. }else {
  90. echo "Error: " . $sql . "<br>" . $conn->error;
  91. }
  92.  
  93. $conn->close();
  94. ?>
Add Comment
Please, Sign In to add comment