Home > Software engineering >  How can I use php function inside a href of the same class
How can I use php function inside a href of the same class

Time:02-10

I am currently making a form and I want to create an interface where I can just call in the form/page I'll like. One issue which I am currently facing is, the login() has a part where it redirects to another function.So I'll have to put the function inside the same class into a href. I have tried different ways of putting it but doesn't work.

Userinterface.php

class UserInterface
{

    public function login()
    {
        return '
        <div >
        <h1>Internship Time Tracker</h1>
        <form action="login.php" method="POST">
            <input type="hidden" name="checkUser">
            <div >
                <input type="text" required>
                <label>Username</label>
            </div>
            <div >
                <input type="password" required>
                <label>Password</label>
            </div>
            <button name="loginform_submit" >Login</button>
            <div >
            Already have an account? <a href="">Sign Up</a>
        </div>

        </form>
        </div> ';
    }


    public function register()
    {
        return '
        <div >
        <h1>Register to Internship Time Tracker</h1>        
        <form action="register.php" method="POST">
        <input type="hidden" name="checkUser">
        <div >
            <input type="text" required>
            <label>Username</label>
        </div>
        <div >
            <input type="password" required>
            <label>Password</label>
        </div>
        <button name="loginform_submit" >Register</button>
        <div >
            Already have an account? <a href="<?php this->login(); ?>">Login</a>
        </div>
    </form>
    </div>';
    }
}

The part which I am referring to in the login() function is

    <div >
    Already have an account? <a href="">Sign Up</a>
    </div>

I'll like to link to the register() but can't seem to work. Some ways I have tried:

Already have an account? <a href="<?php echo register();?>">Sign Up</a>

Already have an account? <a href="<?php register();?>">Sign Up</a>

Already have an account? <a href="<?php echo this->register();?>">Sign Up</a>

CodePudding user response:

Injecting HTML markup into a href attribut does not work.

What you can do is in your UserInterface->login():

<p>
  Do not have an account?
  <a href="?do=register"> Register </a>
</p>

And in your UserInterface->register():

<p>
  Have an account?
  <a href="?do=login"> Login </a>
</p>

And then add a constructor that checks what page is requested and calls the right method.

public function __construct()
{
  if (isset($_GET['do'])) {
    $do = $_GET['do'];
    if ($do == 'login') {
      $this->login();
    } elseif ($do == 'register') {
      $this->register();
    } else {
      $this->login();
    }
  } else {
    $this->login();
  }
}

For example in your index.php:

new UserInterface();
  • On / you will see the login form.
  • On ?do=login you will see the login form.
  • On ?do=register you will see the register form.

CodePudding user response:

What you're actually trying to achieve is being done the wrong way. It's not totally right having your markup (html) saved in your class methods. You want to create a pages as follows:

  • register.php
  • login.php

in login in php build up a markup as follows

<div >
    <h1>Internship Time Tracker</h1>
    <form action="login.php" method="POST">
        <input type="hidden" name="checkUser">
        <div >
            <input type="text" required>
            <label>Username</label>
        </div>
        <div >
            <input type="password" required>
            <label>Password</label>
        </div>
        <button name="loginform_submit" >Login</button>
        <div >
        Already have an account? <a href="register.php">Sign Up</a>
    </div>

    </form>
    </div> 

and in register build up a markup

<div >
        <h1>Internship Time Tracker</h1>
        <form action="login.php" method="POST">
            <input type="hidden" name="checkUser">
            <div >
                <input type="text" required>
                <label>Username</label>
            </div>
            <div >
                <input type="password" required>
                <label>Password</label>
            </div>
            <button name="loginform_submit" >Login</button>
            <div >
            Already have an account? <a href="login.php">Sign Up</a>
        </div>

        </form>
        </div> 

Your methods can then serve the business logic of your application, such as processing login and registration via db conn. Even if you have to refer to "this" keyword in php ensure it's preceded by a dollar sign

$this->login()
  • Related