Welcome to WebmasterWorld Guest from 54.226.146.15

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

automatic emails

based on selection from dynamic dropdow menu

     

brancook

3:20 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



I want to send automatic emails based on a selection from a dynamic dropdox menu once it is selected and submitted. The dropdown menu references an id from a table. What would my if statement look like? So if an id is selected a user is notified via email.

d40sithui

4:44 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



the mail() function will take care of all your emailing needs.
assuming you have a table with at least 1 field(email).

say your form is something like this:
<form action="action.php" method="POST">
<select name="addx">
<option value="-1">Select an email </option>
<?
$query = "select email from myTableName";
$result = mysql_query($result);
while($content = mysql_fetch_assoc($result)){
echo "<option value=\"".$content['email']."\">".$content['email']."</option>\n";
}
?>
</select>
<input type="submit" value="Submit">
</form>


this would be action.php:
<?
$to = $_POST['addx'];
$subject = "hello";
$message = "this is an automated email...";
$message = wordwrap($message, 70);

//if valid selection
if($to!= -1){
$ok = mail($to, $subject, $message);
//if mail was successful
if($ok){
echo "Mail sent";
}
else{
echo "Failed to send mail.";
}
}//ends valid selection

ultimately you will most likely need to validate your post data even further to avoid injection. i guess u can do this by comparing the posted email and/or id with the database. if none match, dont send. also, to send to multiple people, i suggest u use checkboxes instead of dropdown.

brancook

4:52 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



my option value is getting pulled from a database. so this is what my option value looks like:

echo'<option value="'.$rma['id'].'"';

so what i want to do is, based on what that option value is(it could be 5 different options) I want to send an email.

brancook

4:56 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



So i guess where I'm having trouble is what:

