Passing the text of the JS fields

Asked

Viewed 230 times

-1

Well, I pass the data of 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 the HTML I call the function f_veri_dados() to get the data from the fields! But, I would like to pass also 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 further ... I want to pass the text of a combo. That is, in this way that I’m doing it just takes the option code (in combo) selected and I would like to pass the text (name) of it together!

  • You have to show your HTML to make the problem clearer

  • HTML post!!!!!

  • 1

    @Bruno, if solved, please accept the answer that solved your problem or create yourself the answer so that in the future this post helps another person.

2 answers

2

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

Your question is quite confused, but apparently it would suffice, within the function:

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

Being peasant the ID attribute of a <input>:

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

But I would suggest that you organize yourself better:

  1. Even for security reasons, avoid using the column names of your database directly in HTML. This opens up the possibility of a possible intruder knowing the basics of your structure without even trying to break into your bank.

  2. Form elements must be within <form>. If already, by the code presented you would have only one then trigger the function in Event onsubmit() and, one, either inform the function the form identifier 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 happen to have multiple forms on the same screen (which is no longer ideal) instead of working with the form collection (Document.Forms), work with classes.

  1. If all this is enough, but there is a need to improve further, refactor your code and separate HTML from Javascript.

It’s almost the same thing, but instead of using Event onsubmit directly in 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();
}

1

Thanks for the help @Brunoaugusto!

But I found a more "efficient" way to show the name!

var marca = document.getElementById("id_cb_fk_seq_marc").options[document.getElementById("id_cb_fk_seq_marc").selectedIndex].text;
alert(marca);
  • 1

    Glad you solved it yourself, but see how in asking the right question the answer would have come faster: "How to do, in this function, get the text of the selected option in a dropdown menu?". ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.