PDA

View Full Version : Help change compose function



oriecat
10-21-2008, 04:06 AM
Hello. I hope I am in the right forum, I apologize if I should be somewhere else...

I am using this code that someone on here helped with in another thread.


function compose(){
var addys='', cc='?cc=', boxes = document.getElementById('boxes').getElementsByTagName('input')
for (var i=0; i<boxes.length; i++)
if (boxes[i].checked&&boxes[i].name=='mail')
addys+=boxes[i].value+', ';
else if (boxes[i].checked&&boxes[i].name=='cc')
cc+=boxes[i].value+', ';
addys=addys.replace(/, $/, '');
cc=cc!='?cc='? cc.replace(/, $/, '') : '';
var mailwin=window.open('mailto:'+addys+cc);
mailwin.close();

This reviews a database generated list of checkboxes, then inserts the checked rows into an email window. What I would like to do now is change it so that it will insert the email list into the $to field of a php mail function. Is that possible?

Jesdisciple
10-21-2008, 05:59 PM
Absolutely, but this is untested...
function getAjax(){
if(XMLHttpRequest)
// Firefox, Opera 8.0+, Safari, Internet Explorer 7+
return new XMLHttpRequest();
else if(ActiveXObject){
// Internet Explorer 6-
var types = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for (var i = 0; i < types.length; i++)
try{
return new ActiveXObject(types[i]);
}catch(e){}
}
return null;
}
function compose(){
var addys='to=', cc='?cc=', boxes = document.getElementById('boxes').getElementsByTagName('input')
for (var i=0; i<boxes.length; i++)
if (boxes[i].checked&&boxes[i].name=='mail')
addys+=boxes[i].value+', ';
else if (boxes[i].checked&&boxes[i].name=='cc')
cc+=boxes[i].value+', ';
addys=addys.replace(/, $/, '');
cc=cc!='?cc='? cc.replace(/, $/, '') : '';
var ajax = getAjax();
if(ajax){
ajax.open('POST', 'mail.php', true);
ajax.onreadystatechange = function(){
if(ajax.readyState === 4 && ajax.status === 200)
//The page has loaded successfully; check the output to make sure the mail was sent.
};
ajax.send(addys + cc);
}else
//The user's browser is not performing AJAX.
}

oriecat
10-25-2008, 07:40 PM
Thank you, I will try this out. :)

oriecat
10-26-2008, 01:15 AM
Ok, so something in here is causing my database selections not to work now.

Here is all of my code, in case I need to change some other stuff...


<script type="text/javascript">

/*Local Javascript Bulk eMailer script
*As first seen in http://www.dynamicdrive.com/forums
*This notice must remain for legal use. */

//Set number of columns for display:
var columns=1

////////////// Stop Editing ///////////////////

function checkall(mcc, check) {
var boxes = document.getElementById('boxes').getElementsByTagName('input')
for (var i=0; i<boxes.length; i++)
if (check=='t'){
if (mcc=='m'&&boxes[i].name=='mail')
boxes[i].checked=boxes[i].checked? false : true;
else if (mcc=='c'&&boxes[i].name=='cc')
boxes[i].checked=boxes[i].checked? false : true;
else if (mcc=='t')
boxes[i].checked=boxes[i].checked? false : true;
}
else if (check){
if (mcc=='m'&&boxes[i].name=='mail')
boxes[i].checked=true;
else if (mcc=='c'&&boxes[i].name=='cc')
boxes[i].checked=true;
}
else{
if (mcc=='m'&&boxes[i].name=='mail')
boxes[i].checked=false;
else if (mcc=='c'&&boxes[i].name=='cc')
boxes[i].checked=false;
}
}

function getAjax(){
if(XMLHttpRequest)
// Firefox, Opera 8.0+, Safari, Internet Explorer 7+
return new XMLHttpRequest();
else if(ActiveXObject){
// Internet Explorer 6-
var types = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for (var i = 0; i < types.length; i++)
try{
return new ActiveXObject(types[i]);
}catch(e){}
}
return null;
}
function compose(){
var addys='to=', cc='?cc=', boxes = document.getElementById('boxes').getElementsByTagName('input')
for (var i=0; i<boxes.length; i++)
if (boxes[i].checked&&boxes[i].name=='mail')
addys+=boxes[i].value+', ';
else if (boxes[i].checked&&boxes[i].name=='cc')
cc+=boxes[i].value+', ';
addys=addys.replace(/, $/, '');
cc=cc!='?cc='? cc.replace(/, $/, '') : '';
var ajax = getAjax();
if(ajax){
ajax.open('POST', 'sendmail.php', true);
ajax.onreadystatechange = function(){
if(ajax.readyState === 4 && ajax.status === 200)
//The page has loaded successfully; check the output to make sure the mail was sent.
};
ajax.send(addys + cc);
}else
//The user's browser is not performing AJAX.
}
</script>
</head>
<body>


<div id="content">

<H2>Group Email Listings</H2>

<H3>1. Select Email Group</H3>

Select Branch and/or Mail Group to search for...<P>
<table class="search"><tr>
<td>
<FORM METHOD="get" ACTION="emailtest.php">

