Form Email and Field Validation in PHP (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


Mav44 -> Form Email and Field Validation in PHP (3/2/2008 15:48:12)

Grettings,

I am still working on getting a form to send an email with the email address verified and the name and phone number required to be included on the form.

Below is the code I am currently working on and I like the javascript "back" feature. I think I may need to combine the two scripts for this to work and I know the "echo's" are a problem. My goal has been to have the data verified and then passed to the email. I have tried putting chunks of these two together for two days and no luck. Any suggestions are greatly appreciated!

-----------------------------------------------------------------------------

<?php
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($name == "") {
echo "<h4>No Name</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($phone_number == "") {
echo "<h4>No Phone_Number</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
?>
<?php
require 'class.phpmailer.php';
if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$_FILES['userfile']['name'])){
('file upload error');
}

$mail = new PHPMailer();
$mail->From = 'me@mysite.com';
$mail->AddAddress("me@mysite.com","ABC");
$mail->AddAttachment($_FILES['userfile']['name']);
$mail->AddAttachment($_FILES['Filename2']['name']);
$mail->AddAttachment($_FILES['Filename3']['name']);
$mail->Body ='Name: '.$_POST['name']."\n";
$mail->Body .='Company: '.$_POST['Company']."\n";
$mail->Body .='Phone: '.$_POST['Phone_Number']."\n";
$mail->Body .='Fax: '.$_POST['Fax_Number']."\n";
$mail->Body .='Email: '.$_POST['Email']."\n";
$mail->Body .='Details: '.$_POST['Additional_Details']."\n";
$mail->Body .='Type: '.$_POST['File_Type']."\n";
$mail->Body .='App: '.$_POST['App_Type']."\n";
$mail->Subject = "My Order / Quote Request";

if(!$mail->Send())
{

header("Location: http://www.mysite.com/error.htm");
"Mailer Error: " . $mail->ErrorInfo;
exit;
}


header("Location: http://www.mysite.com/thankyou.htm");

?>




ou812 -> RE: Form Email and Field Validation in PHP (3/3/2008 18:50:21)

Not a direct solution to what you have listed, but why not just use javascript on the form to check for errors, prior to even getting to the submission so they don't need to go back and forth?

If you do want to go the route you are, maybe you could do the checking and then automatically redirect to the original page (location header) and pass back what error it was. Then display the error on that form. Again though, your jumping pages when maybe you shouldn't?




Mav44 -> RE: Form Email and Field Validation in PHP (3/4/2008 16:44:39)

Brian,

I went the javascript route and all validation and email of form is working well except my form no longer sends the file attachements. It think it may be how I am passing the info to the javascript and then to the php.

<form name='form1' action='control.php' method='post' onsubmit='return formValidator()'>


What am I missing or done wrong?




ou812 -> RE: Form Email and Field Validation in PHP (3/4/2008 18:11:20)

Hmm,

It should work fine. You should be doing whatever validation on your form, then submit and process the email and attachment on the called php page. The javascript on the calling page should not have anything to do with it. Can you post both pages? Maybe the attach names got renamed or something?




Mav44 -> RE: Form Email and Field Validation in PHP (3/4/2008 20:45:07)

Here goes!

My Form Page

--------------------------------------

<html>

<head>
<TITLE>My Site</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1251">
<meta http-equiv="Content-Language" content="en-us">
<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var name = document.getElementById('name');
var Phone_Number = document.getElementById('Phone_Number');
var Email = document.getElementById('Email');

// Check each input in the order that it appears in the form!
if(madeSelection(name, "Please enter a valid name")){
if(madeSelection(Phone_Number, "Please enter a valid phone number")){
if(emailValidator(Email, "Please enter a valid email address")){
return true;
}
}
}

return false;

}

function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return true;
}
return false;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
</head>

<body bgcolor="#000000">

<div align="center">
<table border="0" width="800" cellspacing="0" cellpadding="0">
<tr>
<td width="800" bgcolor="#C31325">
<font size="1" color="#C31325">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="11" align="left" valign="top">
<img border="0" src="images/tlban.jpg" width="11" height="11"></td>
<td align="center"><b>
<font color="#FFDD00" face="Arial">
<a href="index.html"><font color="#FFDD00">HOME</font></a></font></b></td>
<td align="center">
<font color="#C31325">
<b><font face="Arial">
<a href="printing.htm"><font color="#FFDD00">PRINTING</font></a></font></b></td>
<td align="center"><b><font face="Arial">
<a href="signs.htm"><font color="#FFDD00">SIGNS</font></a></font></b></td>
<td align="center"><b><font face="Arial">
<a href="copies.htm"><font color="#FFDD00">COPIES</font></a></font></b></td>
<td align="center"><b>
<font face="Arial" color="#FFDD00">
<a href="banners.htm"><font color="#FFDD00">BANNERS</font></a></font></b></td>
<td align="center"><b><font face="Arial">
<a href="contact.htm"><font color="#FFDD00">CONTACT</font></a></font></b></td>
<td align="center"><font face="Arial"><b>
<a href="specials.htm"><font color="#FFDD00">SPECIALS</font></a></b></font></td>
<td align="center"><b><font face="Arial">
<a href="links.htm"><font color="#FFDD00">LINKS</font></a></font></b></td>
<td width="11" align="right" valign="top">
<img border="0" src="images/trban.jpg" width="11" height="11"></td>
</tr>
</table>
</td>
<tr>
<td bgcolor="#FFFFFF">
<form name='form1' action='control.php' method='post' onsubmit='return formValidator()'>
<p style="margin-bottom: 0">
<img border="0" src="images/maing1.jpg" width="800" height="171"></p>
<div align="center">
<table border="0" width="90%" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="left">
<p style="margin-top: 0"> </td>
</tr>
<tr>
<td><b><font size="5" color="#C31325"><i>REQUEST FOR
QUOTATION</i></font></b><p><font size="5"><b> By
Phone at 951.735.6100</b></font></p>
<p><font size="5"><b> By Fax at 951.808.8700<br>
</b></font></p>
</td>
<td>
<img border="0" src="images/horsemain.jpg" width="300" height="134"><br>
</td>
</tr>
</table>
</div>
<div align="center">
<table border="0" width="790" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" style="border-left-style: solid; border-left-width: 2px; border-right-style: solid; border-right-width: 2px; border-top-style: solid; border-top-width: 2px">
<blockquote>
<font size="5"><b>Online: <u>Place Order & Upload
File Now</u> <br>
</b></font>Thanks to the convenience of our online file transfer, there's no need to make special trips to our
store just to deliver a disk. You can easily send a file
from your computer to ours in a matter of minutes. 
</blockquote>
</td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> Your Name   </td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input name="name" type='text' size="40" id='name'><font color="#C31325">*
<font size="2" face="Arial">(*required field)</font></font></td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> Company:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input type="text" name="Company" size="40"></td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> Phone Number</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input name="Phone_Number" type="text" size="40 "id='Phone_Number'><font color="#C31325">*</font></td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> Fax Number</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input type="text" name="Fax_Number" size="40"></td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> Email</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input name="Email" type='text' size="40" id='Email'><font color="#C31325">*</font></td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> Additional Details:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<p style="margin-bottom: 0">Please provide information about the file you are sending.
<br>
Include what application you used to create the file.</p>
<p style="margin-top: 0">
<textarea rows="5" name="Additional_Details" cols="55"></textarea></td>
</tr>
<tr>
<td style="border-left-style: solid; border-left-width: 2px" width="130"> File Type:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<font size="3">
<select size="1" name="File_Type"><option value="PC">PC</option>

<option value="Macintosh">Macintosh</option>

</select></font> </td>
</tr>
<tr>
<td nowrap="nowrap" style="border-left-style: solid; border-left-width: 2px" width="130"> Application Type:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<select size="1" name="App_Type">
<option value="Not Selected">Please Select</option>
<option value="Multiple (Compressed)">
Multiple (Compressed)</option>

<option value="Acrobat (PDF)">Acrobat (PDF)</option>

<option value="CorelDRAW">CorelDRAW</option>

<option value="Excel">Excel</option>

<option value="Fonts">Fonts</option>

<option value="Freehand">Freehand</option>

<option value="Illustrator">Illustrator</option>

<option value="InDesign">InDesign</option>

<option value="Pagemaker">Pagemaker</option>

<option value="Photoshop">Photoshop</option>

<option value="PowerPoint">PowerPoint</option>

<option value="Publisher">Publisher</option>

<option value="Word">Word</option>

<option value="QuarkXPress">QuarkXPress</option>

<option value="Other">Other</option>

</select>
</td>
</tr>
<tr>
<td nowrap="nowrap" colspan="2" align="center" style="border-left-style: solid; border-left-width: 2px; border-right-style: solid; border-right-width: 2px">
<hr width="90%"></td>
</tr>
<tr>
<td colspan="2" style="border-left-style: solid; border-left-width: 2px; border-right-style: solid; border-right-width: 2px">
<blockquote>
<p align="left" style="margin-bottom: 0">Click on the "Browse" button below to
select a file(s) on your computer. After you've selected
the file, click the "Submit" button below to transfer your file to
us. Please use WinZip or other file compression software
to compress the file before you send it.
</blockquote>
</td>
</tr>
<tr>
<td nowrap="nowrap" style="border-left-style: solid; border-left-width: 1px" width="131"> Filename:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input type="hidden" name="MAX_FILE_SIZE" value="9000000" />
<input type="file" name="userfile" size="50" value="90000000" /></td>
</tr>
<tr>
<td nowrap="nowrap" style="border-left-style: solid; border-left-width: 1px" width="131"> File 2:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input type="file" name="Filename2" size="50"></td>
</tr>
<tr>
<td nowrap="nowrap" style="border-left-style: solid; border-left-width: 1px" width="131"> File 3:</td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
<input type="file" name="Filename3" size="50"></td>
</tr>
<tr>
<td nowrap="nowrap" style="border-left-style: solid; border-left-width: 1px" width="131"> </td>
<td style="border-right-style: solid; border-right-width: 2px" width="656">
 </td>
</tr>
<tr>
<td colspan="2" bgcolor="#FFDD00" style="border-left-style: solid; border-left-width: 2px; border-right-style: solid; border-right-width: 2px; border-bottom-style: solid; border-bottom-width: 2px">
<p align="center"><font size="3">
<input type="submit" value='Submit' style="font-family: Times New Roman; font-size: 12pt; font-weight: bold"></font><input type="reset" value="Reset" name="B2" style="font-family: Times New Roman; font-size: 12pt; font-weight: bold"></td>
</tr>
</table>
</div>
</form>
</td>
</tr>
<tr>
<td>
<p align="center"><font size="1" color="#FFFFFF">My Site
© 2008 • All Rights Reserved • </font></td>
</tr>
</table>
</div>

</body>

</html>



---------------------------------------My control.php code

<?php
require 'class.phpmailer.php';
if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$_FILES['userfile']['name'])){
('file upload error');
}

$mail = new PHPMailer();
$mail->From = 'me@mysite.com';
$mail->AddAddress("me@mysite.com","ABC");
$mail->AddAttachment($_FILES['userfile']['name']);
$mail->AddAttachment($_FILES['Filename2']['name']);
$mail->AddAttachment($_FILES['Filename3']['name']);
$mail->Body ='Name: '.$_POST['name']."\n";
$mail->Body .='Company: '.$_POST['Company']."\n";
$mail->Body .='Phone: '.$_POST['Phone_Number']."\n";
$mail->Body .='Fax: '.$_POST['Fax_Number']."\n";
$mail->Body .='Email: '.$_POST['Email']."\n";
$mail->Body .='Details: '.$_POST['Additional_Details']."\n";
$mail->Body .='Type: '.$_POST['File_Type']."\n";
$mail->Body .='App: '.$_POST['App_Type']."\n";
$mail->Subject = "My Order / Quote Request";

if(!$mail->Send())
{

header("Location: http://www.mysite.com/error.htm");
"Mailer Error: " . $mail->ErrorInfo;
exit;
}


header("Location: http://www.mysite.com/thankyou.htm");

?>




ou812 -> RE: Form Email and Field Validation in PHP (3/4/2008 22:29:21)

I've not done uploading with php, but it looks like you need to move all of your uploads, and then you can attach those to your email (or just use the temporary names to attach). You do this with the username, but should do this with all of them.

Recall:
$_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
$_FILES["file"]["name"] - the name of the uploaded file

if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$_FILES['userfile']['name'])){
   ('userfile upload error');
} 
if(!move_uploaded_file($_FILES['filename2']['tmp_name'],$_FILES['filename2']['name'])){
   ('filename2 upload error');
} 
if(!move_uploaded_file($_FILES['filename3']['tmp_name'],$_FILES['filename3']['name'])){
   ('filename3 upload error');
} 


Or, you could bypass the checking (probably not recommended) and moving and just attached the tmp_names.

Also, if someone doesn't upload all 3 files, you may need to do checking so you are not attempting to verify, and then attach, no file. I'm not sure if the code will work if nothing is there for one of the uploaded variables.

Does that help?




Mav44 -> RE: Form Email and Field Validation in PHP (3/5/2008 0:37:39)

Brian,

I couldn't figure out why it worked this morning and after the form validator it did not. I compared my earlier code and on the form I had enctype="multipart/form-data" . Once I added this in all is working and I am done!!!!!!!!!!!!!!!

Thanks for all your help![:)]




ou812 -> RE: Form Email and Field Validation in PHP (3/5/2008 1:11:08)

Excellent! Glad you got it all going.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.1098633