PHP: check if any posted vars are empty - form: all fields required
Something like this:
// Required field names
$required = array('login', 'password', 'confirm', 'name', 'phone', 'email');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
} else {
echo "Proceed...";
}
How to check if any fields in a form are empty in php
your form is missing the method...
<form name="registrationform" action="register.php" method="post"> //here
anywyas to check the posted data u can use isset()..
Determine if a variable is set and is not NULL
if(!isset($firstname) || trim($firstname) == '')
{
echo "You did not fill out the required fields.";
}
How to check if HTML forms are empty or not with PHP
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>
Shortcut for checking if all $_POST fields are filled in php
You can use array_filter and compare both counts
if(count(array_filter($_POST))!=count($_POST)){
echo "Something is empty";
}
php check if any of 30 form fields is empty
Try this:
foreach($_POST as $post)
{
if( !empty($post) )
{
// your code here
}
}
PHP If statement to test if form fields are empty
You should do it like this:
if(isset($_POST['submit'])) {
foreach($_POST as $val) {
if(trim($val) == '' || empty($val)) {
//empty field, do something
...
header("Location: /add.php?error=empty_fields");
die();
}
}
//all seems to be ok
...
}
To prevent SQL injection, use PDO and you'll forget about it.
Related Topics
Read Each Line of Txt File to New Array Element
Using PHP'S Swiftmailer With Gmail
Fatal Error: Call to a Member Function Prepare() on Null
How to Get Rid of Eval-Base64_Decode Like PHP Virus Files
Set Cookie Wih Js, Read With PHP Problem
Sending Multiple Iphone Push Notifications + Apns + PHP
Eloquent Orm Code Hinting in PHPstorm
Displaying an Image Stored in a MySQL Blob
How to Use PHPexcel to Read Data and Insert into Database
PHP: If Internet Explorer 6, 7, 8 , or 9
Yii2 Htaccess - How to Hide Frontend/Web and Backend/Web Completely
Multiple MySQL Insert Statements in One Query PHP
Laravel Update Model With Unique Validation Rule For Attribute
How to Convert Many Statement MySQL to Laravel Eloquent
PHP X86 How to Get Filesize of ≫ 2 Gb File Without External Program