Passing parameters by clicking a link instead of a submit button
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>
RickL66
Newbie Poster
22 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
This has nothing to do with PHP , it is a JavaScript question ...
jkon
Posting Whiz
387 posts since Jan 2009
Reputation Points: 124
Solved Threads: 63
Skill Endorsements: 4
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
Biiim
Posting Pro
504 posts since Oct 2011
Reputation Points: 104
Solved Threads: 83
Skill Endorsements: 7
You are a genius! That worked perfectly! Thanks!
RickL66
Newbie Poster
22 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
Biiim
and
jkon