Jump to content

Conditional redirect depending on the presence of certain element in the posted form data


ionicle
Go to solution Solved by PaulRyan,

Recommended Posts

Alright, guys and gals, here's my dilemma:

 

I am using the Tectite Formmail script on one of my pages as a "contact form processor". I've set up the script to record the contact email of the person that submits the form, as well as their message, in a file on my server, and then redirect the user to a "Thank you for your enquiry" page. So far so good.

 

What I wanna do is, add a few lines of code to the script so that it does the following upon a successful submission of the form, in this specific order:

 

1. Record the submitted data as usual.

 

2. Check whether the email address of the user belongs to a certain domain ( Yahoo!, Gmail, etc. ). That should be executed by checking if the word "yahoo" exists after the "@" symbol in the submission of the "email address" field. By doing that, I will be able to apply the rule for not only @yahoo.com, but for all regional @yahoo derivations. Same would go for Gmail and so forth.

 

3. If it does, immediately redirect the user to a page different than the default one I've set up in the script.

 

How would I go about doing that? 

Edited by ionicle
Link to comment
Share on other sites

if( preg_match('YOUR REGEXP HERE', $email_address) ){
//do redirect for custom page
header('Location: '.$redirect_link);
}else{
//do standard form processing
}

 

Above is the basic format that may work for you. You will however need to create the proper regular expression to catch each custom instance of an email address you want to redirect: see this link - http://www.regular-expressions.info/  or this one - http://gskinner.com/RegExr/ or this one - http://regexpal.com/

 

Reply if you need more help

 

E

Edited by The Letter E
Link to comment
Share on other sites

Do I really need to do this with RegEx?

 

Wouldn't something like that work:

 

 

 

if ($_POST['field'] == '@yahoo.') {

header('Location: page.php');

 

 

"field" is obviously the field name that contains the email address. But, since the email address will not be an exact string "@yahoo.", that will not work. 

Link to comment
Share on other sites

  • Solution

Here is something I threw together, give it a try.

 

 

<?PHP

  //### For test purposes
  $email = '123@yahoo.com';

  //### Array of endings and url to go to
  $emailArray = array('@yahoo.com' => 'URL 1',
                      '@gmail.com' => 'URL 2'
                     );
                     
  //### Iterate over each of the array elements to find a match for the email                   
  foreach($emailArray AS $ending => $location) {
    if(strstr($email, $ending)) {
      $headerLocation = $location;
      break;
    }
  }
 
  //### If the ending is matched, re-direct to that url
  if(isset($headerLocation)) {
 
    echo 'Re-directing to: '.$headerLocation;  
 
  //### If the ending is not match do something else
  } else {
 
    echo 'Normal form processing';
 
  }
 
?>
Link to comment
Share on other sites

Yup, that does seem like it's gonna be working. I replaced the example email with:

 $email = $_POST['email'];

where email is the name of the email input field on the submitting page, so it would capture the correct email address. Is that correct?

 

Also, how would you specify a varying "yahoo" domain? So that I can avoid listing all the regional Yahoo domains by hand ( yahoo.de, yahoo.co.uk etc. ). The only constant in that would be "@yahoo." .

 

Gmail doesn't use regional domains, so it wouldn't be an issue.

Edited by ionicle
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.