Function is not pulling data!

Asked

Viewed 75 times

-1

//INSERIR
function move_patr_seri(Origem, Destino)
{
  var w_Cont_Qtde = 0;
  var w_Qtde_Peri = 4;
  var v_patr = new Array();

	var w_valor = Origem.value;
	var w_tipo;
    w_tipo = "S";
      

	if(Origem.name == "tx_patr")
	    {
		w_tipo = "P";
	    }
	    
	if (w_Cont_Qtde <=  w_Qtde_Peri - 1)
       {
		if ((v_patr.indexOf(w_tipo+w_valor) == -1) && (w_valor != ""))
		{
			var opt = document.createElement("option"); 
			opt.text = w_valor ;
			opt.value = w_valor ;
			Destino.options.add(opt);
			//Cria o Vetor
			v_patr[w_Cont_Qtde] = w_tipo + w_valor;				
			w_Cont_Qtde = w_Cont_Qtde + 1;
			if (Origem.name == "tx_patr"){ document.forms['sai_frm_incl_patr_seri'].tx_patr.focus();}
			else { document.forms['sai_frm_incl_patr_seri'].tx_seri.focus();	}		
			return true;
		}
		else
		{
		    alert("Patrimônio OU Serial já existe OU não é válido!");
			return true;
		}
	  }

	else
	if(w_ver == 1){
		alert("Quantidade atingida!");
		if(confirm("Deseja inserir a mesma quantidade para ambos?") == true)
		{
			w_cont = w_Qtde_Peri;
			w_ver = 0;
			w_Qtde_Peri = w_Qtde_Peri + w_Qtde_Peri;
			return true;
		}
	}
	else
		alert("Quantidade informada ja Incluida !!!");
   return true; 
}
function tira(Destino)
{
	var w_letra;
	var w_tira;
	w_letra = "S";
	if (Destino.name == "cb_Patr"){
			w_letra = "P";		
		}
   
	var i;
	for(i = 0; i < Destino.options.length; i++)
	{ 
		if (Destino.options[i].selected && Destino.options[i].value != "")
		{
			w_tira = w_letra+Destino.options[i].value;
			
			v_patr.splice(v_patr.indexOf(w_tira), 1);		
			w_Cont_Qtde = w_Cont_Qtde - 1;						
			Destino.remove(Destino.selectedIndex);
		}
	}
}
<form name="sai_frm_incl_patr_seri" method="post" >
	<body>	
      <table>
	       <tr>
		<td>   
					<font face="arial" align="center" valign="middle" color="blue" size="-1">PATRIMÔNIO</font><br>
						<input type="text" name="tx_patr" id="id_patr" maxlength="12" size="12" style="font-size:11; color:Black;" onkeypress="return SomenteNumero(event);" onkeyup="Mascara(this,Patri);" value="">
						<input type="button" onClick="move_patr_seri(this.form.tx_patr,this.form.cb_Patr);limpa_patr();" value=">>">
					<br>
					<select multiple size="7" name="cb_Patr" style="width:300">
				   </select>					
					<br>
						<input type="button" align="center" valign="middle" onClick="tira(this.form.cb_Patr)" value="<<">
					<br>
				</td>					
			</tr>
		</table><br>


	</body>	
</form>			

I have a snippet of code that the user inserts data into a box and can remove if they wish. So that was the include part. Now I’m in the "alteration" which is basically the same code, where I only populate the box with the data that the user typed and he sees if he wants to remove or insert more data. But, the function is not taking the data that is showing. Code:

