1

I want to insert data into table using ajax so data will insert without reload of page.

This code insert data into table very well but code also reload the page.

But I want insert without reloading of page.

How can i do this ?

<?php
include('connection.php');
if(isset($_POST['cmt'])){
    $comment = addslashes($_POST['cmt']);
    $alertid = $_POST['alert_id'];
    mysql_query("INSERT INTO `comments` (`id`, `alert_id`, `comment`, `username`) VALUES (NULL, '".$alertid."', '".$comment."', 'tomas')");
}
?>


<script>
  function submitform(){
    var comment = $("#comment").val();
    var alertid = $("#alertid").val();
    $.ajax({
        type: "POST",
        //url: "ana.php",
        data:{cmt:comment,alert_id:alertid}
    }).done(function( result ) {
        $("#msg").html( result );
    });

  }
</script>

<form method = "POST" onsubmit = "submitform()">
   <textarea onFocus = "myFunction(1)" onBlur = "myFunction(0)" style="margin: 0px 0px 8.99305534362793px; width: 570px; height: 50px;" rows = "6" cols = "40" id = "comment"></textarea><br />
   <input type = "text" placeholder="Enter Maximium 100 Words" id = "alertid" value = "10">
   <input  type = "submit" name = "submit" value = "Comment">
</form>
4
  • Danger: You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks (addslashes is insufficient) that a modern API would make it easier to defend yourself from. Commented Mar 19, 2014 at 11:03
  • api.jquery.com/event.preventdefault Commented Mar 19, 2014 at 11:04
  • @AbhikChakraborty — Won't do any good since it isn't a jQuery event handler. Commented Mar 19, 2014 at 11:04
  • @Quentin Yeah he is using the onsubmit so yes return false should do the trick. Commented Mar 19, 2014 at 11:05

3 Answers 3

2

try this add this to form onsubmit = "return submitform();"

 function submitform(){
    var comment = $("#comment").val();
    var alertid = $("#alertid").val();
    $.ajax({
        type: "POST",
        //url: "ana.php",
        data:{cmt:comment,alert_id:alertid}
    }).done(function( result ) {
        $("#msg").html( result );
    });
    return false;
  }
Sign up to request clarification or add additional context in comments.

Comments

1

return false from your event handler function.

onsubmit="submitform(); return false;">

Consider moving to modern methods of event binding.

Comments

0

You have to create a php file that insert into your table the posted data and call it with ajax like that :

$.ajax({
url: "/file.php",
type: "POST",
cache: false,
dataType: "json",
data: postValue,
success: function(results) {
    bootbox.alert(results.message, function() {
        bootbox.setIcons(null);
        window.location.reload();
    });
},
error: function(results) {
    bootbox.alert(results.message, function() {
        bootbox.setIcons(null);
    });
}

});

Comments

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.