ster ster - 1 month ago 5
HTML Question

insert value in mysqli from h1 html

I have the file index.php:



<div id="content">
<h4> title </h4>
<h3> subtitle </h3>
<h4> quiz </h4>
<form action="insert_value_mysqli.php" method="POST">
<input type="submit" name="insert" value="Submit">
</form>
</div>





How can i assign attributes or how can i insert in the table below these texts: title, subtitle and quiz? Or what procedures to use to introduce values in the table?



table:
+----+----------+
| ID | VALUES |
+----+----------+
| 1 | title |
| 2 | subtitle |
| 3 | quiz |
+----+----------+





Thank you!

Answer

Try with jquery and php :

Jquery :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">   </script>   

<script>
$(document).ready(function(){
    $('#myForm').on('submit', function (e) {
        e.preventDefault();
        var title = $("#content h1").html();
        var subtitle = $("#content h2").html();
        var quiz = $("#content h3").html();
        console.log(title);
        $.ajax({
            url: "insert_value_mysqli.php", // Url to which the request is send
            type: "POST", // Type of request to be send, called as method
            data: {title:title, subtitle:subtitle, quiz:quiz}, // Data sent to server, a set of key/value pairs representing form fields and values
            dataType: 'json'
        }).done(function (data){
            console.log(data);
        }).fail(function (data) {
            console.log('failed');
        }); 
    });
});
</script>

HTML :

<div id="content">
    <h1>title here</h1>
    <h2>subtitle</h2>
    <h3>quiz</h3>
    <form id="myForm" method="POST">
        <input type="submit" name="insert" value="Submit">
    </form>
</div>

insert_value_mysqli.php code bellow :

<?php
  $title = $_POST['title'];
  $subtitle = $_POST['subtitle'];
  $quiz = $_POST['quiz'];
  //now you can use insert query
?>