Update form using Ajax, PHP, MYSQL

后端 未结 3 1255
一个人的身影
一个人的身影 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:18

    Ended up figuring it out. Thanks for everyones help.

    Business Name:
    Address 1:
    Address 2:
     

    update.js

    $(document).ready(function() {
    
    $('form').submit(function(evt) {
      evt.preventDefault();
    
       $.each(this, function() {
                // VARIABLES: Input-specific
                var input = $(this);
                var value = input.val();
                var column = input.attr('name');
    
                // VARIABLES: Form-specific
                var form = input.parents('form');
                //var method = form.attr('method');
                //var action = form.attr('action');
    
                // VARIABLES: Where to update in database
                var where_val = form.find('#where').val();
                var where_col = form.find('#where').attr('name');
    
      $.ajax({
          url: "/ajax/update_company_info.php",
          data: {
                            val: value,
                            col: column,
                            w_col: where_col,
                            w_val: where_val
          },
          type: "POST",
          success: function(data) {         
    
          $('#alert').html("

    Sent Successfully!

    "); } }); // end post });// end each input value }); // end submit }); // end ready

    update_customer_info.php

         $value)
            ${$column} = clean($value);
    
        // Perform MySQL UPDATE
        $result = mysql_query("UPDATE customers SET ".$col."='".$val."'
            WHERE ".$w_col."='".$w_val."'")
            or die ('Error: Unable to update.');
    }
    ?>
    

提交回复
热议问题