Jump to content

Recommended Posts

Hi , There 

 

I m little bit puzzled how to do something, and wonder if one of you can help me with that. 

 

I have two forms, on 2 separate pages and I would like to use one .php file to send the email. 

 

HTML from 

.
.
.
<input class="buttonSubmit" type="submit" name="submit2" value="Send it!" />

and submit one for 1st form. 

 

Then I have the this PHP file. 

<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "EMAIL HERE";
    $email_subject = "SUBJECT HERE";
	     
       function died($error) {
        // your error code can go here
		
       echo   "We are very sorry, but there were error(s) found with the form you submitted. ";
       echo  "These errors appear below.<br /><br />";
       echo  $error."<br /><br />";
       echo  "Please go back and fix these errors.<br /><br />";
       
		die();
		
	   }
   
     
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
		!isset($_POST['spamcheck']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $name = $_POST['name']; // required
    $email = $_POST['email']; // required
    $phone = $_POST['phone']; // required
    $message = $_POST['message']; // required
	$spamcheck = $_POST['spamcheck']; // required
   
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
   $string_exp = '/[^A-Z]/';
  if(!preg_match($string_exp,$phone)) {
    $error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
  }
  $string_exp = '/^[5]+$/';
  if(!preg_match($string_exp,$spamcheck)) {
    $error_message .= 'The Number you have entered does not appeart to be valid.<br />';
  }
  if(strlen($message) < 2) {
    $error_message .= 'The Message you entered do not appear to be valid.<br />';
  }
  
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "name: ".clean_string($name)."\n";
    $email_message .= "email: ".clean_string($email)."\n";
    $email_message .= "phone: ".clean_string($phone)."\n";
    $email_message .= "message: ".clean_string($message)."\n";

     
     
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

 
<?php
}
?>

Okay. 

As you can see, I have validation there too. 

My first form doesn't have message box , but second does.

 

Now.

I know I would need " IF " there.

Would that be.

if (isset($_POST['submit1'])) { 
    /* INCLUDE FORM ONE  */ 
} else if (isset($_POST['submit2'])) { 
   /* FORM TWO */ 
} 

Thanks 

 

Kind Regards 

 

Asmo

 

 

Link to comment
https://forums.phpfreaks.com/topic/281203-two-html-forms-1-php-process-file/
Share on other sites

I would not rely upon input submit buttons to determine the page. I'm pretty sure that when a users presses their enter key to submit a form (as opposed to clicking the submit button) that the input button fields/values are not passed in the POST/GET data - or at least I don't believe it behaves consistently across browsers. Instead, include a hidden field in the forms to specify whether it is for1 or form2.

<input type="hidden" name="form" name="form1" />

But, do yourself a favor and give them descriptive names!

 

Then on the processing page, use that value (only where needed) to determine which processing to run when it needs to be different. In other words, if you have 9 fields on both forms that need to be processed the same and one for has a 10th field, don't copy and past all of the logic for the 9 fields and put them into if/else blocks. Instead do something like this

$string_exp = '/^[5]+$/';
if(!preg_match($string_exp,$spamcheck)) {
    $error_message .= 'The Number you have entered does not appeart to be valid.<br />';
}

if($form = 'form2' && strlen($message) < 2) {
    $error_message .= 'The Message you entered do not appear to be valid.<br />';
}
Edited by Psycho

HI, 

 

Thank for you reply. 

 

Okay. I done that. 

However , it's still doesnt work the way I want it. 

Actually, the first form. Doesn't get past through the validation. Which makes me believe I will need to set IF/ELSE as on the first form ( it's for newsletter ) , I need , name , phone and email.

 

Thanks 

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.