HTML

    <td>   
        <font face="arial" align="center" valign="middle" color="blue" size="-1">PATRIMÔNIO</font><br>
            <input type="text" name="tx_patr" id="id_patr" maxlength="12" size="12" style="font-size:11; color:Black;" onkeypress="return SomenteNumero(event);" onkeyup="Mascara(this,Patri);" value="">
            <input type="button" onClick="move_patr_seri(this.form.tx_patr,this.form.cb_Patr);limpa_patr();" value=">>">
        <br>
            <select multiple size="7" name="cb_Patr" style="width:300">
                <?
                $w_querybusca="$w_select;";    
                $w_queryresultado=f_class_conecta_bd($w_querybusca);    

                while($w_registro = pg_fetch_object($w_queryresultado))
                {
                    print('<option value="'.$w_registro->tx_num_patr.'">'.trim($w_registro->tx_num_patr).'</option>'."\n");
                }
                ?>
           </select>                    
        <br>
            <input type="button" align="center" valign="middle" onClick="tira(this.form.cb_Patr)" value="<<">
        <br>
    </td>

In this stretch I have the box and buttons to include and remove in the box!

HTML

JS

//RETIRAR 
function tira(Destino)
{
    var w_letra;
    w_letra = "S";
    if (Destino.name == "cb_Patr"){
            w_letra = "P";      
        }

    var i;
    for(i = 0; i < Destino.options.length; i++)
    { 
        if (Destino.options[i].selected && Destino.options[i].value != "")
        {
            w_tira = w_letra+Destino.options[i].value;
            v_patr.splice(v_patr.indexOf(w_tira), 1);       
            w_Cont_Qtde = w_Cont_Qtde - 1;                      
            Destino.remove(Destino.selectedIndex.value);
        }
    }
}

That’s the função which takes data from box, where v_patr contains the data that he informed in the inclusion and w_letter is added in front of the value to thus remove from v_patr also (because in it is added the letters before).

The problem lies in v_patr.splice(v_patr.indexOf(w_tira), 1);, where I’m adding the data to it like this:

<?php
$w_select ="SELECT *    FROM public.sai_cad_patr_seri WHERE 
                                        sai_cad_patr_seri.fk_seq_cara_peri = '$arr_w_param[17]'";
