call controller method from a view file with MVC pattern in PHP - php

I'm writing a simple PHP website with MVC pattern (I'm not using any framework because for this website I don't want to use them).
Now I'm stuck on calling a method inside a controller from a view.
I want to create a login form, this is what I have:
My view
<?php
include_once("controller/ControllerRegLog.php");
$mycontroller = new ControllerRegLog();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="header"></div>
<div id="container">
<form id='register' action='????????' method='post'>
<fieldset >
<legend>Login</legend>
<label for='username' >UserName:</label>
<input type='text' name='username' id='username' />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' />
<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>
</div>
<div id="footer"></div>
</body>
</html>
Here is my controller (ControllerRegLog.php)
<?php
include_once("model/Model.php");
class ControllerRegLog {
public $model;
public function __construct()
{
$this->model = new Model();
}
public function logincheck()
{
$loginresponse = $this->model->checkLogin();
}
?>
And finally my model:
<?php
class Model
{
public function checkLogin()
{
// here should go a database call to see if credentials for login are valid
}
}
?>
Look at the view. I don't know what sould be written inside the action="..." in order to pass the form values to controller for checking the credentials.
I tried with bad result (page not found) with ControllerRegLog/loginCheck and <?php echo $mycontroller."/loginCheck" ?>

Related

Undefined array key when trying to get a value out of a textbox

im currently trying to get a value from a textbox but im always getting undefined array key.
<head>
<!-- Links -->
<link rel="stylesheet" href="./css/vouches.css">
</head>
<body>
<div class="content">
<form method="get"><input type="text" id="oderid" name="oderid" placeholder="oderid / invoiceid"></form>
<form method="post"><input type="submit" name="button1"class="submitbtn" value="Button1" /></form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_GET['orderid'];
echo $id;
}
?>
</div>
</body>
</html>
in the part is where the stuff happens. I hope i can get some help, im really new to php
When clicking the button it should echo $id, better said the textbox input
Don't use both POST and GET; pick one.. Like this:
<form method="post">
<input type="text" id="oderid" name="orderid" placeholder="oderid / invoiceid" />
<input type="submit" name="button1" class="submitbtn" value="Button1" />
</form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_POST['orderid'];
echo $id;
}
?>
Also take care of your spelling, orderid is not the same as oderid.

Redirect to PHP class after HTML form submit

I'm trying to execute a PHP class after a HTML form submit but my browser display that it couldn't open the specify address.
Here's my form:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>Logins</title>
</head>
<body>
<div id='login_form'>
<form action='<?php echo base_url();?>Login/process' method='post' name='process'>
<h2>User Login</h2>
<br />
<label for='username'>UsernameTest</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div>
</body>
</html>
And here's my PHP's class:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load our view to be displayed
// to the user
$this->load->view('login_view');
}
public function process(){
// Load the model
$this->load->model('login_model');
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$this->index();
}else{
// If user did validate,
// Send them to members area
redirect('home');
}
}
}
?>
The class is in the Controller folder and the form is in the View folder
Thank's
If you configured your site as something like http://www.mywebsite.org in your config file, then the line:
<form action='<?php echo base_url();?>Login/process' method='post' name='process'>
Will render as:
<form action='http://www.mywebsite.orgLogin/process' method='post' name='process'>
which is obviously not what you intended because http://www.mywebsite.orgLogin/process isn't even a valid URL. To render URLs in CodeIgniter, use site_url():
<form action='<?php echo site_url('Login/process');?>' method='post' name='process'>
Aside for this, there may be two other problems you might want to check out:
the /Login/process page doesn't exist
the /home page doesn't exist
First echo or dump your base_url() to see what is there, or use site_url() .I usually use '/' as /controller_name/function and please use 'login' not 'Login'.
It works when I write manually the following URL:
localhost:8888/CodeIgniter-3.0.0/index.php/Login/process
But doesn't work using the form redirection.
<form action='<?php echo site_url('Login/process/');?>' method='post' name='process'>
Thanks

How do we send php page attribute values to a php class method

How do we send php page attribute values to a php class method?. I want to get php attribute values into a php class method. following is the way i tried.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<form id="test" method="post" action="testPhp.php">
<label>Name</label>
<input type="text" name="n1" id="name" />
<input type="submit" value="ADD NAME">
</form>
</body>
</html>
Now I want to get this name value in method in separate php file
testPhp.php
<?php
class getParam{
public function getFormParm(){
$name = $_POST['n1'];
}
}
?>
You have no valid action declared as the forms action. Try $_SERVER['self'] as the form action, like so:
<body>
<form id="test" method="post" action="<?php echo $_SERVER['self']?>">
<label>Name</label>
<input type="text" name="n1" id="name" />
<input type="submit" name='submit' value="ADD NAME">
</form>
</body>
And to get the result of this, do the following:
<?php
echo $_POST['n1'];
?>
The way you have the class set up, you aren't actually doing anything with the class. Nothing is being executed. You can do something like this:
<?php
class getParam{
public function getFormParm(){
$name = $_POST['n1'];
return $name;
}
}
// This is the part that you are missing
$getParam = new getParam();
echo $getParam->getFormParm();

