使用表单提交通过ajax jQuery帖子保存数据

时间:2022-04-06 08:42:02

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...

我有一个表单,我想通过ajax jQuery帖子保存数据。如果保存按钮是type =“submit”,它不会做任何这个只是空警报...我想保存数据并保持在同一个表单上刷新字段...

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>

3 个解决方案

#1


13  

Do it this way

这样做吧

HTML

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

你的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

所以在insert.php页面中

<?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

希望这能给出一个想法

#2


7  

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

你不需要click事件来删除$(“#saveNewContact”)。click(function(){并使用.submit()jQuery方法。

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

还可以使用.serialize,它允许您使用值序列化表单字段并创建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);
        }
    });
});

#3


2  

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

绑定表单的提交而不是$ .post必要的成功,功能就足够了:

$(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();
    });
   });
 });

#1


13  

Do it this way

这样做吧

HTML

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

你的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

所以在insert.php页面中

<?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

希望这能给出一个想法

#2


7  

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

你不需要click事件来删除$(“#saveNewContact”)。click(function(){并使用.submit()jQuery方法。

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

还可以使用.serialize,它允许您使用值序列化表单字段并创建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);
        }
    });
});

#3


2  

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

绑定表单的提交而不是$ .post必要的成功,功能就足够了:

$(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();
    });
   });
 });