...

View Full Version : Setting Selected attribute in HTML dropdown Forms



srule_
01-07-2008, 05:41 AM
Hello,
I am want to set the selected attribute in one of my forms dynamicly. Users get to the form by clicking on a link, and I want that link they click on to be set as the default value in the form.

ex:
Building the tables of LINKS:


<table>
<tr>
<td>Rank</td>
<td>Empire</td>
<td>Villagers</td>
<td>Score</td>
<td></td>
</tr>

echo '
<tr>
<td>'.$num.'</td>
<td>'.$row['username'].'</td>
<td>'.$row['villagers'].'</td>
<td>'.$row['score'].'</td>
<td><a href="attack.php?id='.$row['username'].'">Attack</a></td>
</tr>';

</table>



The Forum:


<form name="attack_form" action="attack.php" method="get">

Empire To Attack: <select size="1" class="search" name="id" id="jump-button">
<option selected="selected"><?php //WANT THIS TO CHANGE DEPENDING ON LINK CLICKED BY USER ?></option>

<?php
$sql = "SELECT username FROM resources";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
//do...while loop to dispay users in range of templates
do {
echo '<option value="'.$row['username'].'">'.$row['username'].'</option>';
$row = mysql_fetch_assoc($result);
} while ($row)
?>
</select></p>

<input type="submit" name="attack" value="Attack!" />
</p>
</form>

CFMaBiSmAd
01-07-2008, 06:02 AM
Set up a variable $selected that is either "selected="selected"" or an empty string "" depending on the logical conditions you want to test and then echo it in the option tag -


$selected = (your_conditional_test_here) ? "selected=\"selected\"" : ""; // form the string

echo "<option value=\"your_value\" $selected>your_label</option>";

oesxyl
01-07-2008, 06:14 AM
additional to what CFMaBiSmAd say, you can find what user clicks using $_SERVER['HTTP_REFERER'], but you must check if the user don't disable referer in the browser, that means it is a valid url not '-' or ''. :)

PS: in your code, don't do ... while, because the code inside is executed once even the while condition is false, and you don't want that, :)

best regards

srule_
01-07-2008, 06:23 AM
thx for the tip, I have been looking over my script along with what you posted but I can not seem to get my head around this topic.

I need whatever username is selected by the user VIA the links to be the selected username in the form

oesxyl
01-07-2008, 06:33 AM
thx for the tip, I have been looking over my script along with what you posted but I can not seem to get my head around this topic.

I need whatever username is selected by the user VIA the links to be the
selected username in the form

that means that this part:



<table>
<tr>
<td>Rank</td>
<td>Empire</td>
<td>Villagers</td>
<td>Score</td>
<td></td>
</tr>


is different for each user?

username I suppose you pass thru via POST or GET but I don't understand the relation between username and links. More detail could help.

best regards

srule_
01-07-2008, 06:40 AM
Yes, Im sorry. I typed the orginal code wrong. I forgot a loop!

function forming links that is being called on ranks.php


function displayUserRanks() {
$sql = "SELECT * FROM resources ORDER BY score DESC";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$num=1;
do {

echo ' <tr>
<td>'.$num.'</td>
<td><a href="profile.php?id='.$row['username'].'">'.$row['username'].'</a></td>
<td>'.$row['villagers'].'</td>
<td>'.$row['score'].'</td>
<td><a href="attack.php?id='.$row['username'].'">Attack</a></td>
</tr>';
//attack.php?id=123&attack=Attack&#37;21
$row = mysql_fetch_assoc($result);
$num++;
} while ($row);

}

(Yes I know, that should not be a DO loop either, thanks for the help i will change it asap)

form on page link sends user to via GET:


<form name="attack_form" action="attack.php" method="get">
<p>

Empire To Attack: <select size="1" class="search" name="id" id="jump-button">
<option selected="dddd"><?php $_GET['username'] ?></option>

<?php

$sql = "SELECT username FROM resources";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
//do...while loop to dispay users in range of templates
do
{
echo '<option value="'.$row['username'].'">'.$row['username'].'</option>';
$row = mysql_fetch_assoc($result);
} while ($row)
?>
</select></p>
</p>
<input type="submit" name="attack" value="Attack!" />
</p>
</form>


and then I have more code that processes the the form, but I wont post it because it has nothing to do with trying to set the "selected" attribute.

oesxyl
01-07-2008, 07:50 AM
split the code, make query, compute but not output

this is displayUserRanks modified:



function getUsersStuffByRank(){
$users = array();
$query = "select * form resources order by score desc";
$results = mysql_query($query);
if($results){
$num = 1;
while($row = mysql_fecth_assoc($results)){
// you could also check for username as ''
$users[$num++] = $row;
}
}
return $users; // empty - no user, filled - ...
}


you must insert this piece of code where you need after you call getUserStuffByRank ( I don't see where you call displayUserRanks ).



foreach($users as $num => $user){
echo ' <tr>
<td>'.$num.'</td>
<td><a href="profile.php?id='.$user['username'].'">'.$user['username'].'</a></td>
<td>'.$user['villagers'].'</td>
<td>'.$user['score'].'</td>
<td><a href="attack.php?id='.$user['username'].'">Attack</a></td>
</tr>';
//attack.php?id=123&attack=Attack%21
}


output what you want, but more clear:





// other processing, GET's, and what you need
<php $users = getUsersStuffByRank(); ?>

<form name="attack_form" action="attack.php" method="get">
<p>Empire To Attack: <select size="1" class="search" name="id" id="jump-button">
<option selected="dddd"><?php $_GET['username'] ?></option>

<?php
foreach($users as $user){
echo '<option value="'.$user.'">'.$user.'</option>';
}
?>
</select></p>
</p>
<input type="submit" name="attack" value="Attack!" />
</p>
</form>


I don't test anything the code is just to make clear how I see the solution.

best regards

srule_
01-07-2008, 08:21 AM
Thank you, It works!

oesxyl
01-07-2008, 08:35 AM
Thank you, It works!

I'm glad. :)

Could be improved the code and the design. A better solution, I think, is to use ajax, but the pain is you must deal with php, javascript and html in the same time. The happy part is that both javascript and php support oop, :)

PS: I talk about your app not only this module.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum