Jump to content

php form, send email only if all conditions met


azukah

Recommended Posts

hi-

I have this form that works fine: if an image is not the right format or it's too big, it tells the user the error msg and redirects them back to the form BUT it still sends the email... how do i go about sending the email ONLY if the image is the right format & it's within the size limit..???

 

<?php
define ("MAX_SIZE","100"); 
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}
$errors=0;

if(isset($_POST['Submit'])) 
{
	$image=$_FILES['image']['name'];
	if ($image) 
	{
		$filename = stripslashes($_FILES['image']['name']);
  		$extension = getExtension($filename);
		$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{
			$error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>";
		}
		else
		{
$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
$error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>";
$errors=1;
}

$image_name=time().'.'.$extension;
$newname="images_board/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
$error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>";
$errors=1;
}}}}
if(isset($_POST['Submit']) && !$errors) 
{
	$error = "<p>Thank You!</p><p>Your request has been submitted</p>";
}
//for email... ///
$studentName=$_POST['studentName'];
$studentId=$_POST['studentId'];
$studentEmail=$_POST['studentEmail'];
$studentPhone=$_POST['studentPhone'];
$postSubject=$_POST['postSubject'];
$postDescription=$_POST['postDescription'];

$to='[email protected]';
$subject="Bulletin Board Request: $postSubject"; 
$mainbody.="Student Name: $studentName<br/><br/>";
$mainbody.="Student Id: $studentId<br><br/>";
$mainbody.="Contact email: $studentEmail<br/><br/>";
$mainbody.="Contact Phone: $studentPhone<br/><br/>";
$mainbody.="Subject: $postSubject<br/><br/>";
$mainbody.="Description: $postDescription<br/><br/>";
$mainbody.="Image: $newname<br/>";

$headers="MIME-Version: 1.0\r\n";
$headers.="Content-type:text/html;charset=utf-8\r\n"; 
$headers.="From: $studentName";

mail($to,$subject,$mainbody,$headers);
?>

i think i solved it .... i submitted a couple of times with big images and i haven't received any emails..

 

[code=php:0]
<?php
define ("MAX_SIZE","100"); 
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}
$errors=0;

if(isset($_POST['Submit'])) 
{
    $image=$_FILES['image']['name'];
    if ($image) 
    {
       $filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
       $extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
       {
          $errors=1; // added here
          $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>";
       }
       else
       {
$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
   $errors=1; // moved it here
   $error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>";
}

$image_name=time().'.'.$extension;
$newname="images_board/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
   $errors=1; // moved it here
   $error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>";
}}}}
if(isset($_POST['Submit']) && !$errors) 
{
    $error = "<p>Thank You!</p><p>Your request has been submitted</p>";
}
//for email... ///
$studentName=$_POST['studentName'];
$studentId=$_POST['studentId'];
$studentEmail=$_POST['studentEmail'];
$studentPhone=$_POST['studentPhone'];
$postSubject=$_POST['postSubject'];
$postDescription=$_POST['postDescription'];

$to='[email protected]';
$subject="Bulletin Board Request: $postSubject"; 
$mainbody.="Student Name: $studentName<br/><br/>";
$mainbody.="Student Id: $studentId<br><br/>";
$mainbody.="Contact email: $studentEmail<br/><br/>";
$mainbody.="Contact Phone: $studentPhone<br/><br/>";
$mainbody.="Subject: $postSubject<br/><br/>";
$mainbody.="Description: $postDescription<br/><br/>";
$mainbody.="Image: $newname<br/>";

$headers="MIME-Version: 1.0\r\n";
$headers.="Content-type:text/html;charset=utf-8\r\n"; 
$headers.="From: $studentName";

mail($to,$subject,$mainbody,$headers);
?>

[/code]

You don't need the $errors anymore, this should do it:

 

<?php
define ("MAX_SIZE","100"); 
function getExtension($str) 
{
$i = strrpos($str,".");
    if (!$i) 
    { 
return ""; 
     }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

if(isset($_POST['Submit'])) 
{
   $image=$_FILES['image']['name'];

    if($image) 
    {
       	$filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
       	$extension = strtolower($extension);

	if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
       	{
                $error = "<p>Sorry... only jpg, jpeg, gif and png formats are accepted!</p><p><a href='postRequest.php'>Please try again!</a></p>";
       	}
       	else
       	{
		$size=filesize($_FILES['image']['tmp_name']);

	if ($size > MAX_SIZE*1024)
	{
   			$error = "<p>Sorry... You have exceeded the image size limit!</p><p><a href='postRequest.php'>Please try again!</a></p>";
	}
	else
	{
		$image_name=time().'.'.$extension;
		$newname="images_board/".$image_name;
		$copied = copy($_FILES['image']['tmp_name'], $newname);

		if (!$copied) 
		{
   				$error = "<p>Sorry... there has been an upload error.</p><p><a href='postRequest.php'>Please try again!</a></p>";
		}
		else
		{
			$error = "<p>Thank You!</p><p>Your request has been submitted</p>";
			//for email... ///
			$studentName=$_POST['studentName'];
			$studentId=$_POST['studentId'];
			$studentEmail=$_POST['studentEmail'];
			$studentPhone=$_POST['studentPhone'];
			$postSubject=$_POST['postSubject'];
			$postDescription=$_POST['postDescription'];

			$to='[email protected]';
			$subject="Bulletin Board Request: $postSubject"; 
			$mainbody.="Student Name: $studentName<br/><br/>";
			$mainbody.="Student Id: $studentId<br><br/>";
			$mainbody.="Contact email: $studentEmail<br/><br/>";
			$mainbody.="Contact Phone: $studentPhone<br/><br/>";
			$mainbody.="Subject: $postSubject<br/><br/>";
			$mainbody.="Description: $postDescription<br/><br/>";
			$mainbody.="Image: $newname<br/>";

			$headers="MIME-Version: 1.0\r\n";
			$headers.="Content-type:text/html;charset=utf-8\r\n"; 
			$headers.="From: $studentName";

			mail($to,$subject,$mainbody,$headers);
		}
	}
}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.