how to save data using ajax jquery & php?

i am trying to insert data into table using ajax but i can’t please tell me what is wrong in the code here is my code for my main.php file….

<script src='jq.js'></script>
<script>
$(document).ready(function(){
$("#myfrm").submit(function(e){
    e.preventDefault();
    var mydata = $("#myfrm").serialize();
    $.ajax({
        type:"POST",
        url:"save.php",
        dataType:"text",
        data:mydata,
        sucess:function(response)
        {
            alert('saved!');
            $("#respond").append(response);
        },
        error:function(xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }
    });
});
});
</script>
<form id="myfrm" method="POST" action="">
<div id="respond"></div>
<input type="text" id='myname' name='myname' required/>
<input type="submit" id='mybtn' Value="Submit" />
</form>

and here is my code for save.php

<?php
    include 'data.php';
    $myname=$_POST['myname'];
    $qry="insert into myfile (myname) values('$myname')";
    $insert_it=mysqli_query($con,$qry)or die(mysqli_error($con));
    echo "HI!".$myname;
?>

please tell me where is the error in my code i cant identify it……..
thank you in advance..


Source: php

Leave a Reply