user3761911
user3761911

Reputation: 89

How to check if HTML forms are empty or not with PHP

I was wondering if there was a way to to check if html text inputs are empty, and if so, execute a certain PHP code, which I will provide.

HTML

<input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">

<input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">

<button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>

PHP

if(!isset(['name']) || !isset(['email']) == 0){
        $msg_to_user = '<h1>Fill out the forms</h1>';
}

I was wondering if:

  1. My PHP code is correct syntax, which I believe it's not
  2. Is it possible to check if an input is empty, and if it is empty, execute a PHP code (specifically '$msg_to_user')
  3. It is possible to tweak my code so that when the user clicks the submit button, and the fields are BOTH empty, to have the $msg_to_user code execute

Thank you in advance!

Upvotes: 3

Views: 6248

Answers (2)

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34199

You should use isset check on array - $_POST or $_GET according to your HTML form method.

It should be:

<form method="POST">
    <input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="">
    <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="">
    <button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
</form>

PHP:

if (!isset($_POST['name']) || !isset($_POST['email']))
{
    $msg_to_user = '<h1>Fill out the forms</h1>';
} else {
    // process your results
}

I guess, I have answered questions A and B. What about question C - it's up to your architecture. For example, if you want to post form from page to itself (I mean index.php file has a form with action='index.php' or empty action), then you can do it this way (just an example approach, definitely not the best):

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (!isset($_POST['name']) || !isset($_POST['email']))
    {
        $msg_to_user = '<h1>Fill out the forms</h1>';
    } else {
        // do something in database
        if ($doSomethingInDatabaseSuccess)
            $msg_to_user = '<h1>Succesffully saved!</h1>'; // really? h1? :)
        else
            $msg_to_user = '<h1>Something went wrong!</h1>';
} else {
    $msg_to_user = ''; 
}

<form method="POST">
    <input name="name" type="text" class="form-control form-control-lg" id="exampleInputName2" placeholder="your name" maxlength="36" value="">
    <input style="margin-top: 10px;" name="email" type="text" class="form-control form-control-lg" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="">
    <button name="mySubmitBtn" type="submit" class="btn btn-default btn-md">subscribe</button>
</form>
<p class="result"><?php echo($msg_to_user); ?></p>

Upvotes: 2

m1xolyd1an
m1xolyd1an

Reputation: 535

A. No your syntax is off.

B. Yes

C. Yes, first you need to give your button a name property, so that it can be picked up by PHP. You also need to assign the $email and $name variables with a $_POST variable.

HTML

<input type="text" name="name" class="form-control" id="exampleInputName2" placeholder="your name" maxlength="36" value="<?php echo $name; ?>">
<input type="email" style="margin-top: 10px;" name="email"  class="form-control" id="exampleInputEmail2" placeholder="your email" maxlength="36" value="<?php echo $email; ?>">
<input type="submit" name="mySubmitBtn" class="btn btn-default btn-md" value="subscribe">

PHP

//check to see if the submit button is pressed
if(isset($_POST['mySubmitBtn'])){
//assign the variables from the content of the input form
$email = $_POST['email'];
$name = $_POST['name'];
  //check if name is empty
  if(!isset($name) || trim($name) ==''){
  $errorMsg = "You must enter a name";
  return;
  }
  //check if email is empty
  if(!isset($email) || trim($email) ==''){
  $errorMsg = "You must enter an email";
  return;
  }
  //do something else

Upvotes: 2

Related Questions