Jump to content

PHP email form error and help is needed


oldNsad

Recommended Posts

My questions are as follows:

 

++++ This line is flagged as an error. Why?? (it’s exactly the same as line 8 rows above, which is NOT flagged)

Error message is:

PHP Parse error: syntax error, unexpected '}' in /mailit.php on line 44

 

PHP Parse error: syntax error, unexpected '}' in /mailit.php on line 44

 

 

+++++ How can I make this pattern validate a specific number that was input?

++++++ I want this line to validate_form_email, how do I do it?

+++++++ I want this to take user to thanks.html page that will also echo info sent.

Also, I would like to trim input of special chars., where can I do this in the function?

 

Thanks in Advance to anyone that took the time to try and help. I tried to figure it out for one entire week, so please understand that I want to learn it, not just ask someone else to do it for me. But for the life of me I can't get this. Where did I go wrong? (BTW: I successfully tested server with simple email test).

 

 

 

 

 

 

<?php

 

if (isset($_POST['submitted'])) {

//to load var

$_POST = "name";

$_POST = "email";

$_POST = "about";

$_POST = "nospam";

$_POST = "textarea";

 

$errors = array();

 

$name = $_POST['name'];

$email = $_POST['email'];

$about = $_POST['about'];

$nospam = $_POST['nospam'];

$textarea = $_POST['textarea'];

 

//validation

if (!empty($_POST['name'])) {

$name = $_POST['name'];

} else { $errors[] = 'Please enter your Name.';}

if (preg_match($pattern,$name)){ $name = $_POST['name'];}

$pattern = "/^[a-zA-Z0-9_]{2,20}/";

} else {$errors[] = 'Your Name can only contain letters and numbers.';}

 

 

if (!empty($_POST['email'])) {

$email = $_POST['email'];

} else {$errors[] = 'Please enter your email address.';}

if (preg_match($pattern,$email)){ $email = $_POST['email'];}

$pattern = "/^[a-zA-Z0-9_]{2,20}/";

} else {$errors[] = 'Your Email must be Valid.';}

 

 

if (!empty($_POST['about'])) {

$about = $_POST['about'];

} else {$errors[] = 'Please enter a Subject here.';}

if (preg_match($pattern,$about)){ $about = $_POST['about'];}

$pattern = "/^[a-zA-Z0-9_]{2,20}/";

} else {$errors[] = 'What is this contact about?';}

 

 

if (!empty($_POST['nospam'])) {

$nospam = $_POST['nospam'];

} else {$errors[] = 'Please answer the Anti-Spam Question';}

$pattern = ('/^[a-zA-Z0-9_]{2,20}/');

if (preg_match($pattern,$nospam)){ $nospam = $_POST['nospam'];}

} else {$errors[] = 'Hmmm. Can you try this again?';}

 

 

if (!empty($_POST['textarea'])) {

$textarea = $_POST['textarea'];

} else {$errors[] = 'Please enter your Message.';}

if (preg_match($pattern,$textarea)){ $textarea = $_POST['textarea'];}

$pattern = "/^[a-zA-Z0-9_]{2,20}/";

} else {$errors[] = 'Your Message can only contain regular letters and numbers.';}

 

//compile and send email

if (isset($_POST['submitted'])) {

if (empty($errors)) {

$to = "info@ anything.com";

$from = "info@anything.com";

$subject = "Comment from anything Website";

$message = "Message from" . $_POST['name'] . "rnrn";

"Email Address " . $_POST['email'] . "rnrn";

"About " . $_POST['about'] . "rnrn";

"Text " . $_POST['textarea'] . "";

}

}

 

mail($to, $from, $subject, $message);

//verify submission and show errors

if (isset($_POST['submitted'])) {

 

if (!empty($errors)) {

echo '<hr /><h3>The following occurred:</h3><ul>';

foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}

echo '</ul><h3>Your mail could not be sent due to input errors.</h3><hr />';}

else {

echo '<hr /><h3 align="center">Your mail was sent. Thank you!</h3><hr /><p>Below is the message that you sent.</p>';

echo "Message from" . $name . " " . $email . " " . $subject. " " . $textarea . "";

}

}

exit:('Redirecting you to http://www.anything.com/swg/thankyou.html');

Link to comment
Share on other sites

The indenting doesn't make the code very easy to read, the error will probably be more obvious like this:

              if (preg_match($pattern,$email)) {                 $email = $_POST['email'];              }              $pattern = "/^[a-zA-Z0-9_]{2,20}/";              }                           else {                $errors[] = 'Your Email must be Valid.';              }
The logic in that code doesn't make a lot of sense though. You run preg_match with a variable called $pattern that you haven't defined yet, and if it matches then you set $email to $_POST['email'] (which is what it was already set to, so why set it again?). It looks like you're setting $pattern in the wrong place.Your regular expression pattern doesn't make much sense for validation, either. You use the same pattern for every variable, and the only thing you're testing for is really that the value starts with any 2 letters, numbers, or an underscore, regardless of what comes after that.

+++++ How can I make this pattern validate a specific number that was input?

Regular expressions are for validating patterns. If you're checking for a specific value you don't need to use a regular expression, just check for that value.

++++++ I want this line to validate_form_email, how do I do it?

You shouldn't use a regular expression to validate an email address, you should use filter_var with the FILTER_VALIDATE_EMAIL flag (see example 1):http://php.net/manual/en/function.filter-var.php

+++++++ I want this to take user to thanks.html page that will also echo info sent.

If you want to show a message on the page and redirect after a certain amount of time, you can either use an HTML meta refresh tag, or output some Javascript code to do a redirect after a certain amount of time.

Also, I would like to trim input of special chars., where can I do this in the function?

You could do it wherever you like before you need to use those values. You could use something like preg_replace to replace everything that does or does not match a certain pattern, or you could use filter_var with one of the sanitize or filter flags.
Link to comment
Share on other sites

Man, thank you so much for being patient and responding to each point. I do know that I have things wrong, I tend to get a bit confused. The tuts can show how to do the same thing 5 different ways, and it looks like I do a bit of each :( But to get it right,

 

I do know that the patters are identical, I just used that for ease of testing. I will change them.

 

Is the pattern not defined correctly? (not withstanding the simple val part)(is it in the right place and can it have different values for each place it shows in function?)

 

I do see the error in the simple anti-spam validation.

 

Can you suggest a simple query for JS to get the same email values and echo to empty table in thankyou.htm?

Link to comment
Share on other sites

Is the pattern not defined correctly?

The major problem is that you define the variable between the if and the else, which breaks the else, and there's an extra closing bracket there (after you define the variable). It should be defined before the if statement that uses it.

Can you suggest a simple query for JS to get the same email values and echo to empty table in thankyou.htm?

I'm not sure what you mean.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...