PHP Validation error

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2811 Views - Last Post: 21 September 2011 - 10:21 PM Rate Topic: -----

#1 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

PHP Validation error

Posted 16 September 2011 - 07:20 PM

Not too if I left something out or didnt add something, but I get this error:

Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\xampp\htdocs\xampp\email.php on line 13


<?php
 
// Define some constants
$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];
 
// Read the form values
$success = false;
$first_name = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$email_address = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$message = isset( $_POST['text'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['text'] ) : "";
 
// If all values exist, send the email
mail("myemailaddress@gmail.com","Subject: $subject",
$message, "From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";


?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact</title>
<link href="style.css" rel="stylesheet" type="text/css" />


</head>
<body>

<div id="templatemo_wrapper"> 

	<div id="templatemo_header">

 
        
        <ul id="social_box">
                    <li><a href="http://www.facebook.com/"><img src="images/facebook.png" alt="facebook" /></a></li>
            <li><a href="http://www.twitter.com/"><img src="images/twitter.png" alt="twitter" /></a></li>              
        </ul>
        
      	<div id="site_title">
            <h1><a href="default.htm"><img src="images/logo2.png" alt="logo" /><span></span></a></h1>
        </div> <!-- end of site_title -->
        
      
    </div> <!-- end of templatemo_header -->
    


<!-- end of templatemo_menu -->
    
    <div id="templatemo_content_wrapper">
    	<div id="templatemo_content_top"></div>
        <div id="templatemo_content">
        
            <h2>Contact</h2>


        
            <div class="cleaner_h50"></div>
            
            	<div id="contact_form">
            
                    <h4>Quick Contact</h4>
			<p align="left">* Required fields.</p>
                    
                    <form method="post" name="ContactForm" id="contact" action="email.php" ">
                        
                        <div class="col_w340 float_l">
                        
                            <label for="author">* Name:</label> <input name="author" type="text" class="input_field" id="author" maxlength="40" />
                          	<div class="cleaner_h10"></div>

 <label for="subject">* Subject:</label> <input name="subject" type="text" class="input_field" id="subject" maxlength="40" />
                          	<div class="cleaner_h10"></div>

                            
                            <label for="email">* Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
                          	<div class="cleaner_h10"></div>
                            
                                          

</div>
                        


                        <div class="col_w340 float_r">
                        
                            <label for="text">* Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
                            <div class="cleaner_h10"></div>
                            
                            <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
                            <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />

                            
                        </div>
                        
                   </form>

            </div> 

            <div class="cleaner"></div>
            
        </div>
        <div id="templatemo_content_bottom"></div>
	</div>
    
    <div id="templatemo_sp_box">
    
    	<div class="col_w340 float_l">
         
			</div>
        </div>
        
        <div class="col_w340 float_r">
        
			</div>
        </div>
    
    </div>
    
    <div id="templatemo_footer">

      
        Copyright  2011 <a href="www.twitter.com/">Starr</a><br/> 
        <a href="http://www.iwebsitetemplate.com" target="_parent">Website Templates</a> 
        by <a href="http://www.templatemo.com" target="_parent">Free CSS Templates</a>
    
    </div> <!-- end of templatemo_footer -->
    
</div> <!-- end of templatemo_wrapper -->    

</body>
</html>





Is This A Good Question/Topic? 0
  • +

Replies To: PHP Validation error

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: PHP Validation error

Posted 16 September 2011 - 08:31 PM

You're missing a ' after subject:
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";



Also, lines 4 - 7 are a waste of processor cycles.
Was This Post Helpful? 1
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: PHP Validation error

Posted 17 September 2011 - 07:49 AM

Apart from the syntax error CTphpnwb pointed out above you should run your input through a function instead of manually sanitizing each variable. It makes your script a bit more organized and allows you to quickly make changes from one centralized location.

From the looks of it you're trying to prevent the user from injecting headers. You only really need to get rid of \r and \n along with their encoded equivalents. Have a look here.

This post has been edited by codeprada: 17 September 2011 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 17 September 2011 - 04:59 PM

So far am I on the right track? The example you provided, was new to me cause my knowledge of php is very little.



<?php
 
 
// Read the form values
$success = false;
$first_name = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$email_address = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$message = isset( $_POST['text'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['text'] ) : "";

 
// If all values exist, send the email
mail("myemailaddress@gmail.com","Subject: $subject",
$message, "From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";

function sanitize(&$array)
{
	foreach($array as &$data)
	{
		$data = str_ireplace(array("\r", "\n", "%0a", "%0d"), '', stripslashes($data));
	}
}


?>




Was This Post Helpful? 0
  • +
  • -

#5 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 17 September 2011 - 06:56 PM

I went by another example and this is similar to what I am looking for. Everything is working, but I cant seem to process the form. When I try and process the form it asks for a valid email address.



<?php
if (empty($_POST['author']))
{
$errors[] = 'Please enter a name';
}
if (empty($_POST['subject']))
{
$errors[] = 'Please enter a subject';
}

if (empty($_POST['text']))
{
$errors[] = 'Please enter your comments.';
}

if (empty($_POST['email']))
{
$errors[] = 'Please enter an e-mail';
}
else if (!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$ ^", 

$_POST['email']))
{
$errors[] = 'Please enter a valid e-mail address';
}
if (count($errors) == 0)
{
// Process form
mail("myemailaddress@gmail.com","Subject: $subject",
$message,"From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";
}
else
{
echo $errors[0];
}

?>



Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: PHP Validation error

Posted 17 September 2011 - 07:26 PM

Repetitive code is bad because it is longer than necessary and more prone to human error. It's also harder to read so editing later is harder than it should be. Always look for a way to shorten it with loops and arrays or other data structures. Oh, and filter_var is pretty good at validating email addresses.

$expected_posts = array("author", "subject", "text");
$errors = array();
foreach($expected_posts as $key) {
	if(empty($_POST[$key]) {
		$errors[] = "Please enter a ".$key;
	}
}
if(empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$errors[] = "Please enter a valid email address.";
}

Was This Post Helpful? 0
  • +
  • -

#7 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 17 September 2011 - 07:50 PM

View PostCTphpnwb, on 17 September 2011 - 08:26 PM, said:

Repetitive code is bad because it is longer than necessary and more prone to human error. It's also harder to read so editing later is harder than it should be. Always look for a way to shorten it with loops and arrays or other data structures. Oh, and filter_var is pretty good at validating email addresses.

$expected_posts = array("author", "subject", "text");
$errors = array();
foreach($expected_posts as $key) {
	if(empty($_POST[$key]) {
		$errors[] = "Please enter a ".$key;
	}
}
if(empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$errors[] = "Please enter a valid email address.";
}


Now when I run my test I get the following error: Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\xampp\email.php on line 6
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: PHP Validation error

Posted 17 September 2011 - 08:05 PM

Oops. I left out a parenthesis.
	if(empty($_POST[$key])) {


Was This Post Helpful? 0
  • +
  • -

#9 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 17 September 2011 - 08:45 PM

Just to make sure that I am doing this right, the errors worked, but I think now that something may be wrong with my else statement. When I run it now, I get the following: Notice: Undefined variable: errors in C:\xampp\htdocs\xampp\email.php on line 25






$expected_posts = array("author", "subject", "text");
$errors = array();
foreach($expected_posts as $key) {
	if(empty($_POST[$key])) {
		$errors[] = "Please enter a ".$key;
	}
}
if(empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$errors[] = "Please enter a valid email address.";
}

else
{
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['text'] ;
  mail("someone@example.com", "$subject",
  $message, "From:" . $email);
  echo "Thank you for using our mail form";
  }





This post has been edited by swim_fan08: 17 September 2011 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: PHP Validation error

Posted 18 September 2011 - 04:33 AM

Since you're only showing 24 lines it's hard to say what's wrong with number 25. I can say that copying from an array to a string value and then using the string value serves no purpose and makes your code longer and harder to read.

$expected_posts = array("author", "subject", "text");
$errors = array();
foreach($expected_posts as $key) {
	if(empty($_POST[$key])) {
		$errors[] = "Please enter a ".$key;
	}
}
if(empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
	$errors[] = "Please enter a valid email address.";
}
if(count($errors) > 0) {
	foreach($errors as $err) {
		echo $err."<br>";
	}
} else {
  //send email
  mail("someone@example.com", $_POST['subject'],
  $_POST['text'], "From:" . $_POST['email']);
  echo "Thank you for using our mail form";
}

This post has been edited by CTphpnwb: 18 September 2011 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#11 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 18 September 2011 - 04:50 PM

Now when I actually run it to send an email, I get the error "Please enter a author." This is what I originally had that works, but as you can see it lacks validation. Any way that I could add my validation to what I have that works?



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];

mail("myemailaddress@gmail.com","Subject: $subject",
$message,"From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";
?>
</body>
</html>




Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,474
  • Joined: 23-August 08

Re: PHP Validation error

Posted 18 September 2011 - 05:52 PM

Do you still have an author field coming in form the form?
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,079
  • Joined: 08-August 08

Re: PHP Validation error

Posted 18 September 2011 - 06:10 PM

First, why is your code missing all of the validation previously discussed???

Second, doing the code below is not only a waste of processor cycles but it also makes it more likely that you'll make a mistake in your code.
$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];


Would you do this?
$variable_two = $variable_one;
echo $variable_two;


or this?
echo $variable_one;

This post has been edited by CTphpnwb: 18 September 2011 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#14 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 19 September 2011 - 09:01 AM

View PostJackOfAllTrades, on 18 September 2011 - 06:52 PM, said:

Do you still have an author field coming in form the form?


Yes, I do.

View PostCTphpnwb, on 18 September 2011 - 07:10 PM, said:

First, why is your code missing all of the validation previously discussed???

Second, doing the code below is not only a waste of processor cycles but it also makes it more likely that you'll make a mistake in your code.
$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];


Would you do this?
$variable_two = $variable_one;
echo $variable_two;


or this?
echo $variable_one;


Yes, I do understand its a waste of processor space. I was just posting up what I had that works and would like to stick to that as closely as possible, but with validation. Since when I actually ran my updated code with the validation, it wouldnt send an actual email after I had tested each of the fields by leaving them blank.
Was This Post Helpful? 0
  • +
  • -

#15 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: PHP Validation error

Posted 19 September 2011 - 12:04 PM

If possible, is there a tutorial that I can use that goes along with which version of php I am using? (Iam using php 6 and thats what I understand and works with my webhost).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2