jquery using ajax to send data and save in php

This question already has an answer here:

  • jQuery AJAX submit form 15 answers

  • 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


    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.

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

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

    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.

    链接地址: http://www.djcxy.com/p/71942.html

    上一篇: 通过PHP在GET中清除用户数据

    下一篇: jquery使用ajax发送数据并保存在php中