jquery使用ajax发送数据并保存在php中[重复]

时间:2022-10-22 23:44:51

This question already has an answer here:

这个问题在这里已有答案:

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

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

 <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,但它去了网址,我认为这不是很安全

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


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

    });

3 个解决方案

#1


14  

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是您可以在其中执行数据库事务的文件名

#2


6  

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,或者只为按钮定义一个click-event-handler。

<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));*/
    }
  });

}

#3


6  

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 ..您可以根据您的表单名称更改此名称。

#1


14  

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是您可以在其中执行数据库事务的文件名

#2


6  

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,或者只为按钮定义一个click-event-handler。

<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));*/
    }
  });

}

#3


6  

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 ..您可以根据您的表单名称更改此名称。