Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This has to be Easy....Right?

Status
Not open for further replies.

a6april

Technical User
Nov 12, 2010
3
US
Hi All,

I am trying to add a script that produces a link with values and I am having trouble getting a multi select field to produce the correct URL output.

For example:

function evalURL() {
var tempStr = "";
tempStr="tempStr += "MyValuetype="+document.form1.MyValuetype.value+"&";
tempStr += "city="+document.form1.city.value+"&";
document.location=tempStr;
}

The HTML Looks Like This:

Select Type: 
<select name="MyValuetype">
<option value="Type1">Type 1</option>
<option value="Type2">Type 2</option>
<option value="Type3">Type 3</option>
<option value="Type4">Type 4</option>
</select> (REQUIRED)</font><p>
<font face="Arial">
<br />

That all works ok, however the string above for the city works if my html only selects one city. But I need to Select Multiple cities and have the output reflect that

String:
tempStr += "city="+document.form1.city.value+"&";

HTML:
<font face="Arial">
Select City:
<select name="city" multiple="multiple" size="5">
<option selected="selected" value="">All</option>
<option value="City1,">City 1</option>
<option value="City2,">City 2</option>
<option value="City3,">City 3</option>
<option value="City4,">City 4</option>
<option value="City5,">City 5</option>
<option value="City6,">City 6</option>
<option value="City7,">City 7</option>
</select>

If I select Type 2 for the first selection and Multiple Cities, City 1, 3, 5 & 7 the out put link when I press the submit button should look like this:


However I get This:

How do I adjust the String to Allow for multiple selections separated by a comma?

And another question I had was How do I omit the field all together if it is not selected. If the Form field is any or all or not required by the user it should not be added to the string the output contains the data request but the value is either blank or whatever I tell it, but it messes up the output sting.

If I did a poor job explaining this, please let me know I will try again.

Thank You ALL in advance!!!!!
 
You'll need to fashion a function that can return the multiple selections as a string, then you can simply concatenate it normally.

Code:
[gray]
function evalURL() {
[red]var cities=getSelections(document.form1.city);[/red]

var tempStr = "";
tempStr="[URL unfurl="true"]http://link.somedomain.com/somedirectory/begin&;"+document.form1.value+"&";[/URL]
tempStr += "MyValuetype="+document.form1.MyValuetype.value+"&";
tempStr += "city="+[red]cities[/red]+"&";
document.location=tempStr;
}
[/gray]
[COLOR=#4444AA] 
function getSelections(ddObj){
  var selections = new Array();
 for(var i=0; i<=ddObj.options.length-1;i++){
   if(ddObj.options[i].selected){
     selections.push(ddObj.options[i].value);
   }
 }
return selections.join(',');
}

[/color]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Wow, Thank you soooo Much for getting back to me. This is a prime example of how stupid I can be...lol. I always tend to make things more complicated than they are.

Thank You!


Perhaps you can help me with one last thing.... I am not sure this is even possible.

But when lets say one of the Fields is not filled out or selected by the user, we can omit the string entirely from the output.

String:
tempStr += "FavColor="+document.form1.FavColor.value+"&";

HTML:
Select Favorite Color:
<select name="FavColor"><BR>

<option selected="selected" value="">Any</option>
<option value="Red">Red</option>
<option value="Blue">Blue</option>
<option value="Green">Green</option>
<option value="White">White</option>
<option value="Maroon">Maroon</option>
<option value="Cyan">Cyan</option>
</select>

When I use this:
<option selected="selected" value="">Any</option>
And the user does not require a Favorite Color

The output still refers to the Sting:
tempStr += "FavColor="+document.form1.FavColor.value+"&";

Thus having an incorrect link like:
somedomain/directory&FavColor=&city=1,2,3,4

Whereas it should read:
somedomain/directory&&city=1,2,3,4

if the Favorite Color was not selected by the user.

Thanks again for all of your help!!!!!

(I love this Forum, I have a new best place to brag about!!)

 
If you found Phil's post to be valuable make sure you click on the "Thank vacunita for this valuable post!" link immediately below his answer to you. It's the way we recognise helpful posts in these forums.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
You'll need to have if statements to check if things are or aren't selected.

For instance:
Code:
if(cities!=""){
[green]//if cities is not empty, then add to the url string[/green]
tempStr += "MyValuetype="+document.form1.MyValuetype.value+"&";
tempStr += "city="+cities+"&";
}

if(colors!=""){
[green]//if colors is not empty same thing.[/green]
tempStr += "FavColor="+document.form1.FavColor.value+"&";
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I think I may have screwed this Up, The Script reads as:

When I select say Property Type (Residential) The script works fine, Meaning it displays the link correctly with

Propertytype=Residential&Price=whatever&Cities=whatever,whatever,whatever&bathrooms=whatever&bedroom s=whatever

But say I choose the bedrooms and bathrooms at none or any and property type Lots/Land The URL gets broken it is still looking for the bedrooms or bathrooms. Therefore returning a result of Lots or Land With '0' bedrooms or '0' Bathrooms.

I am using this so far:
<script type="text/javascript">

function evalURL() {
var cities=getSelections(document.form1.city);
var tempStr = "";
if(document.form1.bedrooms.value!=0) tempStr += ""+document.form1.bedrooms.value;
if(document.form1.bathrooms.value!=0) tempStr += ""+document.form1.bathrooms.value;
tempStr="tempStr += "propertytype="+document.form1.propertytype.value+"&";
tempStr += "ListPrice="+document.form1.min_price.value+","+document.form1.max_price.value+"&";
tempStr += "City(PostOffice)="+cities+"&";
tempStr += "TotalBedRms="+document.form1.bedrooms.value+"&";
tempStr += "TotalAllBaths="+document.form1.bathrooms.value+"&";
document.location=tempStr;
}

function getSelections(ddObj){
var selections = new Array();
for(var i=0; i<=ddObj.options.length-1;i++){
if(ddObj.options.selected){
selections.push(ddObj.options.value);
}
}
return selections.join(',');

}

</script>

If I take out:
tempStr += "TotalBedRms="+document.form1.bedrooms.value+"&";
tempStr += "TotalAllBaths="+document.form1.bathrooms.value+"&";

and leave the if statement in, it does NOT return the bedrooms or bathrooms when I select property type residential.

So it works the one way, but not the other, and if I fix the one, it works the other way but not the other.

>>>>>>>>>>>>>>>>>>>>>>>

I added your advice to the bottom, however I think I am referring it incorrectly.

<script type="text/javascript">

function evalURL() {
var cities=getSelections(document.form1.city);
var tempStr = "";
tempStr=" tempStr += "propertytype="+document.form1.propertytype.value+"&";
tempStr += "ListPrice="+document.form1.min_price.value+","+document.form1.max_price.value+"&";
tempStr += "City(PostOffice)="+cities+"&";
document.location=tempStr;
}

function getSelections(ddObj){
var selections = new Array();
for(var i=0; i<=ddObj.options.length-1;i++){
if(ddObj.options.selected){
selections.push(ddObj.options.value);
}
}
return selections.join(',');

}

if(TotalAllBaths="0"){
//if TotalAllBaths is not empty same thing.
tempStr += "TotalAllBaths="+document.form1.bathrooms.value+"&";
}

if(TotalBedRms="0"){
//if TotalBedRms is not empty same thing.
tempStr += "TotalBedRms="+document.form1.bedrooms.value+"&";
}

</script>

I am not familiar with the 'if' statements, I think if I show you the whole thing it may be easier to locate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top