1

I'm trying to submit a form to a different location depending on which (of two) form fields have been filed in.

It work when both fields have a value but when only one if field has a value it always submits to the suppliers/category/ URL. Here is my code.

$('#suppliersForm').submit(function() {

catVal = $('#category').val()
keywordVal = $('#keywords').val()

if( $('#category').val() && $('#keywords').val() ) 
{
var searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/searchresults/' + catVal + '/' + keywordVal
window.location.replace(searchString);
}
else if( $('#category').val() || $('#keywords').val() )
{   
var searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/category/' + catVal
window.location.replace(searchString);  
}
else if( $('#keywords').val() || $('#category').val() )
{   
var searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/keywords/' + keywordVal
window.location.replace(searchString);  
}
return false;
});

Any help would be appreciated

5
  • I think you dont understand the && (AND) and ||(OR) operators. && runs if both statements are true. || runs when one or both statements are true. If one field is left empty the second else if (suppliers/category) will always run. In fact the thirth else if will never run. Commented Sep 23, 2010 at 16:21
  • your right, I didn't fully understand what the || operator did. Is there a way making this work? Commented Sep 23, 2010 at 16:23
  • btw you can check if the value catVal or keywordVal is set, then you don't have to get $('#category').val() or $('#keywords').val() in every if statement. btw dont forget to close this catVal = $('#category').val() with a ; Commented Sep 23, 2010 at 16:24
  • depends on what you want? Do you want the value of #category to be a certain thing when the second if runs? Be more specific in you question. Commented Sep 23, 2010 at 16:24
  • thanks for your help. I understand the operators a little better now Commented Sep 23, 2010 at 16:31

5 Answers 5

1
$('#suppliersForm').submit(function() {

var catVal = $('#category').val();
var keywordVal = $('#keywords').val();
var searchString = "";

if( catVal != "" && keywordVal !="" ) 
{
     searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/searchresults/' + catVal + '/' + keywordVal;
}
else if( catVal != ""  )
{   
     searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/category/' + catVal;
}
else if( keywordVal != "" )
{   
     searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/keywords/' + keywordVal;
}
window.location.replace(searchString);  
return false;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the second checks on both else if:

else if( $('#category').val() || $('#keywords').val() ) --> else if( $('#category').val() )
else if( $('#keywords').val() || $('#category').val() ) --> else if( $('#keywords').val() )

Comments

0

Basically there is no difference between

else if( $('#category').val() || $('#keywords').val() )

and

else if( $('#keywords').val() || $('#category').val() )

So if at least one field contains some value the first else if is executed.

Comments

0
 jQuery('#suppliersForm').click(function(event){       

    var searchString = '';
    var catVal  = $('#category').val();
    var keywordVal = $('#keywords').val();



    if( catVal.length > 0 && keywordVal.length > 0 ){
        searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/searchresults/' + catVal + '/' + keywordVal;
    } else if( catVal.length > 0 ) {
        searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/category/' + catVal;
    } else if( keywordVal.length > 0  ) {
        searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/keywords/' + keywordVal
    } else {
        alert( 'Please enter at least on value' );   
    }


    window.location.replace(searchString);  

});

http://jsfiddle.net/g8AUZ/

Comments

0

I'd probably make it much more concise like this:

var catVal = $('#category').val()
,   keywordVal = $('#keywords').val()
,   searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/';

if( catVal && keywordVal ) 
    searchString += ('searchresults/' + catVal + '/' + keywordVal);
else if( catVal )
    searchString += ('category/' + catVal);
else if( keywordVal )
    searchString += ('keyword/' + keywordVal);
else return;

window.location.replace(searchString);
return false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.