如何在没有页面重新加载的情况下在数据库中插入数据?

时间:2018-04-24 11:00:25

标签: javascript php ajax

我想在没有页面重新加载的情况下插入数据。 这是我用过的html表单。 taskinsert.php是我用过的php文件。

`<form class="form-horizontal" id="form_post" enctype="multipart/form-data">
          <div class="row">

        <div class="form-group col-md-12">
          <label class="col-sm-2" for="uploadedimage">Image :</label>
          <div class="col-sm-7">
            <input type="file" class="btn btn-primary" name="uploadedimage" id="uploadedimage">
          </div> <div class="col-sm-3"></div>
        </div>

        <div class="form-group col-md-12">
          <label class="col-sm-2" for="email">Email :</label>
          <div class="col-sm-7">
            <input type="text" class="" name="email" id="email">
          </div> <div class="col-sm-3"></div>
        </div>

        <div class="form-group col-md-12">
          <label class="col-sm-2" for="phone">Phone :</label>
          <div class="col-sm-7">
            <input type="text" class="" name="phone" id="phone">
          </div> <div class="col-sm-3"></div>
        </div>

        <div class="form-group col-md-12">
          <div class="col-sm-offset-2 col-sm-10">
            <input type="submit" class="btn btn-success" name="gk" id="gk" value="submit">
          </div>
        </div>


        </div>
</form>`

如何在没有页面重新加载的情况下在数据库中插入数据? 我试过这个,有什么想法吗? 下面是我用过的脚本。我认为问题在于脚本。

`<script> 
    $("#gk").click(function(){
                $("form#form_post").submit(function(event){
                    event.preventDefault();
                      var formData = new FormData($(this)[0]);
                     $.ajax({
                        url: 'taskinsert.php',
                        type: 'POST',
                        contentType: false,
                        processData: false,
                        success: function (response) {
                            $('#success').html(response);
                    var form=document.getElementById('form_post').reset();
                                  return false;
                          }
                    });
                  }); 
            }); 
</script>`

1 个答案:

答案 0 :(得分:0)

java脚本:

 $(document).on('click','#save',function(e) {
  var data = $("#form-search").serialize();
  $.ajax({
         data: data,
         type: "post",
         url: "send.php",
         success: function(data){
              alert("Data Save: " + data);
         }
});
 });

你的保存php文件send.php:

<?php 
if(isset($_REQUEST))
{
        mysql_connect("localhost","root","");
mysql_select_db("eciticket_db");
error_reporting(E_ALL && ~E_NOTICE);

$email=$_POST['email'];
$sql="INSERT INTO newsletter_email(email) VALUES ('$email')";
$result=mysql_query($sql);
if($result){
echo "You have been successfully subscribed.";
}
}
?>