6

I'm new to PHP (somewhat), and i've had a look around and can't find any information which caters exactly to my questions, so here it is;

Let's say i declare a form, with 2 fields and a submit button;

<form name = "tryLogin" action = "logIn()" method = "post">
            Username: <input type = "text" name = "username" placeholder = "username.."><br>
            Password: <input type = "text" name = "password" placeholder = "password.."><br>
            <input type = "submit" value = "Submit">
</form>

Here you can see i've tried to set the action as a function "logIn()", which i've included already in the header of this file.

In an external php file i've got the following;

function logIn()
{
if($_POST['username'] == "shane" && $_POST['password'] == "shane")
{
    $_SESSION['loggedIn'] = '1';
    $_SESSION['user'] = $_POST['username'];
}

header ("Location: /index.php");
}

function logOut()
{
$_SESSION['loggedIn'] = '0';
header ("Location: /index.php");
}

(Ignore any "y'shouldn't do this, do that", i'm just painting a picture here).

So basically i want the form to submit to that particular function, is that possible? Am i doing something fundamentally wrong here?

2
  • Yes you can't do that. It's not sent in the response body and you sever all ties with the server as soon as it sends you the response Commented Dec 31, 2012 at 2:19
  • You need to code a login page and use that in form action. Get the login detail on this page then you can call login function from that login page. Commented Dec 31, 2012 at 2:21

3 Answers 3

6

As others have said, you can't direct a post to a function automatically, but you can dynamically decide what to do on the PHP side depending on which form is submitted with PHP code. One way is to define your logic with a hidden input so you can handle different actions on the same page, like this:

<form name="tryLogin" action="index.php" method="post">
            <input type="hidden" name="action" value="login" />
            Username: <input type="text" name="username" placeholder="username.."><br />
            Password: <input type="text" name="password" placeholder="password.."><br />
            <input type="submit" value="Submit">
</form>

<form name="otherform" action="index.php" method="post">
            <input type="hidden" name="action" value="otheraction" />
            Type something: <input type="text" name="something"><br />
            <input type="submit" value="Submit">
</form>

and then in your PHP:

if (isset($_POST['action'])) {
    switch($_POST['action']) {
    case 'login':
        login();
        break;
    case 'otheraction':
        dosomethingelse();
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

No you submit the form to a page and you run your function if the form is submitted:

Html:

<form action="index.php" method="post">

PHP (index.php):

if ($_SERVER['REQUEST_METHOD'] == "POST"){
    // Run your function
    login();
}

Comments

2

To directly answer your question, yes, you are doing something wrong. However, it's easily fixable.

The action on the form is where it submits the form - i.e. the page to send the request. As you're saying that your code is "at the top of the page", you'll want to submit the form back to the page it's on. So, you could either put the full URL of the page in the action or just leave it blank:

<form name = "tryLogin" action = "" method = "post">

To handle the submission, PHP doesn't have a way to directly call a function from client-side code, however, you can process the request in a more request-handling way by sending a hidden field with the current "task".

For instance, in the HTML form, try adding:

<input type="hidden" name="task" value="logIn" />

Then, in the PHP code, try adding this:

if (isset($_POST['task'])) {
    if ($_POST['task'] == 'logIn') {
        // the user is trying to log in; call the logIn() function
        logIn();
    } else if ($_POST['task'] == 'logOut') {
        // the user is trying to log out; call the logOut() function
        logOut();
    }
}

This code will check if the form has been submitted by checking if the task field has been posted. Then, it will check the value. If it's logIn, the logIn() function will be called. Alternatively, if it's logOut, the logOut() function will be called.

To create the log-out form, you would adjust the action accordingly and add a hidden field to that one like the above, but with the value of logOut.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.