Jump to content

Help with a contact form - Case / Switch


Zola

Recommended Posts

Hi,

 

I have been using an old contact form for a while now, this is it:

<?php
if (isset($_POST['submit'])) {
  switch($_POST['contact_type']) {
      case 'Customer':
      $to = '[email protected]';
      break;	
case 'HQ';
      $to = '[email protected]';
      break;  
    case 'Office 2':
      $to = '[email protected]';
      break;
    case 'Office 3':
      $to ='[email protected]';
      break;
   case 'Office 4':
      $to = '[email protected]'; 
      break;
    case 'Office 5':
      $to = '[email protected]'; 
      break;
  }

  $subject = "WEBSITE ENQUIRY";
  $name_field = $_POST['name'];
  $email_field = $_POST['email'];
  $message = $_POST['message'];

  if (strlen(trim($message)) > 0) {

    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

    if (mail($to, $subject, $body)) {
      echo "<h3>Thanks! Your email has been sent <br />We will answer your enquiry as soon as possible.</h3>";
    } else {
      echo 'Cannot sent mail';
    }
  } else {
    echo "<h3>Error! <br />Please ensure that all fields are filled out in order to email us!</h3>";
  } 
} else {
  echo "blarg!";
}
?>

 

 

I recently extended the form with HTML5 etc and got a new form script, which makes a lot of improvements with the PHP.

 

I want to put this on a site I am working on. The current form has an contact type pull down which changes the destination of the submitted form.  It looks like this:

 

<?php
if( isset($_POST) ){

//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name'];	
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$company = $_POST['company'];
$enquiry = $_POST['enquiry'];
$product = $_POST['product'];
$message = $_POST['message'];

//validate form data

//validate name is not empty
if(empty($name)){
	$formok = false;
	$errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
	$formok = false;
	$errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
	$formok = false;
	$errors[] = "You have not entered a valid email address";
}


//validate company is not empty
if(empty($company)){
	$formok = false;
	$errors[] = "You have not entered a name";
}

//validate message is not empty
if(empty($message)){
	$formok = false;
	$errors[] = "You have not entered a message";
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
	$formok = false;
	$errors[] = "Your message must be greater than 20 characters";
}

//send email if all is ok
if($formok){
	$headers = "From: [email protected]" . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	$emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>
				  <p><strong>Name: </strong> {$name} </p>
				  <p><strong>Email Address: </strong> {$email} </p>
				  <p><strong>Telephone: </strong> {$telephone} </p>
				  <p><strong>Company: </strong> {$company} </p>
				  <p><strong>Enquiry: </strong> {$enquiry} </p>

				  <p><strong>Product of Interest: </strong> {$product} </p>

				  <p><strong>Message: </strong> {$message} </p>
				  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

	mail("[email protected]","New Enquiry",$emailbody,$headers);

}

//what we need to return back to our form
$returndata = array(
	'posted_form_data' => array(
		'name' => $name,
		'email' => $email,
		'telephone' => $telephone,
		'company' => $company,
		'enquiry' => $enquiry,
		'product' => $product,
		'message' => $message
	),
	'form_ok' => $formok,
	'errors' => $errors
);


//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
	//set session variables
	session_start();
	$_SESSION['cf_returndata'] = $returndata;

	//redirect back to form
	header('location: ' . $_SERVER['HTTP_REFERER']);
}
}

 

 

As you can see the new php form is much better, validates each field etc...

 

I am having issues getting the case and switch statement into my new form though, can anyone please advise where to put it / what I need to take out to make it work? Its an important part of the form I need to make.

 

Any help would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/259723-help-with-a-contact-form-case-switch/
Share on other sites

I have tried that with no luck.

 

