PHP If statement not working when checking an Array - php

I have this If statement to check user submitted data. The data is first turned into an array which is then passed to a function where this if statement is located.
$errCount = 0;
$errMSG ='';
// Check Name
if ($submitData['FullName'] != '') {
if (strlen ($submitData['FullName']) < 4) {
$errCount + 1;
$errMSG .='The Full Name was too short!';
}
} else {
$errCount + 1;
$errMSG .='The Full Name Field was left blank!';
}
When this is run, even with an empty string, none of the errors are being triggered.
Am I missing something?
Thanks!

You are not assigning values to $errCount varaible when incrementing it;
$errCount = 0;
$errMSG ='';
$submitData['FullName']='';
// Check Name
if ($submitData['FullName'] != '') {
if (strlen ($submitData['FullName']) < 4) {
$errCount=$errCount + 1; // or u can write $errCount++;
$errMSG .='The Full Name was too short!';
}
} else {
$errCount=$errCount + 1; // or u can write $errCount++;
$errMSG .='The Full Name Field was left blank!';
}
echo $errMSG;
echo $errCount;
This will work.....

$errCount = 0;
$errMSG ='';
// Check Name
if(isset($submitData['FullName']) && strlen ($submitData['FullName'])>0 && strlen ($submitData['FullName'])<4)
{
$errCount++;
$errMSG .='The Full Name was too short!';
}
else if($submitData['FullName'] == '')
{
$errCount++;
$errMSG .='The Full Name Field was left blank!';
}
else
{
//success
}

Related

How can I define a variable inside a function?

