12

I have a form and I want to save data through ajax jQuery post. If the save button is type="submit", it does not do any this just empty alert... I want to save data and remain on same form with refresh fields...

Here is what I have tried:

$("#saveNewContact").click(function () {
  var name       = $("#name").val(); 
  var last_name  = $("#last_name").val();
  $.post("ajax_files/save_mydeals.php", {name: name, last_name: last_name},
   success: function(msg){ 
     alert(msg);
  });
 });

and here is my form

<form id="myForm" action="#" method="post">
<table id="table_1" class="form_table" align="center" cellpadding="0" cellspacing="0">
<tbody>
  <tr><td>
    <input  name="name"  id="name" value="" tabindex="5" readonly="" type="text">
    <input  name="name"  id="name" value="" tabindex="5" readonly="" type="text"> 
    <button type="submit" id="update" style="display:none;" 
     class="save_button saveHEX" name="Update" 
     value="Update Listing">Save Lead</button></td>
  </tr>
 </table>
 </form>
6
  • why don't you just set the input type as button ? Commented Mar 9, 2013 at 6:44
  • i want to clear form after this,empty text fields Commented Mar 9, 2013 at 6:45
  • you can clear it another way. $('#myForm').find('input').val(''); do it inside your success function. Commented Mar 9, 2013 at 6:46
  • mplungjan:yes i have this $(function() {...}) and button is submit Commented Mar 9, 2013 at 6:47
  • can i use any validate,or beforsubmit etc etc? Commented Mar 9, 2013 at 6:48

3 Answers 3

16

Do it this way

HTML

<form name="frm" method="POST" action="">
 <input type="text" name="name" id="name" value="" />
 <input type="text" name="last_name" id="last_name" value="" />
 <input type="submit" name="Update" id="update" value="Update" />
</form>

Your jQuery

$("#update").click(function(e) {
  e.preventDefault();
  var name = $("#name").val(); 
  var last_name = $("#last_name").val();
  var dataString = 'name='+name+'&last_name='+last_name;
  $.ajax({
    type:'POST',
    data:dataString,
    url:'insert.php',
    success:function(data) {
      alert(data);
    }
  });
});

So in the insert.php page

NOTE: this is a concept example only. This is not a safe way of handling user submitted data.

<?php
  $name = $_POST['name'];
  $last_name = $_POST['last_name'];
  $insert = "insert into TABLE_NAME values('$name','$last_name')";// Do Your Insert Query
  if(mysql_query($insert)) {
   echo "Success";
  } else {
   echo "Cannot Insert";
  }
?>

Hope this gives an Idea

Sign up to request clarification or add additional context in comments.

4 Comments

exactly i tried this ..but issue was when button type was submit,it return empty alert...no data inserted in db...can i change type=submit?
@Methew : Yes You can use type="submit". But with a little modification.I have updated the answer please check
I think it would be nicer to use the dataType attribute of the post (or ajax method -> set it to json and no need for ugly serialization ! api.jquery.com/jQuery.post
I'm pretty sure that this SQL statement is vulnerable to SQL injection.
9

You do not need click event for this remove $("#saveNewContact").click(function () { and use .submit() jQuery method.

Also use .serialize that allow you to serialize form fields with value and create urlencoded dataString.

$("#myForm").on("submit",function (e)
{
    e.preventDefault();
    var formData = $(this).serialize();
    $.ajax(
    {
        type:'post',
        url:'receiver url',
        data:formData,
        beforeSend:function()
        {
            launchpreloader();
        },
        complete:function()
        {
            stopPreloader();
        },
        success:function(result)
        {
             alert(result);
        }
    });
});

Comments

2

Bind the form's submit instead AND no success in the $.post necessary, the function is enough:

$(function() {
  $("#myForm").on("submit",function (e) {
    e.preventDefault(); // stop the normal submission
    var name       = $("#name").val(); 
    var last_name  = $("#last_name").val();
    $.post("ajax_files/save_mydeals.php", {name: name, last_name: last_name},
     function(msg){ 
       alert(msg);
       $("#name").empty(); 
       $("#last_name").empty();
    });
   });
 });

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.