What's new

Anu kaya kulang sa code ko

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
parehas lumalabas yun if ko at elseif ko

if (isset($_SESSION["signup_error"]['payeename']) && isset($_SESSION['errors_signup']['payeename_taken']) || isset($_SESSION['errors_signup']['empty_input']))
{
$error = $_SESSION["errors_signup"];
foreach($error as $errors => $value)
{
if( $errors === 'empty_input'){
echo '<div class="form-group mt-1 ">';
echo '<label for="PayeeName">Payee Name:</label> <small id="emailHelp" class="form-text text-muted"> Note: Registered your name base on your account</small>';
echo '<input type="text" class="form-control form-control-sm rounded-3" name="payeename" value="" id="pname" placeholder="" >';
echo '<div class="error">' . $value .'</div>';
echo '</div>';

}elseif( $errors === 'payeename_taken' )
{
echo '<div class="form-group mt-1 ">';
echo '<label for="PayeeName">Payee Name:</label> <small id="emailHelp" class="form-text text-muted"> Note: Registered your name base on your account</small>';
echo '<input type="text" class="form-control form-control-sm rounded-3" name="payeename" value="' . $_SESSION["signup_error"]['payeename'] . '" id="pname" placeholder="" >';
echo '<div class="error">' . $value .'</div>';
echo '</div>';
}
}
// unset($_SESSION["errors_signup"]);
}else
{
echo '<div class="form-group mt-1 ">';
echo '<label for="PayeeName">Payee Name:</label> <small id="emailHelp" class="form-text text-muted"> Note: Registered your name base on your account</small>';
echo '<input type="text" class="form-control form-control-sm rounded-3" name="payeename" value="" id="pname" placeholder="Payee Name" >';
echo '</div>';
}
 
Based on your code, it seems that the if statement is not working as expected because you are using the logical OR operator (||) instead of the logical AND operator (&&) in your condition. This causes the elseif block to execute even if only one of the conditions is true.

To fix this issue, you should modify your if statement by separating the conditions for the 'payeename_taken' and 'empty_input' errors with parentheses. Here's the updated code:

PHP:
if (
    (isset($_SESSION["signup_error"]['payeename']) && isset($_SESSION['errors_signup']['payeename_taken'])) ||
    isset($_SESSION['errors_signup']['empty_input'])
) {
    $error = $_SESSION["errors_signup"];
    foreach ($error as $errors => $value) {
        if ($errors === 'empty_input') {
            // code for empty_input error
        } elseif ($errors === 'payeename_taken') {
            // code for payeename_taken error
        }
    }
} else {
    // code when none of the conditions are true
}

Also, you might want to consider removing the commented unset($_SESSION["errors_signup"]); line, as it seems unnecessary in this context.
 
The code you provided seems correct, and the elseif block should work properly. However, there could be issues with the data stored in your $_SESSION variables or the condition checks themselves that prevent the expected behavior.

Here are some troubleshooting steps you can take:

1. Check $_SESSION Variables: Ensure that $_SESSION["signup_error"]['payeename'], $_SESSION['errors_signup']['payeename_taken'], and $_SESSION['errors_signup']['empty_input'] contain the expected values. You can use var_dump or print_r to inspect the session variables.

2. Debugging: Add echo statements or use a debugger to print out the values of $errors and $value within the foreach loop to see what values are being evaluated in the elseif condition. This will help you understand why the elseif block may not be executing as expected.

3. Code Flow: Make sure that the control flow of your application leads to this code block when you expect it to. For example, ensure that the session variables are being set correctly before reaching this code.

4. Conditional Logic: Verify that the values of $errors match the expected strings 'empty_input' and 'payeename_taken'. Be mindful of case sensitivity.

5. Error Reporting: Check if there are any error messages or warnings in your PHP error log that could provide insights into the issue.

If you are still encountering problems after these checks, please provide more information about the specific issue or any error messages you are seeing, and I'll be happy to assist you further.
PHP:
<?php
if (isset($_SESSION["signup_error"]['payeename']) &&
    (isset($_SESSION['errors_signup']['payeename_taken']) || isset($_SESSION['errors_signup']['empty_input']))) {
    $error = $_SESSION["errors_signup"];
    foreach ($error as $errors => $value) {
        if ($errors === 'empty_input') {
            echo '<div class="form-group mt-1 ">';
            echo '<label for="PayeeName">Payee Name:</label> <small id="emailHelp" class="form-text text-muted"> Note: Registered your name base on your account</small>';
            echo '<input type="text" class="form-control form-control-sm rounded-3" name="payeename" value="" id="pname" placeholder="" >';
            echo '<div class="error">' . $value .'</div>';
            echo '</div>';
        } elseif ($errors === 'payeename_taken') {
            echo '<div class="form-group mt-1 ">';
            echo '<label for="PayeeName">Payee Name:</label> <small id="emailHelp" class="form-text text-muted"> Note: Registered your name base on your account</small>';
            echo '<input type="text" class="form-control form-control-sm rounded-3" name="payeename" value="' . $_SESSION["signup_error"]['payeename'] . '" id="pname" placeholder="" >';
            echo '<div class="error">' . $value .'</div>';
            echo '</div>';
        }
    }
    // unset($_SESSION["errors_signup"]);
} else {
    echo '<div class="form-group mt-1 ">';
    echo '<label for="PayeeName">Payee Name:</label> <small id="emailHelp" class="form-text text-muted"> Note: Registered your name base on your account</small>';
    echo '<input type="text" class="form-control form-control-sm rounded-3" name="payeename" value="" id="pname" placeholder="Payee Name" >';
    echo '</div>';
}
?>
 

Similar threads

Back
Top