Passing parameters to IFRAME with multiple conditions

Hi,

I am a novice at this and I got this code online and tweaked it a little. But it doesn't work. I am trying to pass 2 of 3 possible variables to an inline frame. Var_1 is always passed through, and either var_2 or var_3. The following is a sample of the code I am currently using; i have changed the variable names to make it easier to follow.

<iframe name="Shop" id="Shop" width="100%" height="1875" src="shop.php?"></iframe>
<script type="text/javascript">
if(location.search.length>0){
    var search = location.search;
    search = search.replace(/\?/,'');
   
    var searchAttributes = search.split('&');
    for(var no=0;no<searchAttributes.length;no++){
        var items = searchAttributes[no].split('=');
        eval("var "+items[0]+" = '"+items[1]+"';");
    }
    if(Variable_1 && Variable_2){
        document.getElementById('Shop').src='shop.php?var_1='+Variable_1+'&var_2='+Variable_2;
                 }
    else { if(Variable_1 && Variable_3){
        document.getElementById('Shop').src='shop.php?sid='var_1='+Variable_1+'&search[text]='+Variable_3;
                 };}
}else{
    document.getElementById('Shop').src='blank.htm';

}
</script>

It appears that I a able to pass Variable_1 and Variable_2 into the inline frame without a problem. However, in cases where Variable_3 is used the variable is not passed through...and the inline frame only lands at shop.php?. I think it is something to do with the if else statement, but I looked it over a thousand times and I can't seem to figure it out. Can anyone help be figure out what's wrong with this code? Any help would be much appreciated.

Thanks
musqoAsked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:
Here is a vastly simplified version

<iframe name="Shop" id="Shop" width="100%" height="1875" src="blank.htm"></iframe>

<script type="text/javascript">
var search = location.search;
var url = "blank.htm";
if(search) {
  search = search.substring(1);
  var parms = new Array()
  var searchAttributes = search.split('&');
  for(var i=0;i<searchAttributes.length;i++){
    var items = searchAttributes[i].split('=');
    parms[items[0]] = items[1];
  }
  url ='shop.php?sid='var_1='+parms["Variable_1"];
  if (parms["Variable_3"]) url+='&search[text]='+parms["Variable_3"]
  else url+='&var_2='+parms["Variable_2"];
}
document.getElementById('Shop').src=url;

</script>
0
 
rlbalanCommented:
check " search[text]= " ..
0
 
musqoAuthor Commented:
i am not sure I understand what you mean...but I switched search[text] and var_2 and search[text] seems to work fine if it is the first condition. The script appears to stop executing after calculating the first condition.
0
 
Michel PlungjanIT ExpertCommented:
you MAY have to change
'&search[text]='

to

'&search%5Btext%5D='

 
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.