Page 1 of 1
variable passing problem
Posted: Tue Dec 11, 2007 8:08 am
by tommy1987
I am trying to get the selected value of a select box on my page to use in a link. The following doesn't work but you can understand what I am trying to do... (The select box is called cat).
I guess ultimately what I am trying to do is pass a javascript variable into php.
Code: Select all
$lnk = 'addcat.php?cat='.<?='document.write(cat.value)'?>;
Any ideas??
Thanks in advance.
Posted: Tue Dec 11, 2007 8:44 am
by onion2k
Is there more to this script? I'm a bit baffled by your approach. You can't access Javascript values in PHP as you appear to be attempting to do. They're completely separate layers in any application.
Posted: Tue Dec 11, 2007 9:42 am
by webspider
How can you put javascript statement within php tag?
Posted: Tue Dec 11, 2007 9:46 am
by s.dot
You can't.
Posted: Tue Dec 11, 2007 10:05 am
by nathanr
you need a php file similar to this:
Code: Select all
echo '<select name="somename" onchange="navigateBySelect(\'addcat.php\',\'faq\',this);">';
foreach($dbrows as $index => $row) {
echo '<option value="'.$row['catid'].'">'.$row['catname'].'</option>';
}
echo '</select>';
which will output this:
Code: Select all
<select name="somename" onchange="navigateBySelect('addcat.php','faq',this);">
<option value="12">category 12</option>
<option value="13">category 13</option>
<option value="14">category 14</option>
</select>
and finally a javascript to perform the action, like this:
Code: Select all
function getOptionsValue(target) {
return target.options[target.selectedIndex].value;
}
function navigateBySelect(uri, fragment, target) {
document.location.href=uri+'?'+fragment+'='+getOptionsValue(target) ;
}
Hope that helps..
