Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Disable input in the form

    Hello,

    Personnel have the following code:

    Code:
    <script type="text/javascript">
    function mostraDiv(valor){
    if(valor == "fisica"){
    document.getElementById("fisica").style.display = "";
    document.getElementById("juridica").style.display = "none";
    document.getElementById("juridica").disable = "true";
    document.getElementById("fisica").disable = "false";}
    else if(valor == "juridica"){
    document.getElementById("fisica").style.display = "none";
    document.getElementById("fisica").disable = "true";
    document.getElementById("juridica").style.display = "";
    document.getElementById("juridica").disable = "false";}}
    </script>
    
    <table width="480" border="1" style="margin-left: 60px;"><tbody>
    <tr class="radio">
    <td width="200"></td>
    <td width="260" scope="col"><strong>TIPO DE CADASTRO:</strong></td>
    <td width="240" scope="col"><input type="radio" id="pf" name="selecionar" value="fisica" onChange="mostraDiv(this.value)" /><label for="pf">PESSOA FÍSICA</label></td>
    <td width="70"><strong style="color:#818181">|</strong></td>
    <td width="270" scope="col"><input type="radio" id="pj" name="selecionar" value="juridica" onChange="mostraDiv(this.value)"><label for="pj">PESSOA JURÍDICA</label></td>
    </tr>
    </tbody></table>
    
    <table><tbody>
    <form name="form2" method="post" action="{$smarty.server.PHP_SELF}">
    <input type="hidden" name="register" value="true" />
    
    <tr id="fisica">
    <td class="fieldarea">CPF/MF:</td>
    <td><input type="text" name="cpf[1]" value="" onpaste="return false;" id="cpf" maxlength="14" size="43" placeholder="Digite o seu CPF..."/></td>
    </tr>
    
    <tr style="display: none;" id="juridica">
    <td class="fieldarea">CNPJ/MF:</td>
    <td><input name="cnpj[1]" id="cnpj" value="" type="text" maxlength="18" size="43" placeholder="Digite o seu CNPJ..."></td>
    </tr>
    
    <tr>
    <td width="150" class="fieldarea">Nome:</td>
    <td><input type="text" name="firstname" value="Nome" class="large" placeholder="Digite o seu nome..."></td>
    </tr>
    
    <tr>
    <td>
    <button type="submit"><strong>Enviar</strong></span></button>
    </td>
    </tr>
    
    </form>
    </tbody></table>
    I was wondering if someone could help me make a change, I want to disable an input according to the option chosen.

    When selecting "PESSOA FISICA", it disables the input of the "PESSOA JURIDICA", and vice versa.

    Another issue is that to submit the form and a validation error occurs, it is not with the person chosen fields open. That is, if I choose the "PESSOA JURIDICA", submit the form and an error occurs, it returns to the fields of the "PESSOA FISICA", is not in the fields of "PESSOA JURIDICA". I know I have to create a function type checked, but do not know how to do this.

    I do not have much knowledge in java, just the basics. Could anyone help me do this? take much of this change.

    Thanks in advance!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Code:
    document.getElementById("fisica").disabled = true;

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Wait, I didn't know that the id refers to tr not to the form control.

    Why don't you just refer to 'cpf' and 'cnpj' ids to disable the fields?

    Code:
    document.getElementById("cpf").disabled = true;
    Code:
    document.getElementById("cnpj").disabled = true;

  • #4
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Wait, I didn't know that the id refers to tr not to the form control.

    Why don't you just refer to 'cpf' and 'cnpj' ids to disable the fields?

    Code:
    document.getElementById("cpf").disabled = true;
    Code:
    document.getElementById("cnpj").disabled = true;
    Thanks for the comment, had not seen this little mistake, but still does not work. :/

    I need an input is deactivated according to the option chosen.

  • #5
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by andredoom View Post
    Thanks for the comment, had not seen this little mistake, but still does not work. :/

    I need an input is deactivated according to the option chosen.
    I left the script as follows:

    Code:
    function mostraDiv(valor){
    if(valor == "fisica"){
    document.getElementById("fisica").checked = "checked";
    document.getElementById("fisica").style.display = "";
    document.getElementById("juridica").style.display = "none";
    document.getElementById("cnpj").disabled = "true";
    document.getElementById("cpf").disabled = "false";}
    else if(valor == "juridica"){
    document.getElementById("juridica").checked = "checked";
    document.getElementById("fisica").style.display = "none";
    document.getElementById("juridica").style.display = "";
    document.getElementById("cnpj").disabled = "false";
    document.getElementById("cpf").disabled = "true";}}
    But he left the two fields off ..

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Remove the quotes around true and false. It should be a boolean not string.

  • #7
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by andredoom View Post
    Thanks for the comment, had not seen this little mistake, but still does not work. :/

    I need an input is deactivated according to the option chosen.
    Quote Originally Posted by glenngv View Post
    Remove the quotes around true and false. It should be a boolean not string.
    Oww that simple thing, thought it was a big deal .. It worked, he deactivated the field ..

    But a new problem arose, the script is not working on google chrome ta, know what can be done?

  • #8
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I managed to make it work in chrome.

    Now I have another question. Sorry so many questions, I'm a little layman on the subject.

    I'm having problems checked, if the user chooses an option and triggers the button to submit the form, and then an error occurs and the page is updated, but it does not come with the option that the user selected in the open, ie, the button selected input is not checked ..

    Do you know any way to save it which option the user chose, and if the page was refreshed he kept this option?

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Are you doing a server-side (PHP) validation only without client-side (JS) validation? I suggest you do both as much as possible. With JS validation, the page will never be refreshed and the selections will be kept intact.

  • #10
    New Coder
    Join Date
    May 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This would be significantly easier with jQuery seeing as how the CSS selectors would allow you to easily select the form elements you desire and disable them.

  • #11
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hello,

    I've actually managed to make the script work, to disable the fields.

    I'm having trouble just keeping the selections that the user chose intact.

    There is no way to do this with jquery?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •