简体   繁体   中英

setting form action as function in external php file

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?

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;
    }
}

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();
}

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 - ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM