Validate Email Address
Hello,
I have the following code and need to validate the email address. I have asked previuosly but had no luck in getting it to work. Mac promised to sort it but have not been able to get it done by him so far. The PHP code is: <?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) { 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 = "[email protected]"; //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' . "rn"; $header .= 'Content-type: text/html; charset=utf-8' . "rn"; $header .= 'From:'. $email . " rn"; $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); }The Javascript is: $(function() { if ($("#contact").length > 0) { $("#contact").submit(function() { //$('#button').hide(); $('#loader').show(); $("#contact .warn").remove(); var fields = $(this).serializeArray(); $.ajax({ type : 'POST', url : 'inc/form.php', data : fields, dataType : 'json', success: function(data) { $('#button').show(); $('#loader').hide(); if (data.error) { $('.error').fadeIn(300); jQuery.each(data.fields, function(k, v) { $('.' + k).append('<span class="warn">' + v + '</span>'); $('.warn').fadeIn(500); }); } else { $('.error').hide(); $('.confirm').fadeIn(300, function() { var d = setTimeout(function() { $('.confirm').fadeOut(300); }, 2000); }); $('#name').val(''); $('#email').val(''); $('#emailmessage').val(''); } } }); return false; }); } }); The form works fine and submits ok just the email is not validated so the user can type in anything he/she likes and the form will be submitted. Any help is much apprceaited. Thanks
3 Answers
look, this is the fixed files , so now the meaing of its just loading and not giving any thing is (the mail() function is disabiled)
Posted: MacOS 1 of 1 people found this answer helpful. Did you? Yes No
Hello Mac,
Thank you for your reply but the form does not submit when I use this foreach loop. Do you have another solution. I was given the following to do but not usre how to implement it. $email = '[email protected]'; if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // email is valid } else { // email is invalid } How can this be added to the form and used. Thanks Posted: qakbar 0 of 0 people found this answer helpful. Did you? Yes No
try to replace your foreach to
foreach($expected as $key) { if (array_key_exists($key, $_POST)) { if (empty($_POST[$key])) { $errors[$key] = $validation[$key]; } else { if($key == 'email'){ if (filter_var($_POST[$key], FILTER_VALIDATE_EMAIL)) { $output[$key] = $_POST[$key]; }else{ $errors[$key] = $validation[$key]; } }else{ $output[$key] = $_POST[$key]; } } } else { $errors[$key] = $validation[$key]; } } Posted: MacOS 1 of 2 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved