4 Replies - 2513 Views - Last Post: 03 January 2014 - 04:09 PM

#1 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Function to submit a form with just changes - replace not working

Posted 03 January 2014 - 01:25 AM

Hi Guys!

I have the following setup.

1) I had a form that pulled data from a db (checkboxes, radios, inputs and selects)
2) When those are changed, I want to capture just the changes and submit those back to the db
3) When a form value is changed back to original or when it changes again (ie radio orig=1, change1=2, change2=3), I only want to capture the most recent. If the change reflects the original value, I want to remove the name from the submit string.

To do this, I create my form with php and then I create an identical, hidden form called "Mirror" and all the id's/names reflect the form id's/names starting with the word "Mirror_".

Anyway, I have the form changes and a reference point of the original and I'm curious about two things here.

1) When I try to replace, it isn't working. No errors, no flags just not doing anything.
2) From anyone with more experience in this type of layout, how does the code/design look? Anything I could improve on here?

As always, thanks in advance!!
$(document).on('change', 'input, select', function(e){
    
    //Basic variables
    var SubmitString = $("#SubmitString").val();
    var AppendString = "";
    var CurMatch = false;
    
    //Get ID's and Names of visible inputs
    var VisID = $(this).attr('id');
    var VisNameAttr = $(this).attr('name');
    var VisName = '[name="' + VisNameAttr + '"]';
    
    //Get ID and names of mirror inputs
    var MirID = 'Mirror_' + VisID;
    var MirNameAttr = 'Mirror_' + VisNameAttr;
    var MirName = '[name="' + MirNameAttr + '"]';
    
    //Get values by ID (Can be used on text, select and single choice checkboxes)
    var VisIDVal = $(this).val();
    var MirIDVal = $('#'+MirID).val();
    
    //Find out if the checkboxes are checked
    var VisChkVal = $(this).prop('checked');
    var MirChkVal = $('#'+MirID).prop('checked');
    
    //Get value of checked box in multi-name group by Name (For Radios)
    var VisNameVal = $(VisName + ':checked').val();
    var MirNameVal = $(MirName + ':checked').val();
    
    //Get the type of input for processing
    var InpType = $(this).prop('type');
    
    /*
      Radio Steps:
        1) Identify Name
        2) Identify Value
        3) Create String [Name=Value]
        4) Check against original value
          a) If == original value, remove from string
        5) Search 'SubmitString' for name
          a) If name found, identify old value with parse
          B)/> Check if old value = original value
          c) Create replace string to replace old value with new value so we aren't duplicating
        6) If name not found in 'SubmitString', add new value with 'AppendString'        
    */
    
    if(InpType == 'radio'){
      if(VisNameVal == MirNameVal){
        CurMatch = true;
      }else{                              
        /*
          This is a temporary work around and is going to need to be updated.
          Problems:
            This removes the name of the DOM element. The DB won't be updated with the GET from the PHP but it's going to create a larger string than needed. 
          Possible resolutions:
            1) Search 'SubmitString' for name. 
            2) Identify position
            3) Capture string from position of 2 to the following ';'
            4) String has been identified, perform replace function at this point        
        */
        $('#SubmitString').val().replace(/VisNameAttr/g, "");
      }              
      AppendString = VisNameAttr + "=" + VisNameVal + "&";
      //alert(VisNameAttr + ":" + VisNameVal + "--" + MirNameAttr + ":" + MirNameVal);  
    }else if(InpType == 'checkbox'){
      if(VisChkVal == MirChkVal) CurMatch = true;
      AppendString = VisNameAttr + "=" + VisID + "&";
      //alert(VisNameAttr + "-id=" + VisID + ":" + VisChkVal + "--" + MirNameAttr + "-id=" + MirID + ":" + MirChkVal);      
    }else{
      if(VisIDVal == MirIDVal) CurMatch = true;
      AppendString = VisID + "=" + VisIDVal + "&";
      //alert(VisID + ":" + VisIDVal + "--" + MirID + ":" + MirIDVal);
    }
    
    if(CurMatch == true){
      $("#SubmitString").val().replace(AppendString, '');
    }else{
      $("#SubmitString").val(SubmitString + AppendString);
    }
    
  });




Is This A Good Question/Topic? 0
  • +

Replies To: Function to submit a form with just changes - replace not working

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Function to submit a form with just changes - replace not working

Posted 03 January 2014 - 03:18 AM

problem #1, checkboxes and radiobuttons do not change, i.e. the elementís value is always the same no matter whether itís checked or not

firstly, does it really matter whether you do a complete update?

secondly, if you only want to update where the elementsí state changes, use AJAX, not a form submit. IMO way easier to implement.
Was This Post Helpful? 0
  • +
  • -

#3 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: Function to submit a form with just changes - replace not working

Posted 03 January 2014 - 08:38 AM

Thanks!

The checkbox returns true or false when it is read in this function. And the radio returns the number. So if I have drive thru as a radio category and it has 3 options, monthly/weekly/yearly, I can get the value that was clicked in the group.

And as far as ajax, I have a separate submit function I'm using that utilizes ajax and submits the string I created. The reason I don't want to submit the entire form is because it's actually a lot of data and it will probably grow. I'm trying to write this so I can use it across the board on my site on big and small forms but only capture the updates and submit to the php.

Any thoughts on the replace issue?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: Function to submit a form with just changes - replace not working

Posted 03 January 2014 - 11:04 AM

line #76, the change is not saved anywhere.
Was This Post Helpful? 1
  • +
  • -

#5 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 836
  • Joined: 16-September 09

Re: Function to submit a form with just changes - replace not working

Posted 03 January 2014 - 04:09 PM

I don't know if I follow? I have an input named 'SubmitSting' and I'm creating the variable with the AppendString before I get to it? What am I not saving correctly? Sorry if I don't follow - maybe I've just been looking at the same thing too long.

Thank you for helping :)/>

{{{EDIT}}}
lol nm - I get it - I looked at it again and I guess I was confusing reading with assigning at that point - it's been a long week. Much appreciated!

This post has been edited by guyfromri: 03 January 2014 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1