5 Replies - 355 Views - Last Post: 29 April 2020 - 10:42 AM Rate Topic: -----

#1 chrisjchrisj   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 04-February 19

Help with adding e-mail validation to web Form

Posted 28 April 2020 - 09:40 PM

Thanks for all the previous help.
I have this ‘existing’ code that now works successfully not allowing the Form to complete unless both Name and Email fields are populated. Now, I’d like to make sure the entered email address is valid before ‘success’.

And I tried this code snippet (unless you have a better idea), but not sure how to integrate it into the existing code. Anywhere I place it prevents the Form from displaying ‘error’ or ‘success’ messages:

function validEmail(string $email): bool {
	// check if e-mail address is well-formed
	if ( filter_var($email, FILTER_VALIDATE_EMAIL)) {
	  return true;
	}
	return false;
}



Here’s the ‘existing’ code:

<?php
header('Content-type: application/json');

$errors         = array();      // array to hold validation errors
$response = ['success' => false];

    if (empty($_POST['name'])|| empty($_POST['email'])){
    $response['success'] = false;
    } else {
    $response['success'] = true;
    }

   echo json_encode($response);

    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    }

    if(empty($errors)) {
	$to = '[email protected]';
	$subject = 'Thank You';
	$name = $_POST['name'];
	$email = $_POST['email'];
	$message = $_POST['message'];
	$message1 = $_POST['message'];
	$headers = $name;
	$headers = 'from: [email protected]';

	$message1 .= "\r\n\r\nName: ".$_POST['name']." \r\n Email: ".$_POST['email']." ";

	$message = "Hello {$_POST['name']}, ~ Thank you\r\n\r\n";

   		mail( $to, $subject, $message1, $headers );
   		mail( $email, $subject, $message, $headers );

		$data = ['success' => true];
}
?>


I appreciate any comments/guidance


This post has been edited by chrisjchrisj: 28 April 2020 - 09:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with adding e-mail validation to web Form

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4275
  • View blog
  • Posts: 13,542
  • Joined: 08-June 10

Re: Help with adding e-mail validation to web Form

Posted 29 April 2020 - 02:47 AM

something like if (validEmail($_POST['email'])) ...?

Note: you're susceptible to mail header injection.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,977
  • Joined: 08-August 08

Re: Help with adding e-mail validation to web Form

Posted 29 April 2020 - 04:33 AM

Why are you wrapping filter_var in a function? Why not call it directly? What you have makes your code longer, harder to maintain, and a tiny bit slower. It's the little things that when added together can make your site slow.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4275
  • View blog
  • Posts: 13,542
  • Joined: 08-June 10

Re: Help with adding e-mail validation to web Form

Posted 29 April 2020 - 05:30 AM

Looking that way you can even use filter_input() to shave off some microseconds.
Was This Post Helpful? 0
  • +
  • -

#5 chrisjchrisj   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 04-February 19

Re: Help with adding e-mail validation to web Form

Posted 29 April 2020 - 08:34 AM

Thanks for your replies.
How do I prevent "susceptible to mail header injection"?

Also, can you tell me why this doesn't this work:

    
if (empty($_POST['name'])|| empty($_POST['email'])){
    $response['success'] = false;
    } else {
    if (validEmail($_POST['email'])||(validName($_POST['name']))
    $response['success'] = true;
    }

   echo json_encode($response);


I look forward to any additional assistance
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2714
  • View blog
  • Posts: 7,997
  • Joined: 15-January 14

Re: Help with adding e-mail validation to web Form

Posted 29 April 2020 - 10:42 AM

Have you defined a function called validName?

And that logic is wrong, you're going to set success to true if either of those is valid, not only if both of them are.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1