i have a form with duplicate variables...

Discussion in 'PHP' started by Abh, Mar 17, 2011.

  1. #1
    The form has, let's say, the variable "name".
    But i had to split the form in two parts and add some more fields for each, for people and for companies. I made that with some javascript and duplicated the existing fields i needed.
    Problem is, whenever i try to submit the form, it tells me i haven't filled in "name", probably because it sees the empty variable in the second hidden field.

    Can i do something about this without assigning new variables? Maybe with javascript? Because i'm working on prestashop and if i do "name1" i'll have to modify half the script to add it.

    The javascript code that hides two divs is:
    
    function checkMixed(el)	{
    if (el.value == "1")
    {
    document.getElementById('per_fizica').style.display='block';
    document.getElementById('per_juridica').style.display='none';
    }
    else
    {
    document.getElementById('per_juridica').style.display='block';
    document.getElementById('per_fizica').style.display='none';
    }
    }
    
    Code (markup):
    and the checkbox that makes the switch:
    
            <p class="checkbox"> <span>Persoana</span>
                <input name="pessoa" type="radio" id="id_pj" value="1" onclick="checkMixed(this);"  /> 
                <label for="id_pj">{l s='Juridica'}</label>
                <input type="radio" name="pessoa" id="id_pf" value="2" onclick="checkMixed(this);" />
                <label for="id_pf">{l s='Fizica'}</label>
            </p>
    
    Code (markup):
     
    Abh, Mar 17, 2011 IP
  2. samo7

    samo7 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try the following javascript code:

    function checkMixed(el) {
    if (el.value == "1")
    {
    document.getElementById('per_fizica').style.display='block';
    document.getElementById('per_fizica').disabled=false;
    document.getElementById('per_juridica').style.display='none';
    document.getElementById('per_juridica').disabled=true;
    }
    else
    {
    document.getElementById('per_juridica').style.display='block';
    document.getElementById('per_juridica').disabled=false;
    document.getElementById('per_fizica').style.display='none';
    document.getElementById('per_fizica').disabled=true;
    }
    }
     
    samo7, Mar 17, 2011 IP
  3. Abh

    Abh Active Member

    Messages:
    162
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you for the hint, your code wasn't actually doing the job, but it gave me an idea and i ended up using:
    if (per_j && per_j.parentNode && per_j.parentNode.removeChild){
    					per_j.parentNode.removeChild(per_j);
    					}
    Code (markup):
    Thanks a lot!
     
    Abh, Mar 17, 2011 IP
  4. samo7

    samo7 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The idea was to disable the form element to avoid the duplication, but removeChild is also a good solution.
     
    samo7, Mar 18, 2011 IP
  5. vediovis

    vediovis Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    remove child is interesting as you are sure you will not send the deleted elements with your form
     
    vediovis, Mar 22, 2011 IP
  6. Abh

    Abh Active Member

    Messages:
    162
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Yeah, i just don't know how to bring it back :)
    But i made it so that when you tick a box, it hides the other one. That way i don't need that div back.
     
    Abh, Mar 22, 2011 IP