Jump to content

php form troubles


phillyJ

Recommended Posts

Hi there,

 

I'm perplexed by a form that provides a "sent" statement, but the message never arrives in my gmail account.  My client's other (contact) form functions perfectly, and the php form I'm having difficulty with is very similar.

 

I isolated the form so that the user now accesses it by clicking a link. The "complete form" link is in the "gift package insert" box on the right-hand side of the page. This is not an actual page on the site.

http://www.btbaking.com/product_gc.php

 

I've also attached the gift_insert form.

 

I'm a relative newbie, but feel like I've methodically checked over the code.

 

Thanks in advance for any advice or suggestions,

 

Jon

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/136221-php-form-troubles/
Share on other sites

Although the attachment includes the form and all the php scripts, here's the code/script.  Didn't realize how nicely formatted it would be.  Cool!  :)

 

<?php 
include('includes/title.inc.php'); 
include('includes/corefuncs.php'); 
if (function_exists('nukeMagicQuotes')) {
nukeMagicQuotes();
}
//corefuncs.php contains the function nukeMagicQuotes, clears backslash on each quote
//to prevent errors, the call is wrapped in a conditional statement using function_exists

//process all e-mail
if (array_key_exists('send', $_POST)) {
$to = '[email protected]';
$subject = 'gift form';

//list expected fields, processes only expected variables... more secure
$expected = array('to', 'from', 'email', 'message');
//set required fields
$required = array('to', 'from', 'email', 'message');

//create empty array for any missing fields
$missing = array();
//assume there is nothing suspect
$suspect = false;
//create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc;|Cc:/i';
//function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
	//if the variable is an array, loop through each element
	//and pass it in succession back to the same fuction
	if (is_array($val)) {
		foreach ($val as $item) {
			isSuspect($item, $pattern, $suspect);
			}
		}
	else {
		//if one of the supect phrases is found, set Boolean to true
		if (preg_match($pattern, $val)) {
			$suspect = true;
			}
		}
	}
	//check the $_POST array and any subarrays for suspect content
//isSuspect($_POST, $pattern, $suspect);
$suspect=0;
if ($suspect) {
	$mailSent = false;
	unset($missing);
	}
else {
//process the $_POST variables
foreach ($_POST as $key => $value) {
	//assign to temporary variable & strip whitespace if not array
	$temp = is_array($value) ? $value : trim($value);
	//if empty & required, add to $missing array
	if (empty($temp) && in_array($key, $required)) {
		array_push($missing, $key);
		}
	//otherwise, assign to a variable of the same name as $key
	elseif (in_array($key, $expected)) {
		${$key} = $temp;
		}
	}

}

// validate the email address
  if (!empty($email)) {
    // regex to ensure no illegal characters in email address 
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
  array_push($missing, 'email');
  }
}

//go ahead only if all required fields are ok
if (empty($missing)) {

	//build message \n adds new line
	$message = "To: $to\n\n";
	$message .= "From: $from\n\n";
	$message .= "Email: $email\n\n";	
	$message .= "Message: $message";


	//limit line length to 70 characters
	$message = wordwrap($message, 70);

	//send it
	$mailSent =mail($to, $subject, $message);
	if ($mailSent) {
		//$missing is no longer needed if the email is sent, so unset it
		unset($missing);
		}
	}
}
?>

 

<?php
	if ($_POST && isset($missing)) {
	?>
	<p class="warning">Please complete the missing item(s) indicated.</p>
	<?php
	  }
	elseif ($_POST && !$mailSent) {
	?>
	  <p class="warning">Sorry, there was a problem sending your message. Please try later.</p>
	<?php
	  }
	elseif ($_POST && $mailSent) {
	?>
	  <p class="sent">Thank you. An insert will be included with your gift package.</p>
	  <?php } ?>
        
      <form id="gift_form" method="post" action="">
            <fieldset>
            <div id="form_left">
                <label for="to">To: <?php
			if (isset($missing) && in_array('to', $missing)) { ?>
			<span class="warning">Enter name.</span><?php } ?>
			</label>
                <input name="to" id="to" type="text" class="formbox" 
			<?php if (isset($missing)) {
			  echo 'value="'.htmlentities($_POST['to']).'"';} ?>
			/>
       		</div>
            <div id="form_right">
              <label for="from">From: <?php
			if (isset($missing) && in_array('from', $missing)) { ?>
			<span class="warning">Enter name.</span><?php } ?>
		  </label>
              <input name="from" id="from" type="text" class="formbox" 
			<?php if (isset($missing)) {
			  echo 'value="'.htmlentities($_POST['from']).'"';} ?>
			/>
            </div>
            <br class="clearfloat" />
            </fieldset>
            
            <fieldset>
            <label for="email">Your email address: <?php
			if (isset($missing) && in_array('email', $missing)) { ?>
			<span class="warning">Enter email.</span><?php } ?>
		</label>
                <input name="email" id="email" type="text" class="formbox" 
			<?php if (isset($missing)) {
			  echo 'value="'.htmlentities($_POST['email']).'"';} ?>
			/>
            </fieldset>
            
            <fieldset>
                <label for="message">Gift message: <?php
			if (isset($missing) && in_array('message', $missing)) { ?>
			<span class="warning">Please enter your message.</span><?php } ?>
		</label>
              <textarea name="message" id="message"><?php 
			if (isset($missing)) {
			  echo htmlentities($_POST['message']);
			  } ?></textarea>
            </fieldset>
            <div>
                <input name="send" id="send" class="submit" type="submit" value="Send message" />
            </div>
      </form>

 

Thanks,

 

Jon

Link to comment
https://forums.phpfreaks.com/topic/136221-php-form-troubles/#findComment-710607
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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