Page 1 of 1

Pulling data from a database to be emailed via PHP form

Posted: Thu Apr 15, 2004 7:37 am
by rach73
Hi,

I'm very new to PHP and I wonder if anyone could help. I've created an online form which emails the results to me. It is all working fine apart from one field - a drop down menu of company names which gets its contents from a database (called tblCompanies). The field appears ok on screen, the company names are shown, but when the results are send in an email the company name (companyname) is replaced by the id number of the company (companyid).

The code for the drop down menu part of the form is here:

<select name="company" id="company" >
<option value="0">-- Select company from list --</option>
<?
// Select existing companies from the Companies table and populate option list

$query = "select * from tblCompanies GROUP by companyname order by companyname";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<option value=".$row["companyid"].">".$row["companyname"]."</option><br>";}
?>
</select>



and the line for emailing the selected company name is here:


$mesg .= "Company Applied to: $_POST[company]\n";


I would like it to be able to email the companyname to me, rather than the companyid, from the tblCompanies database.

Could anyone help me?

Many thanks for any help.
Rach

Posted: Thu Apr 15, 2004 8:00 am
by magicrobotmonkey
change
"<option value=".$row["companyid"].">".$row["companyname"]."</option>"

to

"<option value=".$row["companyname"].">".$row["companyname"]."</option><

Posted: Thu Apr 15, 2004 8:06 am
by kendall
rach73,

dude uhm firstly i dont know if this is a typo thing but
<option id="company" name="company" value="mysql entry">
note the id and name attributes

secondly,
the reason why you get the "id" is because
echo "<option value=".$row["companyid"].">
WHAT DO YOU EXPECT...lol
the correct thing i believe would be

Code: Select all

echo "<option value=".$row["companyname"].">".$row["companyname"]."</option><br>";
got it

Kendall

Posted: Thu Apr 15, 2004 8:46 am
by rach73
Thanks for your replies - it works now - with one small problem. It takes the names of the companies from the database, but only picks up the first word of each name. Do you know how I can make sure it puts the whole full name of the company into the email?

Thanks
Rach

Posted: Thu Apr 15, 2004 8:49 am
by JayBird
Easy :)

You need to put the option values in quotes like this

Code: Select all

echo "<option value="".$row["companyname"]."">".$row["companyname"]."</option><br>";
Mark

Posted: Thu Apr 15, 2004 8:49 am
by markl999
Change:
echo "<option value=".$row["companyname"].">".$row["companyname"]."</option><br>";

to:
echo '<option value="'.$row['companyname'].'">'.$row['companyname'].'</option><br>';

this will put quotes around the company name, without them you'll end up with value=foo bar .. and only foo will get posted, with the quotes you get "foo bar" and both will be posted.

Edit: Bech100 wins by a nose! ;)

Posted: Thu Apr 15, 2004 9:08 am
by rach73
Thanks very much to you all - that works perfectly.