3

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('');
    });
}
2
  • 2
    Use $("#noteform" ).submit(function( event ) { instead of $("#sub").click(function(){ Commented Nov 30, 2015 at 10:11
  • 1
    Maybe this is helpful : stackoverflow.com/questions/13064179/… Commented Nov 30, 2015 at 10:15

1 Answer 1

5

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>
Sign up to request clarification or add additional context in comments.

6 Comments

i did try the above code but i get redirected to note_sql.php
SO remove note_sql.php from form action
okay it works now but i do not need it in a alert box it needs to come in a span area <span id="result_note"><span> how would i dothis
use $('span#result_note').html(data); in place of alert(data);
Thanks for your hel @rajveer u have been great
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.