<INPUT TYPE=hidden NAME=choice VALUE="request">
<form name="form1" method="get" action="emailtest.php">
Select a Branch:
<select name="search" size=1>
<option value="">All Branches</option>
<option value="03">03 - Portland</option>
<option value="04">04 - Seattle</option>
<option value="05">05 - CRE</option>
<option value="07">07 - Eugene</option>
<option value="08">08 - Salem</option>
<option value="09">09 - Reedsport</option>
<option value="10">10 - Everett</option>
<option value="11">11 - Vancouver</option>
<option value="14">14 - Olympia</option>
<option value="16">16 - Tacoma</option>
<option value="20">20 - Corporate</option>
<option value="21">21 - Longview</option>
<option value="22">22 - CCS</option>
<option value="24">24 - ERAC</option>
</select>
</td>
</tr>
<tr>
<td>
Select a Mail Group:
<select name="search2" size=1>
<option value="">All Groups</option>
<option value="A">A - Corporate Administration</option>
<option value="B">B - Managers</option>
<option value="C">C - Warehouse, Cleaning Techs, Delivery Drivers</option>
<option value="D">D - Repair/Rehab Shop, Techs & Coordinators</option>
<option value="E">E - Respiratory Therapists & Support Staff</option>
<option value="F">F - A/R, Billing, Collections</option>
<option value="G">G - ReRents/Rental Authorization</option>
<option value="H">H - Medical Records/Validation</option>
<option value="I">I - File Records</option>
<option value="J">J - Rehab Sales & Assistants</option>
<option value="K">K - Customer Service</option>
<option value="L">L - Lift</option>
<option value="M">M - Accounting</option>
<option value="N">N - Purchasing</option>
<option value="O">O - Selections</option>
<option value="P">P - CCS & ERAC</option>
<option value="Q">Q - Cash Application/Posting</option>
<option value="R">R - Inventory Task Force</option>
<option value="S">S - Consignment</option>
<option value="T">T - Marketing</option>
<option value="U">U - Termination Emails</option>
<option value="V">V - Managed Care Contracts</option>
</select>
</td></tr>
</table>
<INPUT TYPE="submit" VALUE="Search!" class="btn">
</FORM>

<H3>2. Search results</H3>
<?php
mysql_connect("dbhost","dbuser","dbpw");
mysql_select_db("db");
$choice = $_REQUEST["choice"];
if ($choice == NULL)
{
echo "Please choose one of the above options!";
};
if ($choice == "all")
{
echo "This is a full listing of the email database<p>";
$result = mysql_query("SELECT * FROM evolinks ORDER BY Name");
};
if ($choice == "request") {
$search = $_REQUEST["search"];
$search2 = $_REQUEST["search2"];

echo "All Selected Emails";

$result = mysql_query("SELECT * FROM evolinks
WHERE (Branch LIKE \"%$search%\" OR Branch = 'ALL')
AND (Mailgroup LIKE \"%$search2%\")
ORDER BY Name");

echo "<script type='text/javascript'>";
echo "var students=[];";


while ($myrow = @mysql_fetch_array($result)) {
echo "students[students.length]=['",$myrow["Name"]," - ",$myrow["Title"]," (",$myrow["Branch"],")', '",$myrow["email"],"'];";
}
echo "</script>";

}


?>
<form method="post" action="emailtest.php">
<input type="button" value="Send Email" class="btn" onclick="compose();">
</form>
<p />
<table id="boxes">
<tr>
<!--<td>Mail</td><td></td>-->

<div id="controls">
<input type="button" value="Check all" class="btn2" onclick="checkall('m',1);"> <input type="button" value="Uncheck all" class="btn2" onclick="checkall('m');">
</div>

<script type="text/javascript">


for (var i_tem = 1; i_tem < columns; i_tem++)
document.write('</tr><tr>');
for (i_tem = 0; i_tem < students.length; i_tem++){
document.write('<td><input name="mail" type="checkbox" value="'+students[i_tem][1]+'" checked /></td><td class="sname">'+students[i_tem][0]+'</td>\n');
if(columns==0||columns==1||(i_tem!=0&&[i_tem+1]%columns==0))
document.write('</tr><tr>');
}
</script>

</table>

<form method="post" action="sendmail.php">
To: <input name="to" type="text"><br>
From Name: <input name="name" type="text"><br>
From Email: <input name="email" type="text"><br>
Subject: <input name="subject" type="text"><br>
Your Message:<br>
<textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit">
</form>

<FORM METHOD="LINK" ACTION="emailtest.php">
<INPUT TYPE="submit" VALUE="Start Over" class="btn" />
</FORM>

And here's my sendmail.php page:

<?php
$to = $_REQUEST['to'] ;
$subject = $_REQUEST['subject'] ;
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$message = $_REQUEST['message'] ;

$headers = "From: $name <$email>\r\n";

mail( $to, $subject, $message, $headers );
header( "Location: http://url/emailtest.php" );
?>

Thanks for any other thoughts on what I can do here. :)

Jesdisciple
10-26-2008, 01:27 AM
I don't see any database queries to break... Also, you need to add code where I left these comments:
//The page has loaded successfully; check the output to make sure

//The user's browser is not performing AJAX.I don't know what the code needs to be, and that's mostly why I didn't put it in. But I also want you to learn by adding it.