Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <!-- link to jquery lib -->
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- </head>
- <body>
- <form action="" method="POST">
- <h4 id="result"></h4>
- <div class="container">
- <h2 align="center">Table App Feature</h2>
- <label> Name </label>
- <input type='text' name='name' id='name' value=''/>
- <table id="appFeature" class="table table-hover" align="center" style="width:500px;margin:auto;">
- <thead>
- <tr>
- <th>Firstname</th>
- <th>Please check to enable the features</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>Smarthome</td>
- <td>
- <!-- checkbox for smarthome value -->
- <input type="checkbox" class="get_value" id='smarthome'/>
- </td>
- </tr>
- <tr>
- <td>Intercom</td>
- <td>
- <input type="checkbox" class="get_value" id='intercom'/>
- </td>
- </tr>
- </tbody>
- </table><br />
- <div align="center">
- <label>check if you want to update, unckeck if you want to insert</label>
- <input type="checkbox" class="get_value" id='update'/>
- <br>
- <!-- button name -->
- <button type="button" name="submit" id="submit">Update or Insert</button>
- </div>
- </div>
- </form>
- </body>
- <script type="text/javascript">
- $(document).ready(function(){
- $('#submit').click(function(){
- // get the value is checked from the form
- $.ajax({
- url: "insert.php",
- method: "POST",
- data:{intercom: $('#intercom').is(':checked'),smarthome: $('#smarthome').is(':checked'), name: $('#name').val(), update: $('#update').is(':checked')},
- success:function(data){
- $('#result').html(data);
- }
- });
- });
- });
- </script>
- </html>
- <?php
- $servername = "localhost";
- $username = "YOURDBUSER";
- $password = "YOURDBPASSWORD";
- $dbname = "databaseappfeature"; // db name
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql ;
- if($_POST['update']){
- $sql = "UPDATE appfeature SET smarthome=".$_POST['smarthome'].", intercom=".$_POST['intercom']." WHERE name='".$_POST['name']."'";
- }else{
- $sql = "INSERT INTO appfeature (name, smarthome, intercom) VALUES ('".$_POST['name']."',".$_POST['smarthome'].",".$_POST['intercom'].")";
- }
- if ($conn->query($sql) === TRUE && !$_POST['update']) {
- echo "New record created successfully";
- }else if($conn->query($sql) === TRUE && $_POST['update']){
- echo "record updated";
- }else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- ?>
Add Comment
Please, Sign In to add comment