Forum Moderators: coopster
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.
<?
$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.
}
?>
<?
$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]
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.
- 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
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 </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'
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
<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 </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 </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>
<br /><br /><select name="status">
<option value="none">Select unit status </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 />
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
$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();
}
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
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 </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.
$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();
}
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
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