Passing field text in JS

-1

Well, I pass data from my fields via JS (their codes)!

Example in JS :

function f_veri_dados()
{
    w_form="sai_frm_incl_novo_cara_peri1";
    // Periferico do programa anterior
    w_param = document.forms[w_form].w_cb_peri.value+"@";
    // Marca 
    if(document.forms[w_form].nm_cb_fk_seq_marc.value == "0")
      {     
      alert("Informe a Marca!!");
      document.forms[w_form].nm_cb_fk_seq_marc.focus();
      return false;
      }   
    w_param = w_param + document.forms[w_form].nm_cb_fk_seq_marc.value+"@";  
    // Modelo
    w_param = document.forms[w_form].nm_cb_fk_seq_mode.value+"@";  
    // Nota fiscal
    if(document.forms[w_form].nm_cb_nota_fisc.value == "0")
      {     
      alert("Informe a Nota Fiscal!!");
      document.forms[w_form].nm_cb_nota_fisc.focus();
      return false;
      }     
      w_param = w_param + document.forms[w_form].nm_cb_nota_fisc.value+"@"
}

Within HTML I call function f_veri_dados() to get the field data! But, I would like to also pass the text that the user "typed".

HTML:

<table border="0" width="100%">
    <tr>
        <td>
            <input type="hidden" name="w_cb_peri" value="<?=$w_cb_peri;?>">
        </td>               
        <td align="right" colspan="3" width="12%">
            <font face="arial" color="blue" size="2">Marca :</font>
        </td>
        <td>
            <?
                $w_querybusca="select * from sai_tb_marc order by desc_marc;";
                $w_queryresultado=f_class_conecta_bd($w_querybusca);
                if (pg_num_rows($w_queryresultado) == 0)
                {
                    print("<SCRIPT language=javascript> alert(\"Cadastre uma marca.\");
                        parent.location.replace(\"../sai_prin/sai_menu0.php\");</SCRIPT>");
                }
                print('<select name="nm_cb_fk_seq_marc" id="id_cb_fk_seq_marc" onchange="f_le_modelo(this.form.name);" style="font-size:11; color:Black; width:120">'."\n");
                print('<option value="0"> Selecione</option>'."\n");
                while($w_registro = pg_fetch_object($w_queryresultado))
                {
                    print('<option value="'.$w_registro->seq_marc.'">'.trim($w_registro->desc_marc).'</option>'."\n");
                }
                print ("</select>");
           ?>           
        </td>
        <td align="right" width="14%" >
            <font face="arial" color="blue" size="2">Modelo :</font>
        </td>
        <td id="td_fk_seq_mode" colspan="3" width="20%">
            <select name="nm_cb_fk_seq_mode" id="id_cb_fk_seq_mode" style="font-size:11; color:Black;" width="90">
                <option id="opc_fk_seq_mode" value="0"> Selecione
                </option>
            </select>
        </td>
        <td align="right" width="29%">
            <font face="arial" color="blue" size="-1">Nota Fiscal :</font>
        </td>
        <td align="left" width="17%">
            <?
                $w_querybusca="select * from sai_cad_nf order by num_nf;";
                $w_queryresultado=f_class_conecta_bd($w_querybusca);
                print('<select name="nm_cb_nota_fisc" style="font-size:11; color:Black; width:100">'."\n");
                print('<option value="0"> Selecione</option>'."\n");
                while($w_registro = pg_fetch_object($w_queryresultado))
                {
                    print('<option value="'.$w_registro->seq_nf.'">'.trim($w_registro->num_nf).'</option>'."\n");
                }
                print ("</select>");
            ?>  
        </td>   
    </tr>   
</table><br>
<table border="0" width="50%" align="center">
    <tr>
        <td width="50%">
            <button type="button" style="width:65"  onclick="f_veri_dados();"><img src="../sai_imag/novo-reg.ico">
            </button>
        </td>               
    </tr>
</table>

Let me explain better ... I want to pass the text of a combo . That is, in this way I'm doing it it just takes the option code (in combo ) selected and I would like to pass the text of it together!

    
asked by anonymous 06.08.2014 / 14:54

2 answers

2

Technically this should be a comment, but there are so many points to consider that it is preferable to write more carefully.

Your question is rather confusing, but apparently enough, within the function:

var campoDesejado = document.getElementById('campoDesejado').value;

Being Desired field the ID attribute of a <input> :

<input type="text" id="campoDesejado" />

But I would suggest that you organize yourself better:

  • Even for security reasons, avoid using the names of your database columns directly in HTML. This opens the possibility of a possible attacker to know the basics of its structure without even trying to invade your bank.

  • Form elements must be inside <form> . If they are already, by the code presented you would only have one then trigger the function in the Event onSubmit () and, from one , or tell the function the identifier of the form or define it within the function itself more clearly than it has today:

    function  myFunction( form ) {
    
        // form.action reporta myAction.php
    }
    
  • Or alternatively:

    <script type="text/javascript">
    
        function  myFunction() {
    
            var w_form = document.forms[ 'sai_frm_incl_novo_cara_peri1' ]
    
            // E a partir daí você só usa a variável w_forms
        }
    
    </script>
    

    If you have multiple forms on the same screen (which is no longer ideal) instead of working with the forms collection (document.forms), work with classes.

  • If all this is enough, but there is a need to further improve, refactor your code and separate the HTML from JavaScript.
  • It's almost the same thing, but instead of using the onSubmit Event directly in the HTML, you vary in JS:

    window.onload = function() {
    
        var form = document.getElementById( 'form' );
    
        if( form.addEventListener ) {
    
            form.addEventListener(
    
                'submit', mySubmitFunction, false
            );
    
        } else if( form.attachEvent ) {
    
            form.attachEvent( 'onsubmit', mySubmitFunction );
        }
    };
    
    function mySubmitFunction( event ) {
    
        alert( this.action );
    
        event.preventDefault();
    }
    
        
    06.08.2014 / 16:19
    1

    Thanks for the help @BrunoAugusto!

    But I found a more "efficient" way of showing the name!

    var marca = document.getElementById("id_cb_fk_seq_marc").options[document.getElementById("id_cb_fk_seq_marc").selectedIndex].text;
    alert(marca);
    
        
    06.08.2014 / 18:07