$w_querybusc = "$w_select;"; 
$w_queryresult=f_class_conecta_bd($w_querybusc);
$index = 0;
$patr = array();
$seri = array();        
while($w_registro = pg_fetch_object($w_queryresult))
{
    $patr[$index] = "P".trim($w_registro->tx_num_patr);
    $seri[$index] = "S".trim($w_registro->tx_num_seri);
    $index++;
}
$string_patr = implode(",", $patr);
$string_seri = implode(",", $seri);
    print("<SCRIPT language=javascript> 

            v_patr = new Array (2); 

            v_patr = string_patr+','+ string_seri;

        </SCRIPT>");
?>
  • You can put an example of how HTML looks in the browser?

  • And by the way, what do you want with Destino.remove(Destino.selectedIndex); ?

  • @Sergio, with this excerpt I intend to remove the selected value from the combo!

  • Please note that this example does not help you: http://answall.com/questions/35184/copiar-um-option-select-multiple-para-outra-select-multiple-c

  • @Diegosantos I appreciate the example, but it doesn’t fit into the "company criteria", they kind of don’t like Jquery!

  • @Sergio, about the function Destino.remove(Destino.selectedIndex.value); he takes the value of the combo and removes it from her! But the return she is giving is undefinid! What would be?

  • You could put your code in shape Executable here in SO.pt or on Jsfiddle so we can work on it?

  • Of course @Kaduamaral

  • @Kaduamaral’s executable does not work because it takes php variables and uses them together with JS!

  • So @Felipe, just run it on your project and copy the generated source code...

  • ready @Kaduamaral edited to work according to how I’m doing!

  • And as you can see he doesn’t take the data!

Show 7 more comments

1 answer

1


The problem is that you are declaring your variables within the function:

function move_patr_seri(Origem, Destino)
{
  var w_Cont_Qtde = 0;
  var w_Qtde_Peri = 4;
  var v_patr = new Array();

  var w_valor = Origem.value;
  var w_tipo;

So every time the function is called, it’s like box box all items, and at the time of removing there was no more.

To resolve, enter your variables at the beginning of the script.

<script>
   var v_patr = [];
   var w_Cont_Qtde = 0;
   var w_Qtde_Peri = 4;

   function move_patr_seri(Origem, Destino)
   {
      var w_valor = Origem.value;
      var w_tipo;
      w_tipo = (Origem.name == "tx_patr" ? "P" : "S") ;

 Upshot


var Patri = '';
var v_patr = [];
var w_Cont_Qtde = 0;
var w_Qtde_Peri = 4;

// FIX
function SomenteNumero(){ return true;}
function limpa_patr(){return true;}
function Mascara(el, patri){return true;}

//INSERIR
function move_patr_seri(Origem, Destino) {

   var w_valor = Origem.value;
   var w_tipo;
    w_tipo = "S";
      

   if(Origem.name == "tx_patr")
       {
      w_tipo = "P";
       }
       
   if (w_Cont_Qtde <=  w_Qtde_Peri - 1)
       {
      if ((v_patr.indexOf(w_tipo+w_valor) == -1) && (w_valor != ""))
      {
         var opt = document.createElement("option"); 
         opt.text = w_valor ;
         opt.value = w_valor ;
         Destino.options.add(opt);
         //Cria o Vetor
         v_patr[w_Cont_Qtde] = w_tipo + w_valor;            
         w_Cont_Qtde = w_Cont_Qtde + 1;
         if (Origem.name == "tx_patr"){ document.forms['sai_frm_incl_patr_seri'].tx_patr.focus();}
         else { document.forms['sai_frm_incl_patr_seri'].tx_seri.focus();  }     
         Origem.value = '';
         return true;
      }
      else
      {
          alert("Patrimônio OU Serial já existe OU não é válido!");
          Origem.value = '';
         return true;
      }
     }

   else
   if(w_ver == 1){
      alert("Quantidade atingida!");
      if(confirm("Deseja inserir a mesma quantidade para ambos?") == true)
      {
         w_cont = w_Qtde_Peri;
         w_ver = 0;
         w_Qtde_Peri = w_Qtde_Peri + w_Qtde_Peri;
         Origem.value = '';
         return true;
      }
   }
   else
      alert("Quantidade informada ja Incluida !!!");
   return true; 
}

function tira(Destino) {
   var w_letra;
   var w_tira;
   w_letra = "S";
   if (Destino.name == "cb_Patr"){
         w_letra = "P";    
      }
   
   var i;
   for(i = 0; i < Destino.options.length; i++)
   { 
      if (Destino.options[i].selected && Destino.options[i].value != "")
      {
         w_tira = w_letra+Destino.options[i].value;
         
         v_patr.splice(v_patr.indexOf(w_tira), 1);    
         w_Cont_Qtde = w_Cont_Qtde - 1;                  
         Destino.remove(Destino.selectedIndex);
      }
   }
}
<form name="sai_frm_incl_patr_seri" method="post">
   <table>
      <tr>
         <td>
            <font face="arial" align="center" valign="middle" color="blue" size="-1">PATRIMÔNIO</font>
            <br>
            <input type="text" name="tx_patr" id="id_patr" maxlength="12" size="12" style="font-size:11; color:Black;" onkeypress="return SomenteNumero(event);" onkeyup="Mascara(this,Patri);" value="">
            <input type="button" onClick="move_patr_seri(this.form.tx_patr,this.form.cb_Patr);limpa_patr();" value=">>">
            <br>
            <select multiple size="7" name="cb_Patr" style="width:300">
            </select>
            <br>
            <input type="button" align="center" valign="middle" onClick="tira(this.form.cb_Patr)" value="<<">
            <br>
         </td>
      </tr>
   </table>
</form>

  • i am not declaring the variables within the function! more yes I declare them in PHP! On the executable I left them inside to show how they will work!

  • So @Felipe, the problem I detected was this, your script is working fine. Checks the location you are declaring the variables (tries to put in the head) and checks if you are declaring with the reserved word var. I don’t know if I influence her, but see if she’s in the same scope of duty.

  • the part I added from php where it adds the values in v_patr does not influence?

  • If you are declaring the variable after the function, it will not recognize its global variable. @Felipe

Browser other questions tagged

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