Pulling data from a database to be emailed via PHP form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rach73
Forum Newbie
Posts: 5
Joined: Thu Apr 15, 2004 7:37 am

Pulling data from a database to be emailed via PHP form

Post 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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

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

to

"<option value=".$row["companyname"].">".$row["companyname"]."</option><
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
rach73
Forum Newbie
Posts: 5
Joined: Thu Apr 15, 2004 7:37 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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! ;)
rach73
Forum Newbie
Posts: 5
Joined: Thu Apr 15, 2004 7:37 am

Post by rach73 »

Thanks very much to you all - that works perfectly.
Post Reply