Page 1 of 1

posting to form error

Posted: Wed Aug 10, 2011 5:24 pm
by kc11
Hi everyone ,

I am trying to modify a web page I inherited which posts form data and emails it

Code: Select all


<?php
session_start();

//process form validation
	//include('functions.php');
	//include('contact.php');
	

	if ($_POST['first_name']=='' || strlen($_POST['first_name'])<3)
	{
		$errors[] = 'First name is required.';
	}

	if ($_POST['last_name']=='' || strlen($_POST['last_name'])<3)
	{
		$errors[] = 'Last name is required.';
	}
	
	if ($_POST['phone']=='' || strlen($_POST['phone'])<7)
	{
		$errors[] = 'Phone number is required. Please include your area code.';
	}

		if (valid_email($_POST['email'])==FALSE)
	{
		$errors[] = 'Please supply a valid email address.';
	}

	if(is_array($errors))
	{
		echo '<p class="error"><b>The following errors occured:</b><br />';
		while (list($key,$value) = each($errors))
		{

			echo '<span class="error">'.$value.'</span><br />';
		}
		echo '</p>';
	}
	else {
		//do something here----store to db, send email
		
		if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
	//Insert your code for processing the form here, eg. emailing submission, entering it into a database, etc.
	echo '';
	unset($_SESSION['security_code']);
} else {
	//Insert your code for showing an error message here
	echo "<p><span class=\"error\"><strong>Your form was NOT submitted.</strong></span><br>";
	die ("<span  class=\"error\">You have provided an invalid security code.<br><br><a style=\"cursor:pointer\" onClick=\"window.location.reload()\"> <u>Refresh the Security Code</u></a> to try again.</span></p>");
}
		
		$ip = $_POST['ip'];
		$httpref = $_POST['httpref'];
		$httpagent = $_POST['httpagent'];
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		$phone = $_POST['phone'];
		$time = $_POST['time'];
		$email = $_POST['email'];
		$message = $_POST['message'];
		$hear_about = $_POST['hear_about'];
		$web = $_POST['web'];
		$referral = $_POST['referral'];
		$other = $_POST['other'];
		
//define the receiver of the email
$to  = 'xxx@gmail.com';
//define the subject of the email
$subject = " form submitted.";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: info@xxx.com\r\nReply-To: info@xxx.com";
$headers .= "\r\nBcc: crosputni@xxx.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

We have received the following information:

Contact Information
First Name:  <?php echo $first_name?> 
Last Name:  <?php echo $last_name?>
Phone Number:  <?php echo $phone?>
Email:  <?php echo $email?>
<?php if($time): ?>Best time to contact: <?php echo $time; endif; ?>

<?php if($message): ?>Message: 
<?php echo $message ?><?php endif; ?>

<?php if($hear_about): ?>Where did you hear about us?
<?php foreach ($hear_about as $h) {
  echo "$h, ";
} ?> <?php endif; ?>
<?php if($web): ?>Web: <?php echo $web; endif; ?>
<?php if($referral): ?>Referral from: <?php echo $referral; endif; ?>
<?php if($other): ?>Other: <?php echo $other; endif; ?>

Additional Information:
Visitor IP:  <?php echo $ip?>
Browser Used:  <?php echo $httpagent?>
Referral URL:  <?php echo $httpref?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>We have received the following information:</h2>
<strong><u>Contact Information</u></strong><br />
<strong>First Name: </strong> <?php echo $first_name?><br />
<strong>Last Name: </strong> <?php echo $last_name?><br />
<strong>Phone: </strong> <?php echo $phone?><br />
<strong>Email: </strong> <?php echo $email?><br />
<?php if($time): ?><strong>Best time to contact: </strong> <?php echo $time?><br /><?php endif; ?><br />

<?php if($message): ?><strong>Message:</strong><br />
<?php echo $message ?><br /><br />
<?php endif; ?>
<?php if($hear_about): ?><strong>Where did you hear about us?</strong><br />
<?php foreach ($hear_about as $h) {
  echo "$h<br>";
} ?><br /><?php endif; ?>
<?php if($web): ?><strong>Web:</strong> <?php echo $web; ?><br /><?php endif; ?>
<?php if($referral): ?><strong>Referral from:</strong> <?php echo $referral; ?><br /><?php endif; ?>
<?php if($other): ?><strong>Other:</strong> <?php echo $other; ?> <?php endif; ?><br /><br />
<font size="-1"><strong><u>Additional Information:</u></strong><br />
<strong>Visitor IP: </strong> <?php echo $ip?><br />
<strong>Browser Used: </strong> <?php echo $httpagent?><br />
<strong>Referral URL: </strong> <?php echo $httpref?></font>


--PHP-alt-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer


$message = ob_get_clean();


//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo '<div id="complete"><p class="complete"><b>Thank you!</b><br><span>Your message was successfully processed. Someone will contact you shortly, usually within one business day. If you have any further questions, please contact us by phone.</span></p></div>';
}

?>


Can anyone see a problem. It appears to work fine on the website but on my local test environment , I get

[text]Parse error: syntax error, unexpected $end in validate.php on line 152
[/text]

the above error. any ideas on how to proceed?

Thanks,

KC

Re: posting to form error

Posted: Wed Aug 10, 2011 10:54 pm
by Jonah Bron
Are errors enabled on your website? Try putting this at the top of your code:

Code: Select all

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
?>
If 152 is indeed the last closing brace (?>), then this error should be on both your website and your localhost. It indicates that a bracket or brace or something was not closed (e.g. an { without an } ). I took a quick look, but I wasn't able to turn the source of the problem.