0

I am trying to make a "form" of the type "text" which will check whether an email is valid or not. I first tried to check if the text field was empty by making an if statement, but it didnt work, and it instead ran the "else" option of the statement. How do I fix it so it actually checks if the text field is empty? Here is the code.

INDEX.php:

<?php
include 'connection.php';
echo  "<p> Example Text </p>";



?>

<h1> Submit email address </h1>

<form action = "create.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />

<input type = "submit" name = "submit" />

</form>

Create.php :

<?php
include 'connection.php';
$email = $_POST['email'];

if(!isset($email)){
echo "please fill out the form";
header ('Location: index.php');
}
else{
echo "Successive login";
}

?>
6
  • And your question is? Commented Mar 12, 2015 at 22:52
  • You're not checking if a field is empty, but if variable is defined and is not NULL. var_dump($email); and see that it's a string. Commented Mar 12, 2015 at 22:55
  • Another problem: echo does not get displayed because it's immediately followed by the header redirect. Commented Mar 12, 2015 at 22:58
  • @bloodyKnuckles header() function does not terminate script execution actually. Commented Mar 12, 2015 at 22:59
  • Actually, the header redirect fails (with a warning) since something's already been echoed. Commented Mar 12, 2015 at 23:05

6 Answers 6

1

When doing:

$email = $_POST['email'];

You are in fact setting $email so the !isset() will return false (meaning that it is NOT not isset)

instead you need to check if it is not empty

if(!empty($email)){

}
else{

}

================

EXTRA Here is an example of how how you can check valid email addresses:

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}

Also check out my PHP function that checks to see if the email is valid and can receive emails by connecting to the mail server and verifying it:

https://github.com/hbattat/verifyEmail

I did that. It did work when I removed the header code as well, but I want the echo to be displayed in index.php rather than in a new page (create.php). How do i do that? – tomSurge 3 hours ago

When you submit to the create.php basically you load the create.php page with some post parameters. So anything you display there will not be displayed in index.php because you are not in the same page.

Instead you could post to the same page index.php and do the create process there:

<?php
include 'connection.php';
if($_SERVER['REQUEST_METHOD'] == 'post'){ //check if the page was requested via post method (that means the form was submitted)
  if(!isset($email)){
     echo "please fill out the form";
  }
  else{
    echo "Successive login";
  }
}
echo  "<p> Example Text </p>";
?>

<h1> Submit email address </h1>

<form action = "index.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />

<input type = "submit" name = "submit" />

</form>

OR

You could use AJAX and not load the page when posting the info to create.php and just display the response message:

<?php
include 'connection.php';
echo  "<p> Example Text </p>";
?>

<!-- include jQuery in the header of your html -->
<script src="LINK TO jQUERY .js file"></script>

<script type="text/javascript">
$(document).ready(function(){
  //trigger this when the form is submitted
  $('form').on('submit', function(e){
    //prevent default submission and reload of the page
    e.preventDefault();

    //post the date to the create.php file using ajax
    //get the form data
    var formData = $(this).serialize();
    $.post('create.php', fomrData, function(response){
       var result = parseJSON(response);
         $('#msg').text(result.msg);
       }
    });
  });
});
</script>

<h1> Submit email address </h1>

<form action = "create.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />

<input type = "submit" name = "submit" />

</form>

create.php

<?php
include 'connection.php';
$email = $_POST['email'];

$result = array();
if(!isset($email)){
  $result['status'] = 'fail';
  $result['msg'] = "please fill out the form";
}
else{
  $result['status'] = 'success';
  $result['msg'] = "Successive login";
}

//echo the json
echo json_encode($result);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I did that. It did work when I removed the header code as well, but I want the echo to be displayed in index.php rather than in a new page (create.php). How do i do that?
header("location: index.php') will cause immediate redirection to the index page. instead you might want to submit to the same page index.php and show the message there. I'll include couple of examples for you.
1

isset() only checks if the variable is set. You need to use empty($email) instead.

Comments

1

Replace isset with empty and it should work as you want.

Comments

1

If I understand your question, you have to change the statement to if(!empty($email)) Also, if you want to check if the email is in the correct format, please use a regex to do so. isset() only checks for null

Comments

1

You can also test for submission of something (anything) in the posted email field with:

'' == $_POST['email']

As others have suggested, empty($_POST['email']) works too, just showing another option.

Displaying the Error Message

If you want the error message to appear on index.php then you need to pass the message or some sort of flag from the create.php page (where the error is discovered) to the index.php page (where the error message is displayed).

To demonstrate a bare-bones example (one of many ways to accomplish this), first I added a PHP variable to the form. Notice the line containing echo $_GET['error']; immediately after the email input element:

index.php (form):

<form action="create.php" method="post">
<input type="text" name="email" value="" />
<?php if (isset($_GET['error'])) { echo $_GET['error']; } ?>
<br />

<input type="submit" name="submit" />

</form>

...and, instead of echo'ing the message in the create.php script, send the message along to the index.php script in the URL query:

create.php (email check):

if ( '' == $_POST['email'] ) {
  header('Location: index.php?error=please+fill+out+the+form');
}

Additionally, I recommend you use this (PHP server-side validation) as a backup to a Javascript client-side validation solution. Nicer for your website visitors and uses fewer server resources.

3 Comments

Hi, I liked your solution. The only problem I had with it is the error message i get when loading index.php. I get the following error message: "Notice: Undefined index: error in C:\xampp\htdocs\phpexample\index.php on line 14"
@tomSurge I appreciate the feedback, and updated the code to prevent a notice being issued. About errors, there are different levels and Notice is the least severe and does not prevent the code from proceeding. More severe, but not a show-stopper is Warning, and then Parse, and Error, which do terminate execution.
It now worked. Thank-you very much good sir. I get what you mean, the previous code worked as well, but I have error notifications on, and it's quite annoying seeing any type of error/issues.
0

Another way is to just add a required="" to your input Here is an example https://jsfiddle.net/57s38s2e/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.