Forum Moderators: coopster

Message Too Old, No Replies

populate dropdown menu

         

Gilead

8:32 pm on Nov 15, 2011 (gmt 0)

10+ Year Member



I am setting up an 'email the user their login information if it wasn't done before' script.

The results are a bit odd. I get the name down the left side of the screen as if it were in the option field, but it's not actually there. The results of this will go to the email script page and send the info.

<form method="post" action="email_user.php">
<?php
include('config.php');
$table_name ="users";

$sql=mysql_query("SELECT contactid, firstname, lastname FROM $table_name");
echo'<select name="ContactID">\n';
while ($row=mysql_fetch_assoc($sql))
{
$contactid= $row['contactid'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
echo "<option value= '$contactid' >";
echo "'$firstname$lastname' \n";
echo '</select>\n';
}

?>
</form>
Somewhere along the way the id got lost.
Thanks!

penders

12:06 am on Nov 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello again (Am I the only one on tonight? :)

Have you viewed the resulting HTML page to see if the output looks correct?

The main problem with the output appears to be that you have your closing </select> inside your while loop. This should be after the while loop, when all the <option> elements have been output. Perhaps have a closing </option> tag in the while loop - although this is optional in HTML.

echo '</select>\n';


You will need to use double quotes here, as otherwise the \n will be treated as a literal '\n' and not a line feed.

Gilead

5:28 pm on Nov 16, 2011 (gmt 0)

10+ Year Member



Next situation:
Once the member is selected and the submit button is hit, I want the login info to be sent and mailed off to the specified user. Even though the variables are in the loop, only the last entry is shown.

echo'<select name="ContactID">';
while ($row=mysql_fetch_assoc($sql))
{
$contactid= htmlentities($row['contactid']);
$firstname= htmlentities($row['firstname']);
$lastname= htmlentities($row['lastname']);
$email= htmlentities($row['email']);
$login= htmlentities($row['member_login']);
$password= htmlentities($row['member_password']);
echo "<option value= $contactid >";
echo "$firstname$lastname ";

}
echo '</select>';
echo $email;
echo $login;
echo $password;
?>
<br />
<input type="hidden" name="email" value="<?php echo $email ?>">
<input type="hidden" name="login" value="<?php echo $login ?>">
<input type="hidden" name="password" value="<?php echo $password ?>">
<input type="submit" name="submit" value="Send Details">

The info is sent and mailed out, but it's the last entry in the database that gets the login details.
What did I miss?
Thanks!

topr8

5:55 pm on Nov 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i always close the option tags: </option>
not sure if this is relevant.

... you are echo-ing the values after the loop so obviously only the last entry is shown

... your hidden form field 'login' will always be the last value as it is not in the loop

Gilead

6:54 pm on Nov 16, 2011 (gmt 0)

10+ Year Member



I placed echo commands inside the loop, but the info that is mailed is still only the last entry.

Update:
I think I know what I need to do, but I'm not sure how to do it.

I need the hidden tags values to change with the selected row.

something like this:
<input type="hidden" name="email" value="<?php mysql_result($row, $email); ?>">
<input type="hidden" name="login" value="<?php mysql_result($row, $login); ?>">
<input type="hidden" name="password" value="<?php mysql_result($row, $password); ?>">
<input type="submit" name="submit" value="Send Details">

Only this doesn't work either with variables or with database fields.

penders

7:20 pm on Nov 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



echo "<option value= $contactid >";


You do need quotes around the attribute value - you appear to have removed them. Whilst this might not solve your current problem it will certainly avoid problems in the future.

echo "<option value='$contactid'>";


I placed echo commands inside the loop, but the info that is mailed is still only the last entry.


The echo commands inside the loop (which generates your option elements) presumably outputs all the records? As topr8 states, the $email, $login, $password, etc. variables will always hold the values of the last record in your dataset. Presumably you should be getting this information from the form submission? The code you have posted above appears to only generate this form to present it to the user. The user then picks the required user from the select control, submits the form, and only at that point can you lookup the user in the database to get the other information - this information wouldn't be stored in hidden fields in the form, since you would need to store every users information in hidden fields?! Unless I'm mistaken....

Gilead

7:39 pm on Nov 16, 2011 (gmt 0)

10+ Year Member



So how best to send all the required information to the process/email page if not with hidden fields?
The process/email page takes three variables: email, login and password.

penders

8:31 pm on Nov 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, assuming this is the way your system works, I would have thought you would only need to send the contactid to your process/email page (which is all that is being submitted with your form).

Your process/email page would then look up the necessary email, login and password from the DB based on the contactid.

Gilead

8:39 pm on Nov 16, 2011 (gmt 0)

10+ Year Member



D'OH! I just made much more work for myself than I needed to.