Jump to content

[SOLVED] Replace problem.


Darkness Soul

Recommended Posts

Yo,

 

I'm developing a filter using checkbox to active the options. When I check an option, its send a string to an input text and submit the form; when I uncheck, its replace for nothing and submit the form.

 

The problem is: the first time I try to uncheck, the replace is not working. Like the exemple:

 

    > string = ""

I check a, send a string;

    > string = "a"

I uncheck a, replace fail;

    > string = "a"

I check a, send another string;

    > string = "aa"

I uncheck a, replace the second string;

    > string = "a"

 

This "string builder" genereta a SQL script to put in a where case and when submited, filter the data.

There is a prototype "trim()", its work perfectly.

The function below:

 

function filtraSelecionado ( objeto , tabela , campo , alvo , tipo ) {
    
    var ifrm_selecao = document.frm_Filtragem ;
    var ifrm_formulario = ifrm_selecao.formFiltro ;
    var ifrm_campo = ifrm_formulario[ alvo ] ;
    var str_condicao = "" ;
    var str_expressao_regular = "" ;
    
    if ( tipo == 'numero' ) {
        str_condicao = " "+ campo +" = "+ objeto.value +" " ;
    }
    else {
        str_condicao = " "+ campo +" LIKE '%"+ objeto.value +"%' " ;
    }
    
    while ( str_condicao.match ( '  ' ) != null ) {
        str_condicao = str_condicao.replace ( '  ' , ' ' ) ;
    }
    
    str_condicao = str_condicao.trim () ;
    ifrm_campo.value = ifrm_campo.value.replace ( str_condicao , '' ) ;
    ifrm_campo.value = ifrm_campo.value.trim () ;
    
    if ( ifrm_campo.value.length > 0 ) {
        str_condicao = " OR " + str_condicao ;
    }
    
    if ( objeto.checked != false ) {
        ifrm_campo.value = ifrm_campo.value + str_condicao ;
    }
    
    while ( ifrm_campo.value.match ( '  ' ) != null ) {
        ifrm_campo.value = ifrm_campo.value.replace ( '  ' , ' ' ) ;
    }
    
    ifrm_campo.value = ifrm_campo.value.trim () ;
    
    ifrm_campo.value = ifrm_campo.value.replace ( /OR OR/ , 'OR' ) ;
    ifrm_campo.value = ifrm_campo.value.replace ( /OROR/ , 'OR' ) ;
    ifrm_campo.value = ifrm_campo.value.replace ( /OR$/ , '' ) ;
    ifrm_campo.value = ifrm_campo.value.replace ( /^OR/ , '' ) ;
    
    ifrm_campo.value = ifrm_campo.value.trim () ;
    
    ifrm_formulario.submit () ;
}

 

Thanks for the help.

 

dSoul

Link to comment
https://forums.phpfreaks.com/topic/76384-solved-replace-problem/
Share on other sites

when I use a string like str_condicao, the replace ignore regex..

the string is somelike " int_id = 1 "

 

damn, after many reviews, I go check the trim function, its a prototype.. I've commented the prototype and past it as a function.. the error gone..

 

i will use it as function.. have no much time to accomplish this.. so, I will back to this after, for now, consider it solved..

 

sorry for busy :-(

 

thanks anyway

 

dSoul

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.