how I can define a variable inside a PHP function and make it reachable from another function? I want to set $error variable to "1".
This how would the code look like:
function checkthename ($name){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
function phone ($phone){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
etc.
And in end of the code there would be an another function which is checking if there is any $error variable set to 1
function checktheerror($error){
if ($error == 0){
echo "no error in this code";
}
elseif ($error == 1){
echo "there is one or multiple errors";
}
}
Thank you so much for your help!

Add input's errors in array with for

I have one problem in PHP code. It did not give me whatever output. Where my mistake?
if (isset($_POST['regBtn'])) {
$fname = strip_tags(trim($_POST['fname']));
$lname = strip_tags(trim($_POST['lname']));
$email = strip_tags(trim($_POST['email']));
$password = strip_tags(trim($_POST['password']));
$errMsg = array();
for($i=0; $i<=3;$i++) {
if (strlen($fname) < 0) {
$errMsg[$i] = "Գրեք Ձեր անունը ամբողջությամբ։";
} elseif (strlen ($lname) <0) {
$errMsg[$i] = "Գրեք Ձեր ազգանունը ամբողջությամբ";
} elseif (strlen($email) < 0) {
$errMsg[$i] = "Գրեք Ձեր էլ․ հասցեն";
} elseif (strlen($password) < 6) {
$errMsg = "Գաղտնաբառը պետք է պարունակի առնվազն 6 նիշ";
} }
var_dump ($errMsg);
}
else {
}
Several problems here, assuming that you have a form input with name="regBtn" which is required for this code to run:
strlen will not be < 0 though it may be 0. Use empty or a number greater than 0 if needed.
elseif will only execute if the if fails so just use if.
No need to loop, just use multiple if blocks and dynamically append to the error array [].
if (isset($_POST['regBtn'])) {
$fname = strip_tags(trim($_POST['fname']));
$lname = strip_tags(trim($_POST['lname']));
$email = strip_tags(trim($_POST['email']));
$password = strip_tags(trim($_POST['password']));
$errMsg = array();
if (empty($fname)) { // or use strlen < 2 or < 3 or whatever like $password
$errMsg[] = "Գրեք Ձեր անունը ամբողջությամբ։";
}
if (empty($lname)) {
$errMsg[] = "Գրեք Ձեր ազգանունը ամբողջությամբ";
}
if (empty($email)) {
$errMsg[] = "Գրեք Ձեր էլ․ հասցեն";
}
if (strlen($password) < 6) {
$errMsg[] = "Գաղտնաբառը պետք է պարունակի առնվազն 6 նիշ";
}
var_dump ($errMsg);
}
Also, you probably want to trim after strip_tags in case it leaves space(s):
$fname = trim(strip_tags($_POST['fname']));

PHP, Don't submit if there is errors

Below is my PHP code, currently, it shows all errors and etc, but if one of them are correct it will submit the form, How could I change my code so that if 1 is not correct is does not submit
<?php
$cusMsg = "";
$fNameMsg = "";
if (isset($_POST["submit"])) {
$id = $_POST["custid"];
if(empty($id)) {
$cusMsg = '<span class="error"> Field was left empty</span>';
} else if(!is_numeric($id)) {
$cusMsg = '<span class="error"> Customer ID must be numeric</span>';
} else if(strlen($id) != 6) {
$cusMsg = '<span class="error"> Customer ID must be 6 digits long</span>';
} else {
return true;
}
}
if (isset($_POST["submit"])) {
$fName = $_POST["customerfname"];
$pattern = "/^[a-zA-Z-]+$/";
if(empty($fName)) {
$fNameMsg = '<span class="error"> Field was left empty</span>';
} else if(!preg_match($pattern, $fName)) {
$fNameMsg = '<span class="error"> First name must only containt letters and hyphens</span>';
} else if(strlen($fName) > 20) {
$fNameMsg = '<span class="error"> First name must not be longer than 20 characters</span>';
} else {
return true;
}
}
}
?>
Instead of else in the last use else if and pass this
else if(!empty($fName) && preg_match($pattern, $fName) && strlen($fName) < 20){
return true;
}
It just checks all your condition using AND operator and returns true only if all conditions are met
You could set a flag variable $submit to false by default.
if (isset($_POST["submit"])) {
$submit = false; // Add this
$id = $_POST["custid"];
if (empty($id)) {
$cusMsg = '<span class="error"> Field was left empty</span>';
} else if (!is_numeric($id)) {
$cusMsg = '<span class="error"> Customer ID must be numeric</span>';
} else if(strlen($id) != 6) {
$cusMsg = '<span class="error"> Customer ID must be 6 digits long</span>';
} else {
$submit = true;
}
// Now check the value of $submit and write your code accordingly.
if ($submit) {
// Write your submit action
} else {
// Other action
}
}

PHP: Validate emailaddresses from array

I want to validate some email addresses.
The input is given by users in a textarea and the addresses are seperated by comma, like this:
email1#domain1.com, email2#domain2.com, etc. etc.
This is the script i use to check the addresses:
$emailadressen = explode(',', $_POST['uitnodigen']);
$aantal = count($emailadressen);
$i = 1;
foreach($emailadressen as $emails)
{
if(check_email_address($emails) == false) {
$Melding['omschrijving'] = '<div class="error">Error in address number '.$i.'</div>';
$i++;
}
}
And this is the function i use:
function check_email_address($email) {
// First, we check that there's one # symbol, and that the lengths are right
if (!preg_match("/^[^#]{1,64}#[^#]{1,255}$/", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of # symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("#", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
When the array only contains one emailaddress there is no problem and everything is working fine.
But when I have 2 emailaddresses at least, I get the FALSE error everytime. (Even when the emailaddresses are valid)
What do i do wrong?
I would have used
filter_var($email, FILTER_VALIDATE_EMAIL)
And:
filter_var($email, FILTER_SANITIZE_EMAIL)
Just like
foreach($emailadressen as $email)
{
if(filter_var($email, FILTER_SANITIZE_EMAIL === FALSE) ||
filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
$Melding['omschrijving'] =
'<div class="error">Error in address number '.$i.'</div>';
$i++;
}
}
I think space is getting added in your email address try by using trim
foreach($emailadressen as $emails) {
if(check_email_address(trim($emails)) == false) {
echo '<div class="error">Error in address number '.$i.' ' . $emails. '</div>';
}
else {
echo '<div class="noerror">No Error in address number '.$i. ' ' . $emails. '</div>';
}
$i++;
}
Using a filter, you should be able to simply use:
foreach($emailadressen as $emails)
{
if(!filter_var($emails, FILTER_VALIDATE_EMAIL))
{
$Melding['omschrijving'] = '<div class="error">Error in address number '.$i.'</div>';
$i++;
}
}
Without the need for any of that huge function.
This is assuming that by the time you run this foreach, the array from the post is containing the email only.

Why is the return value of this PHP function zero for a long array?

When the row['error'] is bigger than 35, the value isn't present and the result of the function is 0. Where is the problem?
<?php
if ($row['error'] == "")
{
$error = "0";
}
else
{
$error = $row['error'];
}
if ($row['error'] != "")
{
if (strlen($error) > 35)
{
$error = substr($row['error'],0,32) + "...";
}
else
{
$error = $row['error'];
}
}
?>
Change
$error = substr($row['error'],0,32) + "...";
to:
$error = substr($row['error'],0,32) . "...";
The concatenate operator in PHP isn't a plus (+) sign; it's a period (.) sign
All this code is not necessary. The second condition is redundant, and it doubles the else condition from the above. Make it all with just these few lines of code:
<?php
$error = $row['error'];
if (strlen($error) > 35) {
$error = substr($row['error'],0,32) . "...";
}
?>
Because you check:
if(strlen($error) > 35) {
}

Categories