if($to!= option value from form){

d40sithui

5:04 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



if u want to use id as an option value, you would need another query in your email script to get the email address associated with that id. also, in this way, you can check to see if the id is valid.

<?
$id = $_POST['addx']; //assuming addx is your select name
$query = "select email from myTableName where id=$id";
$result = mysql_query($query);

//found associated email
if(mysql_num_rows($result)==1){
//send email
}
else{
//print error.
}
?>

brancook

5:54 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



Thanks for the advice I got it working.

brancook

10:50 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



I am actually having one problem with this. When I just visit the page or refresh it, it sends an email. So it looks like I'm still missing something.

Habtom

6:57 am on Oct 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more if condition, this is not perfect, but may be it will give you an idea:

<?
$id = $_POST['addx']; //assuming addx is your select name

if ($id <> '' && $_SESSION['id'] <> $id) {
$query = "select email from myTableName where id=$id";
$result = mysql_query($query);

//found associated email
if(mysql_num_rows($result)==1){
//send email
}
else{
//print error.
}
$_SESSION['id'] = $id;
}
?>

[edited by: Habtom at 6:59 am (utc) on Oct. 30, 2007]

brancook

4:01 pm on Oct 30, 2007 (gmt 0)

5+ Year Member



What I'm actually getting know is. When I adjust my if statement:

from: if (mysql_num_rows($result)==1){
to: if (mysql_num_rows($result)=='new rma form'){

'new rma form' is the actual text from the database for that id (1). So I don't seem to be pulling the id, just the text problem is the query is done when the page is loaded so the email gets sent even with out the record being submitted.

brancook

5:10 pm on Oct 30, 2007 (gmt 0)

5+ Year Member



actually I can fill it with pretty much anything and I get an email. So it seems as if nothing from my query is being selected.

jatar_k

5:27 pm on Oct 30, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



do you mean nothing from your select element is being tested?

- show us your exact if statement

- show us the exact value you are testing for

or do you mean that your form isn't being populated with the table data correctly?

- the following makes no sense to change

from: if (mysql_num_rows($result)==1){
to: if (mysql_num_rows($result)=='new rma form'){

the return from mysql_num_rows will never equal to a string because it returns an int

brancook

8:44 pm on Oct 30, 2007 (gmt 0)

5+ Year Member



$to = "myemail@email.com";
$subject = "current email test";
$message = "New RMA FORM FOR";
$id = $_POST['status'];
$query = "select id from status where id='$id'";
$result = mysql_query($query);
//found associated email

if (mysql_num_rows($result)==1){
mail($to, $subject, $message);
}

I want to send an email based on what status is selected from the drop down list. Here is the code for my drop down list:

echo '<select name="status">'."\n";
echo '<option value="none">Select unit status&nbsp;&nbsp;&nbsp;</option>'."\n";
$status_name = mysql_query('SELECT * FROM status ORDER BY id');while ($status = mysql_fetch_array($status_name, MYSQL_ASSOC)){
echo'<option value="'.$status['id'].'"';
if($record_data['ref_status_id']==$status['id']) print " selected=\"selected\" ";
print '>'.$status['status_name']."</option>\n";

}
print '</select>'."<br />\n";

That drop down menu displays status names based on their id numbers from the database.

The table that I'm selecting from in the first if statment is part of a Relational database. So I'm wondering if my $query should look like this:

select status.id from status where status.id = '$id'

brancook

10:06 pm on Oct 30, 2007 (gmt 0)

5+ Year Member



Here is something that may help visualize even more(this isn't code or anything just a visual of my tables and what I need to happen):

main table: dropbox
ref_status_id = status.id

table name: status
id 1: new (if selected send email to user@email.com
id 2: middle aged (if selected send email to usertwo@email.com
id 3: old (if selected send email to user@email.com
id 4: finished (if selected send email to usertwo@email.com

brancook

3:02 am on Oct 31, 2007 (gmt 0)

5+ Year Member



I think I might be wording this wrong. I don't want the email sent based on a selection on want the email sent based on what is inserted into the database.

jatar_k

12:06 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



the way that emailing script is set up it will email the same person every time

can you post the html output (view source on your form) for just the select element

then I can make sure that I give you the right advice

brancook

12:36 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



Thanks, below is the all of the code for my form. Is this what you are looking for?

<form action="<?php echo $_SERVER['PHP_SELF'];?>"
method="post" enctype="multipart/form-data">
<?php
print '<select name="rma">'."\n";
print '<option value="none">Select RMA number&nbsp;&nbsp;&nbsp;</option>'."\n";
//mysql_query currently only selects STA units because of ref_categories_id equaling 2
$rma_number = mysql_query('SELECT * FROM rma WHERE ref_categories_id=2 ORDER BY rma_number');while ($rma = mysql_fetch_array($rma_number, MYSQL_ASSOC)){
print'<option value="'.$rma['id'].'"';
if($record_data['ref_rma_id']==$rma['id']) print " selected=\"selected\" ";
print '>'.$rma['rma_number']."</option>\n";

}
echo '<br /><br />';
echo '<select name="status">'."\n";
echo '<option value="none">Select unit status&nbsp;&nbsp;&nbsp;</option>'."\n";
$status_name = mysql_query('SELECT * FROM status ORDER BY id');
while ($status = mysql_fetch_array($status_name, MYSQL_ASSOC)){
echo'<option value="'.$status['id'].'"';
if($record_data['ref_status_id']==$status['id']) echo " selected=\"selected\" ";
echo '>'.$status['status_name']."</option>\n";

}
echo '</select>'."<br />\n";

?>
<br />
<label>STA or Wand RMA File:
<input type="file" name="upload" /></label><br /><br />
<label>Note:
<input type="text" name="desc" maxlength="255" /></label><br /><br />
<input type="submit" value="Upload" />
</form>

jatar_k

12:46 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



view your form in a browser
view source
copy the output html code for the select we are talking about
paste that here

brancook

1:08 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



Sorry, here is the html output:

<br /><br /><select name="status">
<option value="none">Select unit status&nbsp;&nbsp;&nbsp;</option>

<option value="1">New RMA Form</option>
<option value="2">Estimate</option>
<option value="3">Approved</option>
<option value="4">No Repair</option>
<option value="10">Shipped</option>
</select><br />
<br />

jatar_k

1:17 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



do you really need to do that select before sending?

I would do the insert then verify the insert worked and the email part would look something like

$id = $_POST['']; // you might want to type check all vars before inserting

$to = '';
if ($id = 1 ¦¦ $id = 3) $to = 'user@email.com';
else $to = 'usertwo@email.com';

$subject = "current email test";
$message = "New RMA FORM FOR";
mail($to, $subject, $message);

if there were more options you could use a switch

$to = '';
switch ($id) {
case 1:
$to = 'user@email.com';
break;
case 3:
$to = 'user@email.com';
break;
default:
$to = 'usertwo@email.com';
break;
}

$subject = "current email test";
$message = "New RMA FORM FOR";
mail($to, $subject, $message);

remember to switch ¦ chars with real pipe characters as WebmasterWorld software breaks them

brancook

1:39 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



Would I verify the insert was ok with an if statement? If this is my sql statement for the insert:

$sql = "INSERT INTO dropbox SET filename = '$uploadname', mimetype = '$uploadtype', description = '$uploaddesc', filedata = '$filedata', ref_rma_id = '$rma', ref_status_id = '$status', date = '$timestamp'";

What would my if statement look like? I think that's where I'm a bit confused as well because I do currently have this if statement for the sql statement:

$ok = @mysql_query($sql);
if (!$ok) {
exit('Database error storing file: ' . mysql_error());
} elseif ($ok) {
echo('File uploaded successfully!<br />We have been notified of your upload and you will recieve an estimate soon<br /><a href="www.oursite.com">Back</a>');
}
exit();
}

jatar_k

1:48 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



that would be fine

the other thing you might want to do is put all of the info from the form into the email if the insert doesn't work, then you still have the data

depending on what data you are receiving that might be a bad idea

you could also log the failure to a file somewhere

though what you have is the general idea, test that first and worry about the rest later

brancook

1:56 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



So do I combine that email if statement with the if statement that I currently have? What would that look like? I've been trying to combine those somehow with no success.

Also I tried to put the status and rma info into the body of the email message but I currently only get the id numbers which doesn't do who I'm emailing much good. For instance as you can see by the output here from the form for the rma select:

<select name="rma">
<option value="none">Select RMA number&nbsp;&nbsp;&nbsp;</option>
<option value="98">RMA-2222</option>

When I try to insert 'rma' into the email I get the number 98 and not the name RMA-2222.

jatar_k

2:18 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



to insert RMA-2222 you would have to select that from the db using the id as all that will be sent from the form is the value portion of the selected element

paste the code you are using right now and I can put the if in there

brancook

2:41 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



Here is the if statement that I'm currently using to verify the sql insert:

$ok = @mysql_query($sql);
if (!$ok) {
exit('Database error storing file: ' . mysql_error());
} elseif ($ok) {
echo('File uploaded successfully!<br />We have been notified of your upload and you will recieve an estimate soon<br /><a href="www.oursite.com">Back</a>');
}
exit();
}

jatar_k

2:57 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you could add it right after that, though you need to remove the exit() statements

or, if you only want it to work on success then

$ok = @mysql_query($sql);
if (!$ok) {
exit('Database error storing file: ' . mysql_error());
} elseif ($ok) {
$to = '';
if ($id = 1 $id = 3) $to = 'user@email.com';
else $to = 'usertwo@email.com';

$subject = "current email test";
$message = "New RMA FORM FOR";
mail($to, $subject, $message);

echo('File uploaded successfully!<br />We have been notified of your upload and you will recieve an estimate soon<br /><a href="www.oursite.com">Back</a>');
}
exit();
}

that should work, though I am not sure of the reason for the exit() at the end

brancook

3:08 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



else $to = 'usertwo@email.com';

The else doesn't seem to be working. The emails are just going to the first email user@email.com no matter what selection is made.

jatar_k

3:19 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



oops

would be smarter if I compared them instead of assigning them

f ($id == 1 $id == 3)

brancook

3:29 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



now all selections go to the else email.

jatar_k

3:31 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



have you echo'ed the value of $_POST['status'] to make sure it is correct

do you still have this line in there
$id = $_POST['status'];

and have you changed the ¦ for real unbroken pipe chars

brancook

3:54 pm on Oct 31, 2007 (gmt 0)

5+ Year Member



$id = $_POST['status'];3

oops

yep that's what I forgot. It's working perfect now! thank you thank you thank you!

"to insert RMA-2222 you would have to select that from the db using the id as all that will be sent from the form is the value portion of the selected element"

Is it a simple fix to add the status name instead of the id into the email message? I altered my select statement in the status portion to look like this:

SELECT status.id, status.status_name FROM status ORDER BY id

before I just had

SELECT * FROM status ORDER BY id

This 41 message thread spans 2 pages: 41
 

Featured Threads

Hot Threads This Week

Hot Threads This Month