Jump to content

PHP form: am I doing this the hard way?


ivndrs

Recommended Posts

Hey guys, I have a PHP form that I'm working on -- actually, it's fully functional -- but I feel like I might be going about certain ideas all wrong. It goes like this: the contact.php page has the PHP at the top and then the HTML below. When you arrive at the page it shows the form; when you submit the form, the page posts to itself; if you made errors, it redirects to an error page...otherwise, it displays a success message on the page. My question is: which of these arrays, if statements, etc. could be reworked to be simpler or more correct? I really feel like the line for checking blank fields

if ($submitted == true) {
if($err == true || $name == '' || $message == '' || $recipient == 'please_select') {
header("location:/clients/hollybrook/error"); 
exit();
}
}

is not done well.  Here is the entire page (I removed a lot of the HTML for brevity. doctype is strict):

 

<?php
$name = htmlspecialchars($_POST["name"], ENT_QUOTES);
$phone = htmlspecialchars($_POST["phone"], ENT_QUOTES);
$emailaddress = trim($_POST["email"]);
$recipient = htmlspecialchars($_POST["recipient"], ENT_QUOTES);
$message = stripslashes(htmlspecialchars($_POST["message"], ENT_QUOTES));
$headers = "From: $emailaddress\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1";
$recipients = array(  

'cedar_lane' => 'heather@gomontessori.com', 
'holly_brook' => 'heather@childersmarketing.com', 
'hunter_mill' => 'ivan@gomontessori.com',
'please_select' => 'no_recipient'

);

$schoolEmail = $recipients[$_REQUEST['recipient']];

$emailsubject = array(  

'cedar_lane' => 'Cedar Lane Website Submission', 
'holly_brook' => 'Holly Brook Website Submission', 
'hunter_mill' => 'Hunter Mill Website Submission',
'please_select' => 'no_recipient'

);

$subject = $emailsubject[$_REQUEST['recipient']];

if ($phone) {
  $emailmessage = "
  <p>Hello from your website!</p>
  <p>$name sent you this message</p>
  <p><strong>$message</strong></p>
  <p>Their phone number is: $phone</p>
  ";
}
else {
  $emailmessage = "<p>Hello from your website!</p><p>$name sent you this message</p><p><strong>$message</strong></p>";
};
$submitted = false;
if(count($_POST) > 0)
  {
  $submitted = true;
}
$err = true;
if ($emailmessage) {
  if (validate_email($emailaddress)) {
    mail($schoolEmail,$subject,$emailmessage,$headers);
    $err = false;
  }
};

function validate_email($email){

  $bool = false;

  if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$',  $email)) {
  if(checkdnsrr(array_pop(explode("@",$email)),"MX")) {
	  $bool = true;
  } else {
    $bool = false;
    }
  } else {
  $bool = false;
}   

return (bool) $bool;

}
if ($submitted == true) {
if($err == true || $name == '' || $message == '' || $recipient == 'please_select') {
header("location:/clients/hollybrook/error"); 
exit();
}
}
?>
<html>
<head>
<title>whatever</title>	
</head>

<body>

			<?php if ($err == false && $submitted == true): ?>
			  <p>Thanks so much! If the nature of your contact requires a response or answer, we will respond to you in a timely manner.</p><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
			<?php else: ?>
			  <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" accept-charset="utf-8">
			      <div class='rowElem'>
			        <label for="name">Name</label>
			        <input type="text" name="name" value="" id="name" class="required" />
			      </div>
			      <div class='rowElem'>
			        <label for="email">Email</label>
			        <input type="text" name="email" value="" id="email" class="required validate-email" />
			      </div>
			      <div class='rowElem'>
			        <label for="phone">Phone</label>
			        <input type="text" name="phone" value="" id="phone" />
			      </div>
				  <div class='rowElem'>
			        <label for="recipient">School</label>
			        <select name="recipient">  
					<option value="please_select">Please Select A School</option>
					<option value="cedar_lane">Cedar Lane</option>
					<option value="holly_brook">Holly Brook</option> 
					<option value="hunter_mill">Hunter Mill</option> 
					</select>
			      </div>
			      <div class='rowElem'>
			        <label for="message">Comments/Questions</label>
			        <textarea name="message" id="message" rows="15" cols="40" class="required" ></textarea>
			      </div>
			      <div class='rowElem'><label>Looking forward to hearing from you!</label> <input name="commit" id="submit" type="submit" value="Send" /></div>
			  </form>
			<?php endif ?>

</body>
</html>

 

I like the idea of keeping the form and processing on a single page, but I would be open to changing this if necessary. Thanks so much!

Link to comment
Share on other sites

you could submit to a processing page and have that redirect to the appropriate page on error or sucess, about the errors, you can use an array that you should store in a post variable then you will be able to access it, if there are any errors then the relevant mesages could be placed next to the relevant boxes if needed.

Link to comment
Share on other sites

Thank you for the quick reply. Unfortunately I'm pretty new to PHP so I don't know much about the POST variable and still have some difficulty with arrays. If it's not too much work, could you or someone else show me an example of the code? Thanks again!

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.