PHP CodeIgniter custom login form

I am trying to create a login form using CodeIgniter in PHP. I have implemented the functionality, working okay, however I cannot manage to style my for with a custom stylesheet.
Here is the code I have for the login view:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="UTF-8" />
<title>
DTI Manager
</title>
<link rel="stylesheet" type="text/css" href="/DTIManager/assets/css/style.css" />
</head>
<body>
<div id="wrapper">
<?php echo validation_errors();?>
<?php $attributes = array('class' => 'LoginController', 'id' => 'login-form');
echo form_open('LoginController/checkLogin', $attributes);?>
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Autentificare</h1>
<span>Pentru a accesa informatii despre comenzi transporturi trebuie sa fiti autentificat.</span>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="Utilizator" />
<div class="user-icon"></div>
<input name="password" type="password" class="input password" placeholder="Parola" />
<div class="pass-icon"></div>
</div>
<div class="footer">
<input type="submit" name="submit" value="Login" class="button"/>
</div>
</form>
</div>
<div class="gradient"></div>
</body>
</html>
And this is my LoginController class:
class LoginController extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('login');
}
public function checkLogin() {
$this->form_validation->set_rules('username', 'Utilizator', 'required');
$this->form_validation->set_rules('password', 'Parola', 'required|callback_verifyUser');
if ($this->form_validation->run() == false) {
$this->load->view('login');
} else {
redirect('HomeController/index');
}
}
public function verifyUser() {
$name = $this->input->post('username');
$pass = $this->input->post('password');
$this->load->model('LoginModel');
if ($this->LoginModel->login($name, $pass)) {
return true;
} else {
$this->form_validation->set_message('verifyUser', 'Incorrect Email or Password. Please try again.');
return false;
}
}
}
I tried adding the attribute to the form_open function the ID of my form but it wasn't successful, the buttons and fields are still not styled accordingly.
Any hints are highly appreciated.
Try echoing the base_url() in front of your linked css files.
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>DTIManager/assets/css/style.css" />
Of course, if you use an .htaccess you should ignore the folder that has the assets in your project with a proper rewrite rule.
RewriteCond $1 !^(index\.php|DTIManager|assets|robots\.txt|error\.html)

dynamically changing target of a form

I am trying to display my login inside an iframe which loads in the middle of the start page with gray backgroud, however i have a problem dealing with the target of the iframe, things should work as follows:
1- if user logged in successfully home page should load in the parent window
2- if error, error message should be displayed in the iframe itself.
my code:
<div id="cover5" onclick="javascript:cover5();">
<iframe name="warning" src="SignIn.php" id="warning" onclick="javascript:cover5();">
</iframe>
</div>
SignIn.php
<html>
<head>
<script type='text/javascript'>
function valid(){
if(document.getElementById('username').value.toString().length>=1 || document.getElementById('password').value.toString().length>=1){
return true;
}
else{
alert("The Combination of username and password are Incorrect !");
return false;
}
}
</script>
<script type='text/javascript'>
function ChangeTarget() {
document.getElementById("login").prop("target", "_parent")
}
</script>
<script type='text/javascript'>
function ChangeText(text) {
document.getElementById("label1").innerHTML= text;
}
</script>
<title></title>
</head>
<body>
<form name="login" id='login' target='_self' method='post' accept-charset='UTF-8'>
<fieldset>
<legend>SignIn</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Email:</label>
<input type='text' name='email' id='username' maxlength="50" /> <br />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" /> <br />
<br /> <label id="label1" style="color: red"></label> <br />
<input type='submit' name='Submit' value='Login'/> <br />
<a href='resetPassword1.php'> Forgot/Reset Password? Click Here</a>
</fieldset>
</form>
however, the target is not changed by method ChangeTarget() which is called inisde php code, so the problem now is that everything load inside the iframe or everything inside the parent window. anyone knows how to solve the problem?
server side script:
mysql_select_db("mydb",$check);
$email = $_POST['email']; //username that will be submit from a form
$password = $_POST['password']; //password that will be submit from a form
// if(mysql_num_rows($temp_privileges_id)== 1) //if the id found that means that the user is already assigned to a conference
// {
echo '<script type="text/javascript">',
'ChangeTarget();',
'</script>';
header('Location: main.php'); // transefer to home page with variable username
}
There is no method prop. Either set the value of the target attribute directly
document.getElementById("login").target = "_parent";
or use setAttribute
document.getElementById("login").setAttribute("target", "_parent");
.prop() is a jQuery function. If you're going to do it in jQuery, do this:
$("#login").prop("target", "_parent");
If you're not going to do it in jQuery, do it like this in straight JavaScript:
document.getElementById("login").target = "_parent";

Categories