Reputation: 85653
I have a form in contact page and three links suppose one, two, and three are in home page and I would like to link to contact page for each link but with one option is to be selected one and with two option is to be selected two and so on.
<select id="message_type" name="message_type" class="inputbox">
<option value="one">Suggestion</option>
<option value="two">Inquiry</option>
<option value="three">Offer</option>
</select>
when link one is clicked the contact page should show option selected one and the like.
How can I do that?
Edit
I have three links in home page
<a href="contact.php" id="one">one</a>
<a href="contact.php" id="two">two</a>
<a href="contact.php" id="three">three</a>
Now I want to show the contact page with option selected one for link one and so on...
here is the code which results the select
<?php
function dropdown($active){
$dropdown=array(
'option1'=>'Suggestion','option2'=>'Inquiry','option3'=>'Offers'
);
foreach($dropdown as $key=>$val){
$array[]=JHtml::_('select.option',$val,$key);
}
$dropdown = JHtml::_('select.genericlist',$array,'message_type','class="inputbox"','text','value',$active);
return $dropdown;
}
?>
and in the form
<form name="feedback" id="frmfeedback" action="" method="post" >
<div>
<label for="msg">Message Type: </label><span class="input"><?php echo dropdown(isset($post['message_type'])?$post['message_type']:'');?> </span>
</div>
.......
Upvotes: 0
Views: 137
Reputation: 171698
Can put a hash in the links on home page:
<a href="contact.html#one">Make a Suggestion</a>
Then on contact page:
$(function(){
var opts=['one','two','three'];
var hash =location.hash;// use browser location object to get hash
if(hash && hash !='#'){
hash= hash.replace('#','');
/* get index of hash from array*/
var optIndex= $.inArray(hash,opts);
/* if not in array value will be empty string, otherwise value of hash*/
$('#message_type').val( optIndx !=-1 ? hash : '' );
}
});
EDIT: If ID's are same as values on the links as shown in question
Can append the hashes to href on home page with:
$('#one,#two,#three').attr('href',function(idx, oldHref){
return oldHref +'#' + this.id;
});
EDIT: Using itemId
in query string of url:
$(function(){
var opts=['477','478','479']; /* not sure these are accurate*/
var a = document.createElement('a');
a.href = location.href;
var ret = {},
seg = a.search.replace(/^\?/, '').split('&'),
len = seg.length,
i = 0,
s;
for (; i < len; i++) {
if (!seg[i]) {
continue;
}
s = seg[i].split('=');
ret[s[0]] = s[1];
}
var currVal=ret['itemId'];
if( currVal !=undefined && $.inArray(currVal,opts)>-i){
$('#message_type').val( currVal);
}
})
Upvotes: 1
Reputation: 2345
For different pages
var url= document.URL;
switch(url)
{
case 'http://jsfiddle.net/bs8dp/':
$('#message_type').val('one');
break;
case 'http://jsfiddle.net/bs66p/':
$('#message_type').val('one');
break;
default:
$('#message_type').val('two');
}
OR
If you have options in same page then
('#your_tab_one').click(function(){
$('#message_type').val('one');
});
('#your_tab_two').click(function(){
$('#message_type').val('two');
});
('#your_tab_three').click(function(){
$('#message_type').val('three');
});
Upvotes: 0