0

I'm new to PHP, trying to replicate a JavaBeans/JSF assignment...

I need to call a method in class Service that verify user name and password.

I have this php in the top of my index.php:

<?php 
include("Service.php");
$service = new Service();
if($_POST) {
    if (!$service->checkUser($_POST['name'], $_POST['password'])) {
        $error = "Username and password incorrect";
        echo $error;
    }
}?>

Mu form action calls it's own document:

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

I have 2 input fields (name, password) like this:

My Service.php class has the function checkUser. It returns a String:

public function checkUser($name, $password) {
    foreach($this->users as $user) {
        if ($user->getName() == $name && $user->getPassword() == $password) {
            return "welcome.php";
        }
        else
            return "error.php";
    }
}

How can I use that String to use as navigation when form is submitted?

2 Answers 2

3

Few issues:

1) $POST should be $_POST

2) Does checkUser() return a string containing the form action? As that's what your code is currently doing..?

It looks to me like you should be posting to a PHP script, probably the same filename that you are already in, and then call the checkUser method:

Form:

<form action="/" method="post">
..
</form>

Then in the PHP side:

<?php
if ($service->checkUser($_POST['name'], $_POST['password']))
{
  // do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to move the checkUser function out of my index.php and into a separate class Service.php. Yes, checkUser returns a string with a filename: welcome.php (if OK) or error.php (if not logged in). So how do i call the function checkUser in Service-php in the action attrib.
0

Left the action property empty and on the top of the page write the following code

  <?PHP
       if($_POST){
               if (!$service->checkUser($_POST['name'], $_POST['password'])){
                     $error = "Username and password incorrect";
               }
       }
  ?>

include your function here, And echo this error above the form

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.