note 106237 added to function.filter-var

From: Date: Thu, 20 Oct 2011 16:04:52 +0000
Subject: note 106237 added to function.filter-var
Groups: php.notes 
Request: Send a blank email to php-notes+get-184018@lists.php.net to get a copy of this message
Hello, I was hoping someone can help me. I want to get the email to validate in the following script using the filter_var() This is my script but I cannot get it to validate email address. <?php if ($_POST) { $expected = array('name', 'email', 'emailmessage'); $validation = array( 'name' => 'Please provide your full name', 'email' => 'Please provide your valid email address', 'emailmessage' => 'Please provide message' ); $errors = array(); $output = array(); foreach($expected as $key) { $input = htmlspecialchars($_POST[$key]); if (array_key_exists($key, $_POST)) { if (empty($_POST[$key])) { $errors[$key] = $validation[$key]; } else { $output[$key] = $_POST[$key]; } } else { $errors[$key] = $validation[$key]; } } if (!empty($errors)) { $array = array('error' => true, 'fields' => $errors); } else { // PROCESS FORM // --------------------------------------------------------- // BEGIN EDITING // --------------------------------------------------------- $to = "qakbar@hotmail.co.uk"; //This is the email address messages will be sent to $web_name = "My Test Web Form"; //This is the name of your website that will show in your email inbox //get IP address $ip = $_SERVER['REMOTE_ADDR']; //make time $time = time(); $date = date("r", $time); // --------------------------------------------------------- // END EDITING // --------------------------------------------------------- $emailmessage = trim($emailmessage); $emailmessage = nl2br($emailmessage); $emailmessage = htmlspecialchars($emailmessage); $emailmessage = wordwrap($emailmessage, 70); //Visible form elements $name = $_POST['name']; //Sender's name $email = $_POST['email']; //Sender's email $emailmessage = htmlspecialchars($_POST['emailmessage']); //Sender's message //Setting up email $subject = "New Message from $web_name"; $message = " New message from $name <br/><br/> Message:<br /> $emailmessage <br/> <br/> Email: $email<br /> IP:</strong> <span style=\"color:#990000;\">$ip</span><br /> Date:</strong> $date "; $header = 'MIME-Version: 1.0' . "\r\n"; $header .= 'Content-type: text/html; charset=utf-8' . "\r\n"; $header .= 'From:'. $email . " \r\n"; $sent = mail($to, $subject, $message, $header); //$message = '<div id=message>You have successfully subscribed to our newsletter</div>'; $array = array('error' => false, 'message' => $message); } echo json_encode($array); } I have this code but do not know how to use it or call it in the $validation array if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // email is valid } else { // email is invalid } Any help given is much appreciated Thanks Qaysar ---- Server IP: 69.147.83.197 Probable Submitter: 2.30.186.228 ---- Manual Page -- http://www.php.net/manual/en/function.filter-var.php Edit -- https://master.php.net/note/edit/106237 Del: integrated -- https://master.php.net/note/delete/106237/integrated Del: useless -- https://master.php.net/note/delete/106237/useless Del: bad code -- https://master.php.net/note/delete/106237/bad+code Del: spam -- https://master.php.net/note/delete/106237/spam Del: non-english -- https://master.php.net/note/delete/106237/non-english Del: in docs -- https://master.php.net/note/delete/106237/in+docs Del: other reasons-- https://master.php.net/note/delete/106237 Reject -- https://master.php.net/note/reject/106237 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184018) next »