Update form using Ajax, PHP, MYSQL

后端 未结 3 1284
一个人的身影
一个人的身影 2021-01-19 11:10

I found a tutorial that auto submits the form data but all I want to do is add a submit button to pass the data to ajax.

My goal is to have a form with multiple inp

3条回答
  •  误落风尘
    2021-01-19 11:31

    1. I think that you want to update form when submit.so you should remove submit with a button given below.

      .
      
    2. You should add the given below code in ur js file.

      var myBtn = document.getElementById('myBtn'); 
      myBtn.addEventListener('click', function(event){ 
          Updateform('give id of the form'); 
      }); 
      function updateform(id){
          var data = $('#'+id).serialize();
          // alert(data);
          $.ajax({
              type: 'POST',
              url: "/ajax/update_company_info.php",
              data: data,
              success: function(data) {
                  $('#id').html(data);
                  // alert(data);
                  //alert(data);
              },
              error: function(data) { // if error occured
                  alert("Error occured, please try again");
              },
          });
      
      1. You can retrieve input value in your php code by using unserialize() as an array.So you can save data to database and whatever you want to.i hope you get the answer.Hence,your code will become

           
        Business Name:
        Address 1:
        Address 2:
         

        Your js code become

         var myBtn = document.getElementById('myBtn'); 
         myBtn.addEventListener('click', function(event)
        {   Updateform('form1'); }); 
        function updateform(id){
                    var data = $('#'+id).serialize();
                   // alert(data);
                     $.ajax({
                        type: 'POST',
                        url: "/ajax/update_company_info.php",
                        data: data,
                         success: function(data) {
                             $('#id').html(data);
                       // alert(data);
                         //alert(data);
                          },
                          error: function(data) { // if error occured
                                alert("Error occured, please try again");
                            },
                                    }); }
        

      update_company_info.php will become

             $data=unserialize($_POST['data']);
              // you can retrieve all values from data array and save all .
      

      ?>

提交回复
热议问题