Forum Moderators: open

Message Too Old, No Replies

sending form results to random email addy in array

sending form results to random addy

         

tootseywootsey

12:03 am on Jan 11, 2007 (gmt 0)

10+ Year Member



so i have my form, and my array of email addresses, and now i need to make the validated results of the form get sent to a random address from the array. gee it sounds so simple, but all i've ever written was a mailto generator and a couple other super simple scripts. where do i start
laura

mehh

7:00 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



mail cant be sent from a javascript file. however useing AJAX you can communicate with a severside script eg PHP and send the mail that way.
if useing PHP it may look something like this:
mail.php

<?php
$email=array();
$email[0]="email1@whatever.com";
$email[1]="email2@whatever.com";
$email[2]="email3@whatever.com";
function spamcheck($field)
{
if(eregi("to:",$field) ¦¦ eregi("cc:",$field))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==TRUE)
{
echo "Error";
}
else
{
//send email
$email = $email[rand(0, count($email))];
$subject = $_REQUEST['sub'] ;
$message = $_REQUEST['mes'] ;
mail($email, "Subject: $subject",
$message, "From: $email" );
echo "Success";
}
}
else
{
echo "Error";
}
?>

form.html

<html>
<body>

<script type="text/javascript">
function sendMail(message,subject)
{
var xmlHttp;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Error. Cannot send email");
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.responseText=="Success")
{
alert("Email Sent!")
}
}
}
xmlHttp.open("GET","mail.php?mes="+message+"&sub="+subject,true);
xmlHttp.send(null);
}
</script>

<form onsubmit="sendMail(document.getElementById('mes').value,document.getElementById('sub').value)">
Subject: <input id="sub">
Message: <textarea id="mes">
</form>

</body>
</html>

tootseywootsey

3:53 am on Jan 28, 2007 (gmt 0)

10+ Year Member



Thanks, I will look into the AJAX info you sent - I really need to get better at using it. I seem to learn most when I follow an example, then recreate. But I made my form in PHP and it seems to work well. I made an array of email addresses, then had a random number selected between the total number of addresses in the array, then sent that to the recipient varible.