Monday, 6 January 2014

Without webpage refreshing insert data into MySQL with jQuery

--------------------- my_form.php ------------------

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
$(function(){
      $("#myForm").submit(function() {
            $.post("insert_data.php", $(this).serialize(),function(data){
       // data inserted "insert_data.php" 
            } )
            return false
      })
})


</script>
</head>



    <body>
        <form id="myForm" name="myForm" method="post" action="" >
<input type="text" id="name" name="name" />
<input type="text" id="message" name="message" />
<input id="submitbutton" type="submit" name="senddata" value="Submit Data" method="post">
        </form>

<div id="info" > </div>

</body>
</html>







--------------------- insert_data.php ---------------------

<?php


mysql_connect("localhost", "root", "") or die(mysql_error());

echo "Connected to MySQL<br />";


$name=$_REQUEST["name"];
$message=$_REQUEST["message"];

        mysql_query("INSERT INTO tbl_mydata (name, message) VALUES('$name', '$message')");

         echo "Successfully Inserted";
        


?>

No comments:

Post a Comment

Alternative content