Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End php variables Reply To: php variables

#171415
__
Participant

htmlspecialchars(trim($_POST["dbname"]));

Is there a reason you are using htmlspecialchars? This function is for escaping special characters in HTML. Are you using these POST value in HTML? If you’re doing this for some other purpose (e.g., a database query) it will not work. It will cause problems and create security risks.

Even if this is the case, you should escape the value when you print it, not when you’re checking the value that was submitted.

echo $error['$dberro']="Hey you have missed the database name";

Are you trying to assign this sentence to a variable, or are you trying to print it?

echo "$error[$dberror]";

Have you tried this? It’s a syntax error.
Also, I don’t see where you have defined a variable by the name of $dberror…?

This is what it sounds like:

  • you have a list of form field inputs
  • you want to check and make sure each has a value
  • if an item is missing, assign an error message
  • print all the error messages.

Is this what you want to do? If so, here’s the most straightforward approach. It might not be the best solution for your situation, so, if not, please explain what you are trying to accomplish in more detail.

  • Make a list of the field names.
$list = [
    "dbname",
    "dbusername",
    //  etc. ...
];
  • Loop through the list to check each input from POST.
foreach( $list as $fieldName ){
    if( empty( $_POST[$fieldName] ) ){
        /*  see next step  */
    }
}
  • If an item is missing, assign an error message.
foreach( $list as $fieldName ){
    if( empty( $_POST[$fieldName] ) ){
        $error[$fieldName] = "You have missed the $fieldName field.";
    }
}
  • After that loop (and wherever you want to print the error messages), loop over the error messages and print them.
<!doctype html>
    <!--  all your html goes here  -->

<?php
if( ! empty( $error ) ){
    echo "<ul id=errorMessages>";
    foreach( $error as $message ){
        echo "<li>$message</li>";
    }
    echo "</ul>";
}
?>

    <!-- rest of your html goes here  -->