This is what I have so far:

 

 

        <h2>Contact us</h2>  
        
        <ul id="errors" class="">  
            <li id="info">There were some problems with your form submission:</li>  
        </ul>  
        
        <p id="success">Thanks for your message! We will get back to you ASAP!</p>  
        
        
        <form method="post" action="process.php">  
            <label for="name">Name: <span class="required">*</span></label>  
            <input type="text" id="name" name="name" value="" placeholder="John Doe" required="required" autofocus="autofocus" />  
      
            <label for="email">Email Address: <span class="required">*</span></label>  
            <input type="email" id="email" name="email" value="" placeholder="[email protected]" required="required" />  
      
            <label for="telephone">Telephone: </label>  
            <input type="tel" id="telephone" name="telephone" value="" />  
            
             <label for="company">Company: </label>  
            <input type="text" id="company" name="company" value="" />  
      
            <label for="enquiry">Enquiry Type: </label>  
            <select id="enquiry" name="enquiry">  
                <option value="Customer">General Contact</option>  
                <option value="HQ">Sales Enquiry</option>  
            </select>  
            
            
            <label for="product">Product of Interest: </label>  
            <select id="product" name="product">  
                <option value="1">1</option>  
                <option value="2">2</option>  
                <option value="3">3</option> 
                <option value="4">4</option>   
                <option value="5>5</option>  
                <option value="6">6</option>  
                <option value="7">7</option>  
                <option value="8">8</option>  
                <option value="9">9</option>  
                <option value="10">10</option>  

            </select>  
            
      
            <label for="message">Message: <span class="required">*</span></label>  
            <textarea id="message" name="message" placeholder="Your message must be greater than 20 charcters" required="required" data-minlength="20"></textarea>  
      
            <span id="loading"></span>  
            <input type="submit" value="Send" id="submit-button" />  
            <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>  
        </form>  

 

 

Process.php:

 

<?php
if( isset($_POST) ){

//form validation vars
$formok = true;
$errors = array();

//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name'];	
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$company = $_POST['company'];
$enquiry = $_POST['enquiry'];
$product = $_POST['product'];
$message = $_POST['message'];

//validate form data

//validate name is not empty
if(empty($name)){
	$formok = false;
	$errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
	$formok = false;
	$errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
	$formok = false;
	$errors[] = "You have not entered a valid email address";
}


//validate company is not empty
if(empty($company)){
	$formok = false;
	$errors[] = "You have not entered a name";
}

//validate message is not empty
if(empty($message)){
	$formok = false;
	$errors[] = "You have not entered a message";
}
//validate message is greater than 20 charcters
elseif(strlen($message) < 20){
	$formok = false;
	$errors[] = "Your message must be greater than 20 characters";
}

//send email if all is ok
if($formok){
	$headers = "From: [email protected]" . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	$emailbody = "<p>You have recieved a new message from the enquiries form on your website.</p>
				  <p><strong>Name: </strong> {$name} </p>
				  <p><strong>Email Address: </strong> {$email} </p>
				  <p><strong>Telephone: </strong> {$telephone} </p>
				  <p><strong>Company: </strong> {$company} </p>
				  <p><strong>Enquiry: </strong> {$enquiry} </p>

				  <p><strong>Product of Interest: </strong> {$product} </p>

				  <p><strong>Message: </strong> {$message} </p>
				  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
				  
				  
			  switch($_POST['enquiry']) {
			case 'Customer':
			$to = '[email protected]';
			break;	
		  case 'HQ';
			$to = '[email protected]';
			break;  
		}			  
				  
	mail($to,"New Enquiry",$emailbody,$headers);			  


}

//what we need to return back to our form
$returndata = array(
	'posted_form_data' => array(
		'name' => $name,
		'email' => $email,
		'telephone' => $telephone,
		'company' => $company,
		'enquiry' => $enquiry,
		'product' => $product,
		'message' => $message
	),
	'form_ok' => $formok,
	'errors' => $errors
);


//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
	//set session variables
	session_start();
	$_SESSION['cf_returndata'] = $returndata;

	//redirect back to form
	header('location: ' . $_SERVER['HTTP_REFERER']);
}
}



First echo the $_POST['enquiry'] / $to

to see your value

 

also I would suggest having a default on your switch

 

switch($_POST['enquiery']){
case 'Customer':
$to = '[email protected]';
break;
case 'HQ';
$to = '[email protected]';
break;
default:
$to = '[email protected]';
}
echo 'POST is '.$_POST['enquiry'].' SO Sending to '.$to.'<br />';

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.