How to check if HTML forms are empty or not with PHP
I was wondering if there is a way to check that the html text inputs are empty, and if so, execute the specific PHP code I provide you.
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:
- My PHP code is the correct syntax which I believe is not
- Is it possible to check if the input is empty and if it is empty execute PHP code (in particular
'$msg_to_user'
) - It is possible to configure my code so that when the user clicks the submit button and the fields are BOTH empty to
$msg_to_user
execute the code
Thank you in advance!
source to share
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 think I answered questions A and B. How about question C - it's up to your architecture. For example, if you want to submit a form from a page to yourself (I mean a index.php
file has a form with an action = 'index.php' or an empty action), then you can do it this way (just a rough approach, definitely not the best one):
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>
source to share
and. No syntax.
Q. Yes
C. Yes, first you need to give your button a property name so that it can be picked up by PHP. You also need to assign the $ email and $ name variables to the $ _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
source to share