Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting data to sql database without reloading page

Tags:

jquery

php

the problem is that i am unable to insert data in my database without reloading i have tested it without the note_sql.js and my note_sql.php works fine but there seems to be a problem in my js file if some body could point me in the right direction it would be wonderful

Index.php

    <form id="noteform" action="note_sql.php" method="POST">
    <input type="text" name="note"></input>
    <input id="sub" type="submit" value="Save Note" />
    </form>
    <span id="result_note"><span>
   <script type ="text/javascript" src="jquery/note_sql.js"></script>

note_sql.js

$("#sub").click(function(){

var data = $("#noteform: input").serializeArray();
$.post($("#noteform").attr("action"),data,function(info){$("result_note").html(info); });
    clearInput();
}); 

$("#noteform").submit(function(){
    return false;
});

function clearInput(){
    $("#noteform :input").each(function(){
        $(this).val('');
    });
}
like image 722
kunz Avatar asked Jul 25 '26 13:07

kunz


1 Answers

Use Jquery Ajax the working code is given below : please add ID to Note Form Element :

<pre>
      <script>
            $(document).ready(function () {
                $("#sub").click(function () {

                    var name = $("#note").val();
                    var message = $("#message").val();

                    $.ajax({
                        type: "post",
                        url: "note_sql.php",
                        data: "name=" + name,
                        success: function (data) {
                            alert(data);
                        }

                    });

                });
            });
        </script>
</pre>
like image 84
Rajveer gangwar Avatar answered Jul 27 '26 04:07

Rajveer gangwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!