Gayan
Gayan

Reputation: 2935

How to give form action to PHP Class

I have written class call Users -> path: class/users.php

and create login form in index.php

<form action="" method="post">
    <p>Email: <input type="email" name="user_email"/></p>
    <p>Password: <input type="password" name="user_password" /> </p>
    <p> <input type="submit" value="Login" name="login" /> </p>
</form>

as form action I want to call that class -> userLogin() methode how to do that,

Upvotes: 3

Views: 3362

Answers (1)

Fluffeh
Fluffeh

Reputation: 33512

You can't make a form directly call a function.

The form action - or page that the request is being sent to on the other hand - that can call a function within a class - assuming that of course there is an instance of that class.

So for example, if you have a page with this - make the form action this page:

<?php

    if($_REQUEST['login'])
    {
        $users= new users();
        $users->userLogin();
    }

?>

At that point your class can go off and reference the global variables on its own - having said that, it's probably cleaner to actually send it the data it needs so that classes don't have to use globals:

<?php

    if($_REQUEST['login'])
    {
        $users= new users();
        $users->userLogin($_REQUEST);
    }

?>

This way, the class is certain that it has the information that is needed - and as an added bonus you can still call the userLogin() function even if you wanted to call if differently - like from a session, or cookie or through the help of a flock of flying unicorns - you get the idea.

The code in the form action doesn't even have to be much more than this. A user verification function will often send a redirect to the user - where to might depend on whether the user is successfully logged in or not.

Upvotes: 3

Related Questions