variable passing problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

variable passing problem

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post by webspider »

How can you put javascript statement within php tag?

Code: Select all

<? javascriptstatement; ?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You can't.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post 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.. :)
Post Reply