Jump to content

PHP Contact form display errors


rb3883

Recommended Posts

Hi, i'm a PHP newbie and am working on a contact form which is below. I've just included one field (email) to make things shorter. In practice this works okay but when the page is first displayed it shows the "No Email was entered" error. I know that I need to include something like "if (isset($_POST['submit'])) is true then process the error checking otherwise display the form however I can't seem to find where to put it and spent all weekend on this now, little help? Once i've got this sorted i'll add the other fields and javascript error checking as well. Many thanks Ryan

 

<?php // shortform.php
$email = "";
$to = 'myemail@address.co.uk';
$subject = 'Contact form email';
$message = 'please work!';

if (isset($_POST['email']))
    $email = fix_string($_POST['email']);
    
    $fail = validate_email($email);

    echo "<html><head><title>Shortform</title>";

if ($fail =="") {
    echo "</head><body>$email successfully validated: .</body></html>";
        
    mail($to, $subject, $message, "From: $email");
    
    exit;
}

echo <<<_END
<table class="contactform" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">contactform</th>

<tr><td colspan="2">Sorry, the following errors were found<br />
in your form: <p><font color=red size=1><i>$fail</i></font></p>
</td></tr>
<form method="post" action="shortform.php"
onSubmit="return validate(this)">
<tr><td>Email</td><td><input type="text" maxlength="64"
name="email" value="$email" /></td>
</tr><tr><td colspan="2" align="center">
<input type="submit" value="Submit" /></td>
</tr></form></table>
_END;


function validate_email($field) {
if ($field == "") return "No Email was entered<br />";
	else if (!((strpos($field, ".") > 0) &&
		       (strpos($field, "@") > 0)) ||
				preg_match("/[^a-zA-Z0-9.@_-]/", $field))
	return "The Email address is invalid<br />";
return "";
}

function fix_string($string) {
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?>

17567_.php

Link to comment
Share on other sites

It looks like you've neglected to include braces around the code you wish to be executed conditionally. The syntax is:

 

if(condition){
    //code to execute if the condition is true
    //more code to execute if the condition is true
}

 

Note that if you omit the braces, this is still legal syntax. However, only the single next statement after the if statement will be executed conditionally -- all subsequent code will be execute regardless of the value of the condition. In general, this usually means the next line after the if statement will be conditional. So, for example:

 

if(condition)
    //This line executed conditionally
    //This line executed unconditionally

 

Although it is a matter of personal taste and coding convention, many people (including myself) advise against omitting the braces of an if statement even if you do not need them. In general, the braces make the code clearer.

Link to comment
Share on other sites

Tough love huh?? :)

 

Right, I got rid of that messy table lay out and this appears to work okay, although i'd be extremely grateful if you'd give me your thoughts again? I'll then replicate this for my other 'contact form' fields (Name and Message) although slightly different validation will take place. Thanks a lot for your help, Ryan

 

CODE

<?php // shortform.php

$fail = $email = "";

 

$to = 'myemail@address.co.uk';

$subject = 'Contact form email';

$message = 'please work!';

 

if (isset($_POST['email']))

    $email = fix_string($_POST['email']);

   

    $fail = validate_email($email);

 

    echo "<html><head><title>Shortform</title>";

 

if ($fail =="") {

    echo "</head><body>$email successfully validated: .</body></html>";

       

    mail($to, $subject, $message, "From: $email");

        exit;

    }

 

if ((isset($_POST["Submitted"])) && ($fail !=""))

{     

echo <<<_END

Sorry, the follwoing errors were found<br />

in your form - $fail<br />

_END;

}

 

echo <<<_END

<form method="post" action="shortform2.php"

    onSubmit="return validate(this)">

  Email: <input name='email' type="text" value=$email><br />

  <input type="submit" value="Submit" />

  <input type="hidden" name="Submitted" value="Yes"/>

  </form>

_END;

 

 

function validate_email($email) {

if ($email == "") return "No Email was entered<br />";

else if (!((strpos($email, ".") > 0) &&

      (strpos($email, "@") > 0)) ||

preg_match("/[^a-zA-Z0-9.@_-]/", $email))

return "The Email address is invalid<br />";

return "";

}

 

function fix_string($string) {

if (get_magic_quotes_gpc()) $string = stripslashes($string);

return htmlentities ($string);

}

 

?>

Link to comment
Share on other sites

In general, it looks like you're getting there but you still appear to be missing the braces around the code to be executed only when the form has been submitted. You want to structure your code along the lines of:

 

if(form submitted){
    //validate form
    if(validated){
        //Try emailing
        if(email sent ok){
            //Tell user it was successful
        }else{
            //Tell user there was a problem, try again later, alternative contact method etc
        }
    }else{
        //Tell user there's a problem with their input
    }
}
//Display form

 

Indenting your code properly will help to identify the flow of control you want.

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.