php jquery使用ajax发送数据并保存在php中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19029703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:41:56  来源:igfitidea点击:

jquery using ajax to send data and save in php

phpjqueryhtmlajax

提问by user1868185

Good day im am trying to send or get data from a form and then using jquery and then ajax to send the data into a php page that should save it in the database how can i do it in jquery and use ajax to do it, any help will do and thanks!

美好的一天,我正在尝试从表单发送或获取数据,然后使用 jquery 和 ajax 将数据发送到 php 页面,该页面应该将其保存在数据库中我如何在 jquery 中执行并使用 ajax 执行此操作,任何帮助会做,谢谢!

HTML page 1 that will use jquery ajax to send data into the php page

将使用 jquery ajax 将数据发送到 php 页面的 HTML 页面 1

 <form>
       Name:<input type='text' name='name'>
       E-mail:<input type='text' name='email'>
       Gender:<select name='gender'>
       <option value='male'>male</option>
       <option value='female'>female</option>
       </select>
       Message:<textarea name='about'></textarea>
 </form>

PHP page 2 that would recieve the data from the page 1 form

PHP 页面 2 将接收来自页面 1 表单的数据

<?php
echo "
 $_POST['name'];
 $_POST['email'];
 $_POST['gender'];
 $_POST['about'];
";  
?>

any help with this project would help us greatly and thanks!

对这个项目的任何帮助都会对我们有很大帮助,谢谢!

(update) This is the jquery that i tried to use but it went to the url and i think that is not very safe

(更新)这是我尝试使用的 jquery,但它转到了 url,我认为这不是很安全

    $(document).ready(function(){
    $("#chat").click(function(){
        $("#content").load("a.php");
    });


    $("#send").ajaxSubmit({url: 'a2.php', type: 'post'})

    });

回答by Sunil Verma

you can use following code :

您可以使用以下代码:

var form = new FormData($('#form_step4')[0]);
form.append('view_type','addtemplate');
$.ajax({
    type: "POST",
    url: "savedata.php",
    data: form,
    cache: false,
    contentType: false,
    processData: false,
    success:  function(data){
        //alert("---"+data);
        alert("Settings has been updated successfully.");
        window.location.reload(true);
    }
});

where savedata.php is the file name in which you can do the the DB things

其中 savedata.php 是您可以执行数据库操作的文件名

回答by ratnesh dwivedi

Try this one:

试试这个:

 <form id="formId">
           Name:<input type='text' name='name'>
           E-mail:<input type='text' name='email'>
           Gender:<select name='gender'>
           <option value='male'>male</option>
           <option value='female'>female</option>
           </select>
           Message:<textarea name='about'></textarea>
           <input type="button" value="Send" onclick="save()"/>
     </form>

 <script type="javascript">

    function save(){
        var query = $('#formId').serialize();
        var url = 'savedata.php';
        $.post(url, query, function (response) {
         alert (response);
        });

    }
</script>

assign Id to your form... for now in my code i have given ID formId.. you can change this one as per your form name.

将 Id 分配给您的表单...现在在我的代码中我已经给出了 ID formId .. 您可以根据您的表单名称更改这个。

回答by Karlton

Hi i would start by adding a id to the form. and then either go with a onclick on the button element, or just define a click-event-handler for the button.

嗨,我会首先在表单中添加一个 id。然后要么在按钮元素上单击 onclick,要么只为按钮定义一个单击事件处理程序。

<form id="my_form">
       Name:<input type='text' name='name'>
       E-mail:<input type='text' name='email'>
       Gender:<select name='gender'>
       <option value='male'>male</option>
       <option value='female'>female</option>
       </select>
       Message:<textarea name='about'></textarea>
       <input type="button" value="Send" onclick="sendForm()"/>
 </form>

Then the jquery/ajax/js part.

然后是 jquery/ajax/js 部分。

 function sendForm(){
    $.ajax({
    type: "POST",
    url: "PAGE2.php",
    data: jQuery("#my_form").serialize(),
    cache: false,
    success:  function(data){
       /* alert(data); if json obj. alert(JSON.stringify(data));*/
    }
  });

}