Setting up a form as a function in an external php file

I am new to PHP (somewhat), and I looked around and cannot find any information that answers exactly my questions, so here it is:

Let's say I declare a form with two 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 that I tried to set the action as the "logIn ()" function, which I already included in the header of this file.

In an external php file, I have 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 it, do it", I'm just painting a picture here).

So basically, I want the form to be subordinate to this particular function, is this possible? Am I doing something fundamentally wrong here?

+4
source share
3 answers

As others have said, you cannot send a message to a function automatically, but you can dynamically decide what to do on the PHP side, depending on what form is submitted with the PHP code. One way is to define your logic using hidden input so that you can handle various actions on one page, for example:

 <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; } } 
+3
source

No, you submit the form to the page and 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(); } 
+2
source

To answer your question, yes, you are doing something wrong. However, it is easily fixed.

The action on the form is where it submits the form - that is, the page for sending the request. Since you say that your code is "at the top of the page," you will want to submit the form back to its page. Thus, you can either put the full URL of the page into action, or simply leave it blank:

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

To process the view, PHP is not able to directly call the function from the client code, however you can process the request in a more convenient way of processing requests by sending a hidden field with the current "task" ".

For example, in an HTML form, try adding:

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

Then in your 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 checks if the form has been submitted by checking if the task field has been published. Then it checks the value. If it is logIn , the logIn() function is logIn() . Alternatively, if it is logOut , the logOut() function will be called.

To create a release form, you will correct action accordingly and add a hidden field to that described above, but with the value logOut .

+2
source

All Articles