View Full Version : xmlHttpReq and a PHP mailform
Xeophex
October 16th, 2008, 06:43 AM
I wasn't sure weather to post this in the server-side or client-side forum. I guess it concerns both.
I'm using ALAX to load content dynamically on a website (xmlHttpReq). It's working fine.
My problem is when I try to use a php mail form with it. I've tried a few mailforms and my knowledge of php is little to none. All of them don't work when loaded in by the Ajax (but would have worked otherwise), more simple ones might have worked but the main issue is that when it shows a confirmation message afterwards, e.g. "thank you, your messages has been sent", it doesn't load dynamically within the div like all the other content, it changes the whole page.
So I tried onClick="javascript:AJAX('mailer.php')" on the submit button, still not working, maybe I'm not doing that bit right?
Basically I'm looking for a way to get a php mailform to work while using xmlHttpReq.
The only other alternative I considered is to make an iframe and have the mailform on a totally independent page, displayed in the iframe. But I really would prefer to sort it without using an iframe.
Any help would be greatly appreciated. Many thanks. :thumb:
actionAction
October 18th, 2008, 12:29 AM
Can you post how what your AJAX function id doing in the onReadyStateChange function? That's most likely where your error is coming from. Where is the mail form? Is it in your Flash program? More likely you wouldn't be injecting any content back into your Flash page (or the current page) other than a success statement. I think what you need to do is alter the url and how the mail form works. It's probably going to be something along the lines of (and this will need alteration to your implementation):
onClick="javascript:AJAX('mailer.php?sender="+sender.text+"&email="+email.text+"&message="+message.text+"')"
and your mailer.php file is going to use the $_GET superglobal to access the values inputted like this:
$name = $_GET['sender'];
$email = $_GET['email'];
$message = $_GET['message'];
$body =<<< HEREDOC
$name sent you this message:<br />
$message<br />
You can respond to them at $email.
HEREDOC;
$recipient = 'me@myaddress.com';
mail($recipient, "contact form", $body);
Hope this clarifies a bit for you.
Xeophex
October 18th, 2008, 10:35 AM
Thanks for the reply actionAction. I think I do get it! But judging form my past experience, I don't have enough knowledge of php or javascript to get it all working ok. haha
Only the nav menu (also like a header) will be flash. I'll pm you a url to what I have so far.
Each page loads dynamically from the ajax.
Here is the ajax:
function AJAX(strURL) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(strURL);
}
function updatepage(str){
//This part injects the content into the current document without reload
document.getElementById("content").innerHTML = str;
}
Then in the html I have:
<div id="content">...for the content to inject in to. And
<a href="javascript:AJAX('profile.php')"...would be for a text link, and then the flash would do the same, to switch pages.
I don't know how tidy the ajax is but it's all working fine!
In the html form input "submit" button I tried
onClick="javascript:AJAX('incs/mailer.php')" for example, But it didn't work, just loaded it full page rather than in the content div.
This is the php mailform I have:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP mail form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Robots" content="noindex, nofollow" />
<link href="css/form_style.css" rel="stylesheet" type="text/css" />
<link href="css/common_style.css" rel="stylesheet" type="text/css" />
<!--[if !IE]>--><link href="css/not_ie.css" rel="stylesheet" type="text/css" /><!--<![endif]-->
<!--[if IE ]><link href="css/ie_specific.css" rel="stylesheet" type="text/css" /><![endif]-->
<link rel="shortcut icon" type="image/ico" href="images/favicon.ico" />
</head>
<body>
<div id="mailform">
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
$exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javas cript|alert)/i";
$profanity = "/(beastial|bestial|blowjob|clit|cum|cunilingus|cuni llingus|cunnilingus|****|ejaculate|***|felatio|fel latio|****|fuk|fuks|gangbang|gangbanged|gangbangs| hotsex|jism|jiz|orgasim|orgasims|orgasm|orgasms|ph onesex|phuk|phuq|****|*******|*****|spunk|xxx)/i";
$spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazol am|ambien|ambian|amoxicillin|antivert|blackjack|ba ckgammon|texas|holdem|poker|carisoprodol|ciara|cip rofloxacin|debt|dating|****)/i";
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";
if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
exit("<p>Known spam bots are not allowed.</p>");
}
foreach ($_POST as $key => $val) {
$c[$key] = clean($val);
if (preg_match($exploits, $val)) {
exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
} elseif (preg_match($profanity, $val) || preg_match($spamwords, $val)) {
exit("<p>That kind of language is not allowed.</p>");
}
}
$show_form = true;
$error_msg = NULL;
if (isset($c['submit'])) {
if (empty($c['name']) || empty($c['email']) || empty($c['comments'])) {
$error_msg .= "Name, e-mail and comments are required fields. \n";
} elseif (strlen($c['name']) > 15) {
$error_msg .= "The name field is limited at 15 characters. Your first name or nickname will do! \n";
} elseif (!ereg("^[A-Za-z' -]*$", $c['name'])) {
$error_msg .= "The name field must not contain special characters. \n";
} elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",strtolower($c['email']))) {
$error_msg .= "That is not a valid e-mail address. \n";
}
if ($error_msg == NULL) {
$show_form = false;
if (!empty($c['url']) && !ereg("^(http|https)", $c['url'])) {
$c['url'] = "http://" . $c['url'];
}
$subject = "Automatic Form Email";
$message = "You received this e-mail message through your website: \n\n";
foreach ($c as $key => $val) {
$message .= ucwords($key) . ": $val \n";
}
$message .= "IP: {$_SERVER['REMOTE_ADDR']} \n";
$message .= "Browser: {$_SERVER['HTTP_USER_AGENT']}";
if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
$headers = "From: ralph@ralphmanning.com \n";
$headers .= "Reply-To: {$c['email']}";
} else {
$headers = "From: ralphmanning.com <ralph@ralphmanning.com> \n";
$headers .= "Reply-To: {$c['email']}";
}
$recipient = "ralph@ralphmanning.com";
if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Your mail was successfully sent.</p>";
} else {
echo "<p>Your mail could not be sent this time.</p>";
}
}
}
if (!isset($c['submit']) || $show_form == true) {
function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];
}
}
if ($error_msg != NULL) {
echo "<p><strong style='color: red;'>ERROR: </strong>";
echo nl2br($error_msg) . "</p>";
}
?>
<form name="mail form" action="mailform.php" method="post">
<table>
<tr>
<td>Name:</td>
<td><div class="inputWrapper"><input name="name" type="text" class="field" id="name" value="<?php get_data("name"); ?>" /></div></td>
</tr>
<tr>
<td>E-mail:</td>
<td><div class="inputWrapper"><input name="email" type="text" class="field" id="email" value="<?php get_data("email"); ?>" /></div></td>
</tr>
<tr>
<td>Website:</td>
<td><div class="inputWrapper"><input name="url" type="text" class="field" id="url" value="http://<?php get_data("url"); ?>" /></div></td>
</tr>
<tr>
<td>Location:</td>
<td><div class="inputWrapper"><input name="location" type="text" class="field" id="location" value="<?php get_data("location"); ?>" /></div></td>
</tr>
<tr>
<td>Comments:</td>
<td><textarea name="comments" class="field" id="comments"><?php get_data("comments"); ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit" class="send" id="submit" value="Send" onfocus="blur()"/> <input name="reset" type="reset" class="login" id="reset" value="Reset" onfocus="blur()"/></td>
</tr>
</table>
</form>
<?php
}
?>
</div>
</body>
</html>
I've got it working currently by just having that as a separate page and making an Iframe in the page that gets loaded in by the ajax. Not ideal, but still works. Hopefully what you suggested can fix it, so no need for iframe! :D
Also, I added the "reset" button to clear the form, but it's behaviour is not quite as i hoped. It clears it up to when a visitor hits send. After that it only resets back to the text they put when they submitted it. And the error message that appears above the form stays- ideally I'd love for the reset button to just refresh the form back to how it started as for the visitor. Is that possible?
As I mentioned, I'll pm a url to a temporary upload.
Thank you so much. I owe you a pint, or 4! :)
Kind regards,
actionAction
October 19th, 2008, 12:22 AM
You could use a little more php and js to finish it off. How about this:
if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Your mail was successfully sent.</p>";
sleep(3);//waits three seconds before moving to the next line (time to read)
echo '<script type="text/javascript">location.href = "index.html";//redirects to index.html</script>';
....rest of your code
for your reset button, you could utilize something similar like:
onclick="javascript:location.reload();"
Xeophex
October 19th, 2008, 09:40 AM
:doh: I think I put that bit of php in the right place within the rest of it. But it doesn't work exactly as I hoped. It seems to delay the specified number (of seconds?) before displaying the message. So the message flashes for under a second after the pause and it seems to just take longer to load while it's waiting out the number of seconds. This is how it is now:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP mail form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Robots" content="noindex, nofollow" />
<link href="css/form_style.css" rel="stylesheet" type="text/css" />
<link href="css/common_style.css" rel="stylesheet" type="text/css" />
<!--[if !IE]>--><link href="css/not_ie.css" rel="stylesheet" type="text/css" /><!--<![endif]-->
<!--[if IE ]><link href="css/ie_specific.css" rel="stylesheet" type="text/css" /><![endif]-->
<link rel="shortcut icon" type="image/ico" href="images/favicon.ico" />
</head>
<body>
<div id="mailform">
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
$exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javas cript|alert)/i";
$profanity = "/(beastial|bestial|blowjob|clit|cum|cunilingus|cuni llingus|cunnilingus|****|ejaculate|***|felatio|fel latio|****|fuk|fuks|gangbang|gangbanged|gangbangs| hotsex|jism|jiz|orgasim|orgasims|orgasm|orgasms|ph onesex|phuk|phuq|****|*******|*****|spunk|xxx)/i";
$spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazol am|ambien|ambian|amoxicillin|antivert|blackjack|ba ckgammon|texas|holdem|poker|carisoprodol|ciara|cip rofloxacin|debt|dating|****)/i";
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";
if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
exit("<p>Known spam bots are not allowed.</p>");
}
foreach ($_POST as $key => $val) {
$c[$key] = clean($val);
if (preg_match($exploits, $val)) {
exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
} elseif (preg_match($profanity, $val) || preg_match($spamwords, $val)) {
exit("<p>That kind of language is not allowed.</p>");
}
}
$show_form = true;
$error_msg = NULL;
if (isset($c['submit'])) {
if (empty($c['name']) || empty($c['email']) || empty($c['comments'])) {
$error_msg .= "Name, e-mail and comments are required fields. \n";
} elseif (strlen($c['name']) > 15) {
$error_msg .= "The name field is limited at 15 characters. Your first name or nickname will do! \n";
} elseif (!ereg("^[A-Za-z' -]*$", $c['name'])) {
$error_msg .= "The name field must not contain special characters. \n";
} elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",strtolower($c['email']))) {
$error_msg .= "That is not a valid e-mail address. \n";
}
if ($error_msg == NULL) {
$show_form = false;
if (!empty($c['url']) && !ereg("^(http|https)", $c['url'])) {
$c['url'] = "http://" . $c['url'];
}
$subject = "Automatic Form Email";
$message = "You received this e-mail message through your website: \n\n";
foreach ($c as $key => $val) {
$message .= ucwords($key) . ": $val \n";
}
$message .= "IP: {$_SERVER['REMOTE_ADDR']} \n";
$message .= "Browser: {$_SERVER['HTTP_USER_AGENT']}";
if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
$headers = "From: ralph@ralphmanning.com \n";
$headers .= "Reply-To: {$c['email']}";
} else {
$headers = "From: ralphmanning.com <ralph@ralphmanning.com> \n";
$headers .= "Reply-To: {$c['email']}";
}
$recipient = "ralph@ralphmanning.com";
if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Your mail was successfully sent.</p>";
sleep(5);//waits three seconds before moving to the next line (time to read)
echo '<script type="text/javascript">location.href = "mailform.php";//redirects to mailform.php</script>';
} else {
echo "<p>Your mail could not be sent this time.</p>";
}
}
}
if (!isset($c['submit']) || $show_form == true) {
function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];
}
}
if ($error_msg != NULL) {
echo "<p><strong style='color: red;'>ERROR: </strong>";
echo nl2br($error_msg) . "</p>";
}
?>
<form name="mail form" action="mailform.php" method="post">
<table>
<tr>
<td>Name:</td>
<td><div class="inputWrapper"><input name="name" type="text" class="field" id="name" value="<?php get_data("name"); ?>" /></div></td>
</tr>
<tr>
<td>E-mail:</td>
<td><div class="inputWrapper"><input name="email" type="text" class="field" id="email" value="<?php get_data("email"); ?>" /></div></td>
</tr>
<tr>
<td>Website:</td>
<td><div class="inputWrapper"><input name="url" type="text" class="field" id="url" value="http://<?php get_data("url"); ?>" /></div></td>
</tr>
<tr>
<td>Location:</td>
<td><div class="inputWrapper"><input name="location" type="text" class="field" id="location" value="<?php get_data("location"); ?>" /></div></td>
</tr>
<tr>
<td>Comments:</td>
<td><textarea name="comments" class="field" id="comments"><?php get_data("comments"); ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit" class="send" id="submit" value="Send" onfocus="blur()"/> <input name="reset" type="submit" class="login" id="reset" value="Reset" onclick="javascript:location.reload();" onfocus="blur()"/></td>
</tr>
</table>
</form>
<?php
}
?>
</div>
</body>
</html>
Also, that onclick="javascript:location.reload();" reloads the page, great. But the fields don't clear in the process. I had to change the input type to "submit" for it to work at all. Am I missing something. Did I still need it to be type="reset"?
<input name="reset" type="submit" class="login" id="reset" value="Reset" onclick="javascript:location.reload();" onfocus="blur()"/>
If you need me to re-upload it for you to see it in action, let me know, I can do.
actionAction
October 19th, 2008, 12:48 PM
Hmmm...
It's most likely replacing the page because you are loading in an entire page. The only content that needs to be there is this (don't copy paste this, just take what you have and delete everything except the div tag and its contents):
<div id="mailform">
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
$exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javas cript|alert)/i";
$profanity = "/(beastial|bestial|blowjob|clit|cum|cunilingus|cuni llingus|cunnilingus|****|ejaculate|***|felatio|fel latio|****|fuk|fuks|gangbang|gangbanged|gangbangs| hotsex|jism|jiz|orgasim|orgasims|orgasm|orgasms|ph onesex|phuk|phuq|****|*******|*****|spunk|xxx)/i";
$spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazol am|ambien|ambian|amoxicillin|antivert|blackjack|ba ckgammon|texas|holdem|poker|carisoprodol|ciara|cip rofloxacin|debt|dating|****)/i";
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";
if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
exit("<p>Known spam bots are not allowed.</p>");
}
foreach ($_POST as $key => $val) {
$c[$key] = clean($val);
if (preg_match($exploits, $val)) {
exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
} elseif (preg_match($profanity, $val) || preg_match($spamwords, $val)) {
exit("<p>That kind of language is not allowed.</p>");
}
}
$show_form = true;
$error_msg = NULL;
if (isset($c['submit'])) {
if (empty($c['name']) || empty($c['email']) || empty($c['comments'])) {
$error_msg .= "Name, e-mail and comments are required fields. \n";
} elseif (strlen($c['name']) > 15) {
$error_msg .= "The name field is limited at 15 characters. Your first name or nickname will do! \n";
} elseif (!ereg("^[A-Za-z' -]*$", $c['name'])) {
$error_msg .= "The name field must not contain special characters. \n";
} elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",strtolower($c['email']))) {
$error_msg .= "That is not a valid e-mail address. \n";
}
if ($error_msg == NULL) {
$show_form = false;
if (!empty($c['url']) && !ereg("^(http|https)", $c['url'])) {
$c['url'] = "http://" . $c['url'];
}
$subject = "Automatic Form Email";
$message = "You received this e-mail message through your website: \n\n";
foreach ($c as $key => $val) {
$message .= ucwords($key) . ": $val \n";
}
$message .= "IP: {$_SERVER['REMOTE_ADDR']} \n";
$message .= "Browser: {$_SERVER['HTTP_USER_AGENT']}";
if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
$headers = "From: ralph@ralphmanning.com \n";
$headers .= "Reply-To: {$c['email']}";
} else {
$headers = "From: ralphmanning.com <ralph@ralphmanning.com> \n";
$headers .= "Reply-To: {$c['email']}";
}
$recipient = "ralph@ralphmanning.com";
if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Your mail was successfully sent.</p>";
} else {
echo "<p>Your mail could not be sent this time.</p>";
}
}
}
if (!isset($c['submit']) || $show_form == true) {
function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];
}
}
if ($error_msg != NULL) {
echo "<p><strong style='color: red;'>ERROR: </strong>";
echo nl2br($error_msg) . "</p>";
}
?>
<form name="mail form" action="mailform.php" method="post">
<table>
<tr>
<td>Name:</td>
<td><div class="inputWrapper"><input name="name" type="text" class="field" id="name" value="<?php get_data("name"); ?>" /></div></td>
</tr>
<tr>
<td>E-mail:</td>
<td><div class="inputWrapper"><input name="email" type="text" class="field" id="email" value="<?php get_data("email"); ?>" /></div></td>
</tr>
<tr>
<td>Website:</td>
<td><div class="inputWrapper"><input name="url" type="text" class="field" id="url" value="http://<?php get_data("url"); ?>" /></div></td>
</tr>
<tr>
<td>Location:</td>
<td><div class="inputWrapper"><input name="location" type="text" class="field" id="location" value="<?php get_data("location"); ?>" /></div></td>
</tr>
<tr>
<td>Comments:</td>
<td><textarea name="comments" class="field" id="comments"><?php get_data("comments"); ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit" class="send" id="submit" value="Send" onfocus="blur()"/> <input name="reset" type="reset" class="login" id="reset" value="Reset" onfocus="blur()"/></td>
</tr>
</table>
</form>
<?php
}
?>
</div>
As for the waiting problem, don't really know! Try this instead:
echo "<p>Your mail was successfully sent.</p>";
echo '<script type="text/javascript">url="mailform.php";waitSeconds=5;setTimeout("location.href=url",(waitSeconds*1000));</script>';
Try this to clear your input elements:
<script type="text/javascript">
function clearAll()
{
var inputs= document.getElementsByTagName('input');
for(i = 0; i < inputs.length; i++)
{
if(inputs[i].type == 'text')
{
inputs[i].value = "";
}
}
}
</script>
<input type="button" onclick="javascript:clearAll();" value="reset" />
Xeophex
October 20th, 2008, 11:38 AM
We'll get there eventually.
Ok, the waiting problem is sorted! Well it works with how I'm currently doing it (with an iframe). Great.
The clear all worked for clearing the submit fields perfectly, but doesn't clear the textarea. I had a fiddled with it to no success, but it's probably not too tricky to amend the script to clear the textarea also? So that's almost sorted. Thanks :D:D:D
At the least now, I could have it working fine in the iframe.
BUT as you know, what I really want to achieve is it loading like the rest of the content, by ajax, in the div.
Ofcourse, before I resorted in doing it the iframe way, i didn't have all that html head stuff, just the div with the php in, like you said in prior post. However that was tried right at the start and it didn't work. It loaded full page when a mail was sent. I had the html head and body tags in it later since it was a separate page (and needed css styling) to be shown by using an iframe (my last resort workaround).
To recap, I've tried onClick="javascript:AJAX('mailform.php')" in the submit button (with only the php in a div, not the head and body tags etc), but sadly it didn't work. But I haven't tried the suggestion in the second post to this thread (your first post) about using the $_GET superglobal! It went a bit over my head. If you think this could be the answer. Do you think you could explain a little more "step-by-step" in relation to my current php script, like you have done in later posts.
Would the form tag and action attribute have anthing to do with how it loads, (in div by ajax or fullpage)?:
<form name="mail form" action="mailform.php" method="post">
Again, many thanks
actionAction
October 20th, 2008, 04:00 PM
Yes, the action has everything to do with what the form does. First off, the name shouldn't have any whitespace, change it to mail_form for good measure. Second, now I totally understand what you are doing and why it isn't working. When you use AJAX('mailform.php'), it is doing two things: your onclick method (which is probably so fast it's not noticeable, and then it is moving the POST data to your action, which is mailform.php. Here is the fixed javascript to clear the textarea, I will post a solution to your submit problems a little later (no time now)
<script type="text/javascript">
function clearAll()
{
var inputs= document.getElementsByTagName('input');
document.getElementsByTagName('textarea').value = "";
for(i = 0; i < inputs.length; i++)
{
if(inputs[i].type == 'text')
{
inputs[i].value = "";
}
}
}
</script>
Xeophex
October 21st, 2008, 08:16 AM
Great, thanks! I'm glad there is a solution.
The amended js to clear the fields still doesn't clear the textarea. ? hmm :-/
actionAction
October 28th, 2008, 02:11 PM
Great, thanks! I'm glad there is a solution.
The amended js to clear the fields still doesn't clear the textarea. ? hmm :-/
Okay, change your form header to be this:
<form name="contactForm" action="" method="get" onsubmit="process(this)">
Add this script somewhere in your page:
<script type="text/javascript">
function process(obj)
{
var name = obj.name.value;
var email = obj.email.value;
var url = obj.url.value;
var loc = obj.location.value;
var comments = obj.comments.value;
AJAX("incs/mailer.php?name="+name+"&email="+email+"&url="+url+"&loc="+loc+"comments="+comments);
}
</script>
And change your incs/mailer.php script to this:
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
$exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javas cript|alert)/i";
$profanity = "/(beastial|bestial|blowjob|clit|cum|cunilingus|cuni llingus|cunnilingus|****|ejaculate|***|felatio|fel latio|****|fuk|fuks|gangbang|gangbanged|gangbangs| hotsex|jism|jiz|orgasim|orgasims|orgasm|orgasms|ph onesex|phuk|phuq|****|*******|*****|spunk|xxx)/i";
$spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazol am|ambien|ambian|amoxicillin|antivert|blackjack|ba ckgammon|texas|holdem|poker|carisoprodol|ciara|cip rofloxacin|debt|dating|****)/i";
$bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";
if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
exit("<p>Known spam bots are not allowed.</p>");
}
foreach ($_GET as $key => $val) {
$c[$key] = clean($val);
if (preg_match($exploits, $val)) {
exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
} elseif (preg_match($profanity, $val) || preg_match($spamwords, $val)) {
exit("<p>That kind of language is not allowed.</p>");
}
}
$show_form = true;
$error_msg = NULL;
if (isset($c['submit'])) {
if (empty($c['name']) || empty($c['email']) || empty($c['comments'])) {
$error_msg .= "Name, e-mail and comments are required fields. \n";
} elseif (strlen($c['name']) > 15) {
$error_msg .= "The name field is limited at 15 characters. Your first name or nickname will do! \n";
} elseif (!ereg("^[A-Za-z' -]*$", $c['name'])) {
$error_msg .= "The name field must not contain special characters. \n";
} elseif (!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$",strtolower($c['email']))) {
$error_msg .= "That is not a valid e-mail address. \n";
}
if ($error_msg == NULL) {
$show_form = false;
if (!empty($c['url']) && !ereg("^(http|https)", $c['url'])) {
$c['url'] = "http://" . $c['url'];
}
$subject = "Automatic Form Email";
$message = "You received this e-mail message through your website: \n\n";
foreach ($c as $key => $val) {
$message .= ucwords($key) . ": $val \n";
}
$message .= "IP: {$_SERVER['REMOTE_ADDR']} \n";
$message .= "Browser: {$_SERVER['HTTP_USER_AGENT']}";
if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
$headers = "From: ralph@ralphmanning.com \n";
$headers .= "Reply-To: {$c['email']}";
} else {
$headers = "From: ralphmanning.com <ralph@ralphmanning.com> \n";
$headers .= "Reply-To: {$c['email']}";
}
$recipient = "ralph@ralphmanning.com";
if (mail($recipient,$subject,$message,$headers)) {
echo "<p>Your mail was successfully sent.</p>";
} else {
echo "<p>Your mail could not be sent this time.</p>";
}
}
}
if (!isset($c['submit']) || $show_form == true) {
function get_data($var) {
global $c;
if (isset($c[$var])) {
echo $c[$var];
}
}
if ($error_msg != NULL) {
echo "<p><strong style='color: red;'>ERROR: </strong>";
echo nl2br($error_msg) . "</p>";
}
?>
Basically, I change your form processor to $_GET instead of $_POST. The Javascript function sends an encoded URL to be processed by mailer.php. When you have a form action, the browser will load the action page when the form is submitted. This should instead use AJAX and a querystring to send the message. I haven't tested any of this, but in theory it should work.
Oh, and as for the textarea, change this line:
document.getElementsByTagName('textarea').value = "";
to this:
document.getElementsByTagName('textarea')[0].value = "";
Xeophex
October 29th, 2008, 05:10 PM
Well the clear all fields bit is working perfectly. :D
But the other bit didn't work. My form isn't incs/mailer.php ...it was mailform.php which was inside an iframe in contact.php (which was loaded with the ajax).
For this, I got rid of contact.php changed mailform.php's name to contact.php and removed all the header and body tags so it was just the new php you gave and my form inside a div.
I changed the location of the file in the js from incs/mailer.php to contact.php - but didn't work! it just reloads to the index. were there any other things where i needed to change incs/mailer.php to contact.php
Also does
<input name="submit" type="submit" class="send" id="submit" value="Send" onfocus="blur()"/> stay as it is, for this to work.
I feel like it's so nearly there.
I could zip the whole thing and give it to you if you think it would help?
Thanks for your continued help.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.