I'm a newbie in PHP. I'm trying to figure out a way to pass a second paramter in a URL by clicking a link instead of a submit button. For example, my page has a link that reads: <td><a href="agencydetails.php?id=1">Adams County</a></td> - the id=1 is the first parameter, I need to pick the year in a drop down, for example 2009, then when I click the link, I want it to pass agencydetails.php?id=1&year=2009 in my url. I have a simple piece of my code below so you can see what I mean. I have a submit button in there, but I don't want to use it.

<?php 

$year = "2011"; 
// update 
if (isset($_POST['Submit'])) { 
    echo "Year selected is " . $_POST['year']; 
} 
?> 
 
<html> 
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">  
<br /> 
<label for="year">year:</label> 
<select name="year"> 
    <option <?php echo (isset($_POST['year']) || $year == "2011") ? 'selected="selected"' : ''; ?> value="2011">2011</option> 
    <option <?php echo (isset($_POST['year']) || $year == "2010") ? 'selected="selected"' : ''; ?> value="2010">2010</option> 
    <option <?php echo (isset($_POST['year']) || $year == "2009") ? 'selected="selected"' : ''; ?> value="2009">2009</option> 
		<option <?php echo (isset($_POST['year']) || $year == "2008") ? 'selected="selected"' : ''; ?> value="2008">2008</option>
</select> 
<br /><br /> 
<input type="submit" name="Submit" value="Update"> 
</form> 

<table width="50%" class="text">
<tr align="center" valign="top">
	<td><a href="agencydetails.php?id=1">Adams County</a></td>
	<td><a href="agencydetails.php?id=2">Douglas County</a></td>
	<td><a href="agencydetails.php?id=3">Jefferson County</a></td>
	<td><a href="agencydetails.php?id=4">Yuma County</a></td></tr>
</html>

Recommended Answers

All 3 Replies

This has nothing to do with PHP , it is a JavaScript question ...

This may go over your head a bit but this is how i would do it, just tested it in chrome and works:

<?php 
$year = "2011"; 
// update 
if (isset($_POST['Submit'])) { 
    echo "Year selected is " . $_POST['year']; 
}
if(!ctype_digit($_POST['year'])){
	$_POST['year'] = date("Y");
}
?> 
 
<html> 
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">  
<br /> 
<label for="year">year:</label> 
<select onchange="updateLinks(this);" name="year"> 
    <option <?php echo (isset($_POST['year']) || $year == "2011") ? 'selected="selected"' : ''; ?> value="2011">2011</option> 
    <option <?php echo (isset($_POST['year']) || $year == "2010") ? 'selected="selected"' : ''; ?> value="2010">2010</option> 
    <option <?php echo (isset($_POST['year']) || $year == "2009") ? 'selected="selected"' : ''; ?> value="2009">2009</option> 
	<option <?php echo (isset($_POST['year']) || $year == "2008") ? 'selected="selected"' : ''; ?> value="2008">2008</option>
</select> 
<br /><br /> 
<input type="submit" name="Submit" value="Update"> 
</form> 
 
<?php 
$agencies = array(
	1=>'Adams County',
	2=>'Douglas County',
	3=>'Jefferson County',
	4=>'Yuma County');
?>
<table width="50%" class="text">
<tr align="center" valign="top">
	<?php 
	$i = 0;
	$js = '';
	foreach($agencies as $k=>$v){
		echo "<td><a id='link{$i}' href='agencydetails.php?id={$k}&year={$_POST['year']}'>{$v}</a></td>\r\n";
		$js .= "document.getElementById('link{$i}').href='agencydetails.php?id={$k}&year='+obj.value\r\n";
		$i++;
	}
	?>
<script type='text/javascript'>
function updateLinks(obj){
	<?php 
	echo $js;
	?>
}
</script>
</html>

basically just add all your links you want to the $agencies array

You are a genius! That worked perfectly! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.