Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Need help mailing someone based on a form variable

Codes here !

Moderators: macek, egami, gesf

Need help mailing someone based on a form variable

Postby amp » Wed Apr 02, 2003 3:19 am

Hi all

I'll try simplify my problem as much as I can so you can understand me. I have a form that takes the users input & puts it into a mysql DB, 1 of the fields in the form is a dropdown menu containing names that are being taken from a table in the DB.
My form is submitted to a file called process.php, here is my process.php file
Code: Select all
<?php
include("config.php");
$dbh = mysql_connect ($dbhost, $dbuname, $dbpass) or die ( 'I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname) or die (mysql_error());
$result = mysql_query ("INSERT INTO travel (prn, date, name, dir, purpose, sponsor, refund, what_refundable, flight_cost, subsis_cost, no_nights, depart_location, depart_time, out_flight, via_location, via_arrive, via_depart, arrive_location, arrive_time, arrive_flight, return_location, return_time, return_flight, via_location2, via_arrive2, via_depart2, depart_time2, arrive_time2, flight_num2, uname) values ('".$_POST['prn']."','".$_POST['date']."','".$_POST['name']."','".$_POST['dir']."',
'".$_POST['purpose']."','".$_POST['sponsor']."','".$_POST['refund']."',
'".$_POST['what_refundable']."','".$_POST['flight_cost']."',
'".$_POST['subsis_cost']."','".$_POST['no_nights']."',
'".$_POST['depart_location']."','".$_POST['depart_time']."',
'".$_POST['out_flight']."','".$_POST['via_location']."','".$_POST['via_arrive']."',
'".$_POST['via_depart']."','".$_POST['arrive_location']."',
'".$_POST['arrive_time']."','".$_POST['arrive_flight']."',
'".$_POST['return_location']."','".$_POST['return_time']."',
'".$_POST['return_flight']."','".$_POST['via_location2']."',
'".$_POST['via_arrive2']."','".$_POST['via_depart2']."',
'".$_POST['depart_time2']."','".$_POST['arrive_time2']."',
'".$_POST['flight_num2']."','".$_POST['uname']."')", $dbh);


$result = mysql_query ("select id, name, email from managers");
while ($row = mysql_fetch_array($result)) {
if ($dir = $name);
echo "$email";
mail ("$email", "test", "content", "test");
}
?>

What I want to do is get the value of "$_POST['dir']" & email the address that corresponds to that variable in my managers table.

Example, if I have bob as a user & bob@bob.com in my managers DB & a user selects the name bob in the form, I want something mailed to bob's email which is bob@bob.com.

Thanks in advance for any replies, all input & suggestions welcome :wink:
User avatar
amp
New php-forum User
New php-forum User
 
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland

fetch_array again...

Postby mammal » Wed Apr 02, 2003 3:41 am

Hi again, the problem was you were referring to the email pulled from the database as $email when it should have been $row[email] as mysql_fetch_array makes an array of the information where the [key] is the name of the db table column. This script should work and also ive change your select function to only pull the details out if it matches with the name submitted on your form.


<?php
include("config.php");
$dbh = mysql_connect ($dbhost, $dbuname, $dbpass) or die ( 'I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname) or die (mysql_error());

$result = mysql_query ("INSERT INTO travel (prn, date, name, dir, purpose, sponsor, refund, what_refundable, flight_cost, subsis_cost, no_nights, depart_location, depart_time, out_flight, via_location, via_arrive, via_depart, arrive_location, arrive_time, arrive_flight, return_location, return_time, return_flight, via_location2, via_arrive2, via_depart2, depart_time2, arrive_time2, flight_num2, uname) values ('".$_POST['prn']."','".$_POST['date']."','".$_POST['name']."','".$_POST['dir']."',
'".$_POST['purpose']."','".$_POST['sponsor']."','".$_POST['refund']."',
'".$_POST['what_refundable']."','".$_POST['flight_cost']."',
'".$_POST['subsis_cost']."','".$_POST['no_nights']."',
'".$_POST['depart_location']."','".$_POST['depart_time']."',
'".$_POST['out_flight']."','".$_POST['via_location']."','".$_POST['via_arrive']."',
'".$_POST['via_depart']."','".$_POST['arrive_location']."',
'".$_POST['arrive_time']."','".$_POST['arrive_flight']."',
'".$_POST['return_location']."','".$_POST['return_time']."',
'".$_POST['return_flight']."','".$_POST['via_location2']."',
'".$_POST['via_arrive2']."','".$_POST['via_depart2']."',
'".$_POST['depart_time2']."','".$_POST['arrive_time2']."',
'".$_POST['flight_num2']."','".$_POST['uname']."')", $dbh);


$result = mysql_query ("select id, name, email from managers WHERE name = '$dir'");
while ($row = mysql_fetch_array($result)) {
echo "$row[email]";
mail ("$row[email]", "test", "content", "test");
}
?>

Let me know if you have any problems...
User avatar
mammal
New php-forum User
New php-forum User
 
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Postby Ihoss » Wed Apr 02, 2003 4:27 am

if you want to make teh email message a little cooler, use the following script instead:
Code: Select all
mail("$row[email]"."<".$row[name]".">",Subject, Messages, "From: ".Your email."<".your name.">");


by the way, what does invalid session mean. i get it sometimes when i try to post a reply here
Ihoss
New php-forum User
New php-forum User
 
Posts: 17
Joined: Sun Mar 30, 2003 11:41 pm

Postby amp » Wed Apr 02, 2003 4:39 am

Mammal, thank you very, very much :mrgreen: :mrgreen: :mrgreen:
I just had a big long reply for you to tell you it wasnt working but when I changed $_POST['name'] to $name it worked :D
Thanks to you things are making alot more sense !

Ihoss, thanks I'll try that. As for your invalid session, could it be that you went to reply to a message, got disconnected from the internet & came back to reply to the message with a different IP address ?? that happened me yesterday.

Cheers
amp2000
User avatar
amp
New php-forum User
New php-forum User
 
Posts: 15
Joined: Tue Apr 01, 2003 3:12 am
Location: Ireland

No problem

Postby mammal » Wed Apr 02, 2003 4:48 am

No worries amp. let me know if you get stuck in future...
User avatar
mammal
New php-forum User
New php-forum User
 
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron