Jump to content

php form script wont run error code


kimikai

Recommended Posts

Been working on this form and it does send a mail to me which is good...but the problem is, it always sends a mail to me.

I can't seem to get a working check in it where it checks for required fields etc.

This is what I have so far, so I'm wondering what I'm doing wrong...could someone send me in the right direction?

 

This is the PHP part:

<?php
    if(isset($_POST['submit']))
    {
		$error = "";
		
		if(!empty($_POST['naam'])) {
			$naam = $_POST['naam'];
		} else {
			$error .= "Vul uw naam in";
		}
		
    	//The form has been submitted, prep a nice thank you message
    	$output = '<center><b><u>Uw Kampioenschapscertificaat is verzonden</u></b></center>';
    	//Set the form flag to no display (cheap way!)
    	$flags = 'style="display:none;"';

    	//Deal with the email
    	$to = 'nabben.daisy@live.nl';
    	$subject = 'Kampioenschapscertificaat';

    	$message = 'Naam: ' .$naam ."\n";
		$message .= 'Naam Hond: ' .$naamhond ."\n";
    	$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
    	$filename = $_FILES['file']['name'];

    	$boundary =md5(date('r', time())); 

    	$headers = "From: pp-vn@website.nl\r\nReply-To: pp-vn@website.nl";
    	$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

    	$message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

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

This is the HTML of the form:

<body>

<?php echo $output; ?>
<div id="container">
	<div id="form">
		<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
		<p>
		  <label for="naam">Naam</label>
		  <input type="text" name="naam" id="naam">
		</p>
		<p>Uw Naam + Naam Hond:
		<textarea name="naamhond" id="naamhond" cols="80" rows="8"></textarea>
		</p>
		<label for="file">File</label><input type="file" name="file" id="file"></p>
          <center><input type="submit" name="submit" id="submit" value="Verzend Kampioenscertificaat"></center>
		</form>
	</div>
</div>
</body>

Anyone has an idea on how to fix this?

Link to comment
Share on other sites

Here your code is validating the naam field

		if(!empty($_POST['naam'])) {
			$naam = $_POST['naam'];
		} else {
			$error .= "Vul uw naam in";
		}

When the field has an empty value, you are concatenating an error message to $error. But you do not do anything with that variable afterwards.

 

To prevent the email from sending if an error is defined you should check to make sure that $error is empty before sending the email, eg

// code to validate form fields, any field that fails validation add an error to $error

// now check to make sure no errors where set
if(empty($error))
{
   // code to send email
}
else
{
   // display errors
}
Edited by Ch0cu3r
Link to comment
Share on other sites

Not quite understanding that, but this is what I ment

<?php
    if(isset($_POST['submit']))
    {
        $error = "";

        // code to validate form fields, any field that fails validation add an error to $error
        if(!empty($_POST['naam'])) {
            $naam = $_POST['naam'];
        } else {
            $error .= "Vul uw naam in";
        }

        // now check to make sure no errors where set
        if(empty($error))
        {
            // code to send email
            //The form has been submitted, prep a nice thank you message
            $output = '<center><b><u>Uw Kampioenschapscertificaat is verzonden</u></b></center>';
            //Set the form flag to no display (cheap way!)
            $flags = 'style="display:none;"';

            //Deal with the email
            $to = 'nabben.daisy@live.nl';
            $subject = 'Kampioenschapscertificaat';

            $message = 'Naam: ' .$naam ."\n";
            $message .= 'Naam Hond: ' .$naamhond ."\n";
            $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
            $filename = $_FILES['file']['name'];

            $boundary =md5(date('r', time())); 

            $headers = "From: pp-vn@website.nl\r\nReply-To: pp-vn@website.nl";
            $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

            $message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

            mail($to, $subject, $message, $headers);
        }
        else
        {
            // display errors
            echo '<p>You have not filled in all fields:<br />$error<p>';
        }
    }
?>
Edited by Ch0cu3r
Link to comment
Share on other sites

btw one more question...
The fields that are required ill have to set them seperate to get the error codes for each one

but now for the not required fields im typing this everytime:

if(!empty($_POST['nameofthefield'])){
      $nameofthefield = $_POST['nameofthefield'];
}

would there be a way to say like "if there is information in any of these unrequired fields, then send the info thats within them"?

so basically if(!empty -- string of all the unrequired fields-)) then send everything thats within those fields?

Link to comment
Share on other sites

So you don't want nonrequired fields that have an empty value to be included in your email?

 

Rather than storing each field in a separate variable. Build up the email message as you validate the fields eg

$message = '';

if(!empty($_POST['naam'])) {
    $message .= 'Naam: '.$_POST['naam'] . "\n";  // add naam to email message
} else {
     $error .= "Vul uw naam in";
}

if(!empty($_POST['otherfield']))
    $message .= 'Other field: ' . $_POST['otherfield'] . "\n"; // add other field to email message, if it has a value
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.