Better html form validation method

I visited the site and saw what something object Oriented and compozer am not to good with those things for now.

Still don’t know how to add a dependency

Another huge trouble on my mind is the myths that adding html and php in same form is a bad practice, i have always used same file for html form and processing it too in php. But after i came across it i started using include () therefore makng me to have two files even when am using the redirect method.

Also when you want to pull data from database and list it out in html then using external file may not work perfectly.

Is it a bad pratice to have my html form and processing code in same file?

Please i really need to learn more about this…

  1. Dont chain anything to the REQUEST METHOD check.
  2. Just check if hidden field isset. It doesn’t need a value
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
    if (isset($_POST['hiddenFieldForm1']))
        {
        //Do something
        }

    if (isset($_POST['hiddenFieldForm2']))
        {
        //Do something
        }
    }
?>
** HTML HERE **

No, it is better practice. Php at the top of the page, Html at the bottom.

1 Like

@benanamen thanks a whole lot this explanation was so perfect.

This is exactly how my page will then look

show_header()

php process form code

html form code

show_footer()

No.

php process form code

show_header()

html form code

show_footer()

Okay perfect, i will follow your lead, but the show header() is an include that adds my php header file, and the file header.php has some codes inside it not just a HTML header files.

Something like search database and get users account number and echo, so most users details are been echoed programatically in the header.php

And this is the code for show_header().

function show_header() {
include (PATH . 'folder/header.php');
}

So i use that function in all my pages especially user portal or dashboard
Do you still think is okay to call it below process code?

Sounds like you have too much going on in the header file. It shouldn’t be more than what would normally go between the head tag. This should be the extent of the header.php along with any CSS and JS includes. You probably need at least one other include for the rest.

<!DOCTYPE HTML> 

<html>

<head>
  <title></title>
</head>

<body>

Okay thanks, i will split it, i copied that method from a bootstrap backadmin theme, most functions are called in the header, like scroll prices, market watch, user profile pic

So if i split it i will still need to include it inside the header file to fit in to the layout else i will still need it below the header as to output certain things, so instead of running two files.

In some header div you can see things like <?php echo show_pic()?>

Then the html tag is

If you put the header before the form processing code, and that header code sends any output to the browser, you will have issues with doing things like header redirects when you come to process the form.

1 Like

Very true i encountered that problem, i noticed that in php there is no one cap fits all syndrome, i have used these same method in all my projects with redirect and even calling return; in my form validation without problems, and everything works fine. But it does work fine when am using include to add the php process form in the php page that has the html form and everything works perfect, the return; stops the other php codes from processing and looks like a perfect option, then if validation are okay the complete code to submit to the database is called and after which the header() is been called to clean up the form by redirecting to a query called success.

But i needed a more professional way and most tutorials here are perfect but seems that i must use a separate methods for my files. Am trying to adopt this methods but am facing header issues.
Am facing return call removes every other html code below it especially the footer i called using get_footer()

And more especially the use of a header that puts users profile pic, account number and certain other information since is a backend dashboard looks like a treat to me, so i must use a plain header which maybe plain html file with all my css and js src.
Then i will create another file called subheader which will be php files that is included in the my dashboard head

So that i will start my programing from there leaving the header or any div in it blank. Bcs @benanamen insisted i had many things going on in the header which am beginning to notice it.

That my old method works doesn’t make it perfect thats why am here for a redress

I would suggest that you try to shift to a more OOP oriented approach. As you are starting to discover, organization is key. And OOP does not have to be a huge scary thing.

I am going to throw some code at you with a single class that does the processing stuff in one method (i.e. function) and the rendering in another. The render itself is broken into one section that basically does the html page outline and another section which does the content. In this case a very simple form. It’s an example I have used before so it does not exactly match your code but the general idea is the same.

If it looks like gibberish to you then fine. Ignore it and move on. If you can kind of see what is going on then it you might be able to expand on it. And if you start with this sort of approach it become easier to break things up into smaller units and multiple files.

<?php
# user_action.php
$action = new UserAction();
$action->run();

class UserAction
{
    public function run()
    {
        // Fake post handling
        if (isset($_POST['btn_save'])) {
            var_dump($_POST);
            die();
        }
        // Fake form data
        $currentUserId = 42; // $_SESSION['id'];
        $rowUser = [
            'id' => 42,
            'status' => 33,
            'first_name' => 'Tom <"> Jerry',
        ];
        // Render and send the html
        $html = $this->render($currentUserId,$rowUser);
        echo $html;
    }
    // Utility for html output escaping
    protected function escape(string $value) : string
    {
        return htmlspecialchars($value, ENT_COMPAT);
    }
    private function render($currentUserId,$rowUser)
    {
        $html = <<<EOT
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Test</title>
</head>
<body>
{$this->renderContent($currentUserId,$rowUser)}
</body>
</html>
EOT;
        return $html;
    }
    private function renderContent($currentUserId,$rowUser)
    {
        return <<<EOT
    <form method="POST">
        <input type="text" name="first_name" value="{$this->escape($rowUser['first_name'])}" >
        <input type="submit" name="btn_save" value="Save">
    </form>
EOT;

    }
}
1 Like

OOP is great am currently taking a course on it, using one class to build a whole lot of squeence but àm notx gona use it in this project until am perfect using it.

Thanks .

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.