Jump to content

New to PHP, need help with comparing input


macman90

Recommended Posts

I just got started working with PHP this week, and I could use some help. I am working on a project for my job, and this is what I need for my first phase.  I have a form setup with a text box, what I need php to do is take the input from that text box and search through it for a particular string. If it finds the string it needs to send an email, if not it needs to display a message saying so.  Basically people will be inputing their email address into this text box. I need to search the address to see if it contains either "@apple.com" or "@filemaker.com". If it contains either of those two I need it to generate an email and send it to the address, if not it needs to display a message saying we can't send them the email. I have been playing with elseif and strpos, but this is all new to me and I could use some pointers. Thanks!

$valid = filter_var((String) $variable, FILTER_VALIDATE_EMAIL);
if ($valid) {
// send mail
} else {
// do something else
}

 

filter_var only works with PHP >= 5.2 so if you don't have that running you can use preg_match and the regex pattern from the filter_var function here:

http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c?view=co&content-type=text%2Fplain

 

"/^((\\\"[^\\\"\\f\\n\\r\\t\\b]+\\\")|([A-Za-z0-9_][A-Za-z0-9_\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\=\\?\\^\\`\\|\\{\\}]*(\\.[A-Za-z0-9_\\!\\#\\$\\%\\&\\'\\*\\+\\-\\~\\/\\=\\?\\^\\`\\|\\{\\}]*)*))@((\\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9])(([A-Za-z0-9\\-])*([A-Za-z0-9]))?(\\.(?=[A-Za-z0-9\\-]))?)+[A-Za-z]+))$/D"

(found in function php_filter_validate_email)

Arh okay.

Here's how you create that form and the php file it sends to:

http://www.w3schools.com/PHP/php_forms.asp

 

Then you use that $_POST["mail"] you are going to create with your form as <input type="text" name="mail"... > with the first code I provided.

And to send a mail with php you use the mail() function:

http://php.net/manual/en/function.mail.php

 

Here's some help.

<?php
// Do we have form data?
if (isset($_POST["mail"]))
{
  // Check to see if the text is in a valid mail format
  $valid = filter_var((String) $_POST["mail"], FILTER_VALIDATE_EMAIL);

  // So is it valid?
  if ($valid)
  {
    // Yes, send mail
    mail('[email protected]', 'My Subject', 'My Message');
  } else {
    // No, do something else
    echo "Invalid Email";
  }

}
?>

Thanks, thats allot more helpful. Now with this particular form I need to check the input to see if it matches apple.com or filemaker.com. How can I set it to check multiple strings?

 

Hi, i'm just learning this myself but thought this could be helpful.

 

<form method="post" action= "test.php" >
<p>Email: </p>
<input type="text" name="email" id="email" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>

<?php
//retrieve variable from the form.
$email = $_POST['email'];
//the 2 address extensions you wish to include.
$findme   = '@apple.com';
$findme2 = '@filemaker.com';
//Checks to see if the email contains the extensions.
$pos = strpos($email, $findme);
$pos2 = strpos($email, $findme2);

//if the email contains the extention then send email.
//if not send sorry message.
if ($pos !== false || $pos2 !== false) {
     echo "Thankyou, we have emailed sent an email to '$email'";
	 //insert mail code... use $email as the to part.
} else {
     echo "Sorry your email address, '$email' is not compatible.<br />";
}
?>

 

Sorry i'm not sure how to implement the mail code for you but i think if you just put it where i commented then it should work?

 

Let me know, as i need the practice.

Thanks, thats exactly what I needed, now I just have to work with my boss to make some mods for our specific setup, thanks allot! I'll post if I have any more questions.

 

 

Hi, i'm just learning this myself but thought this could be helpful.

 

<form method="post" action= "test.php" >
<p>Email: </p>
<input type="text" name="email" id="email" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>

<?php
//retrieve variable from the form.
$email = $_POST['email'];
//the 2 address extensions you wish to include.
$findme   = '@apple.com';
$findme2 = '@filemaker.com';
//Checks to see if the email contains the extensions.
$pos = strpos($email, $findme);
$pos2 = strpos($email, $findme2);

//if the email contains the extention then send email.
//if not send sorry message.
if ($pos !== false || $pos2 !== false) {
     echo "Thankyou, we have emailed sent an email to '$email'";
	 //insert mail code... use $email as the to part.
} else {
     echo "Sorry your email address, '$email' is not compatible.<br />";
}
?>

 

Sorry i'm not sure how to implement the mail code for you but i think if you just put it where i commented then it should work?

 

Let me know, as i need the practice.

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.