I made a new application with just 2 views in application/views and 1 controller. I just want to go from page1 in view to page2 in view via the controller.
here is the whole code:
https://www.dropbox.com/s/cytt5azgerrbsg2/Afstudeerproject.zip?dl=0
Relevant Code
view 1:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<?= form_open(site_url('Welcome/steven')) ?>
<input type="file" name="userfile" />
<p><input type="submit" value="submit" name="submit" /></p>
<?= form_close() ?>
</div>
</div>
</body>
</html>
view 2:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<?= form_open(site_url('welcome')) ?>
<input type="file" name="userfile" />
<p><input type="submit" value="submit" name="submit" /></p>
<?= form_close() ?>
</div>
</div>
</body>
</html>
controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function steven() {
$this->load->view('welcome_secondpage');
}
}
routes.php:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
config.php:
$config['index_page'] = '';
$config['base_url'] = '';
autoload.php:
$autoload['helper'] = array('form' , 'url');
the rest of the files are all standard CodeIgnitor
In your config file assign this $config['index_page'] = 'index.php';
Related
So, I am trying to redirect the page from login.blade.php into halamanUtama.blade.php with a button in a form, but somehow it always shows page expired. I don't know why. Here is my source code:
login.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Login</title>
<!--CSS-->
<link rel="stylesheet" href="{{ asset('css/styles.css') }}" type="text/css">
</head>
<body>
<div class="login-page-frame">
<div class="header-login-page-frame">
<h3>Login</h3>
</div>
<div class="inner-login-form-frame">
<form method="post" action="./home" class="login-form">
<input type="text" placeholder="username" name="login-username">
<br>
<input type="password" placeholder="pass" name="login-password">
<br>
<button type="submit" class="btn-login">Log In</button>
</form>
</div>
</div>
</body>
</html>
web.php
Route::get('/', function () {
return view('login');
});
Route::post('/home', 'loginController#index');
loginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class loginController extends Controller
{
//
public function index(){
return view("halamanUtama");
}
}
halamanUtama.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Halaman Utama</title>
</head>
<body>
Selamat datang
</body>
</html>
Please help.
check this steps:
your login route is better to be get
check your directory in resource/views
your submit form
...
have a look on tutorials like scotch.io, etc...
I'm trying to make a form that acts as a search engine and returns results. However, the data from the user's entry is either unable to save to a session or the session cannot be passed to another file. Here is the code for the "home" search page and the "Search-Engine" results page.
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
<?php session_register(); session_start(); ?>
<?php $_GET['query'] = $_SESSION['Query']; ?>
</body>
</html>
Search-Engine.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
<?php session_start(); ?>
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I don't know the exact purpose of using Sessions in your form. But you are doing in a wrong way by starting Session in middle of page and using Sessions within the form. You can add value in Sessions in another page after submitting the form.
You can update your files in the below way:
Home.php
<html lang="en-US">
<html>
<head>
</head>
<body>
<form action="Search-Engine.php" method="GET">
<input type="text" name="query" id="query" placeholder="I'm looking for..." onkeydown = "if (event.keyCode == 13) document.getElementById('searchbtn').click()">
<input type="submit" id="searchbtn" value="Search">
</form>
</body>
</html>
Search-Engine.php
<?php session_start();
$_SESSION['Query'] = $_GET['query']; ?>
<html lang="en-US">
<html>
<head>
</head>
<body>
<div class="results">
We could not find: <?php echo $_SESSION['Query']; ?>
</div>
</body>
</html>
I have two files here as a test the first one which is this below and when I click submit it suppose to do the action on the next page but I want to know how to get retrieve athe $life variable from the action php file and put it in the normal html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
Second file which is the php file:
<?php
$life ="Yo";
?>
check this code. you need to run this code in server
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
include_once 'edit.php';
echo $life;
?>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
and this is your include edit.php
<?php
$life = 'Ok';
?>
then your first file show ok when you run this code
I have created a simple register/login system and it (I think) works well so far.
Now I want to implement some of it's functions to my site.
controllers/main.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->home();
}
public function home()
{
$this->load->view('home.php');
}
}
views/home.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
</head>
<body>
<div id="container">
<?php if ($this->session->userdata('is_logged_in')): ?>
logged in as: <?php echo $this->session->userdata('username'); ?></br>
Logout
<?php else: ?>
not logged in</br></br>
<?php echo form_open('login'); ?>
<?php echo form_error('username'); ?>
<input type="text" name="username" placeholder="Login" />
</br>
<?php echo form_error('password'); ?>
<input type="password" name="password" placeholder="Password" />
</br></br>
<div><input type="submit" value="Sign in" /></div>
<?php echo form_error('login_validation'); ?>
</form>
<?php endif ?>
</div>
</body>
</html>
Is this the right way of doing things in CI?
For controller part instead of using the home(), you can directly load the view in index() because internally you are navigating from index to home() without any specific reason.
Also the loading view doesn't require ".php" extension in your file and you can do like this:
$this->load->view('home');
Where home.php is the file located inside application/views/
For showing the error messages, you can use another optimal way by defining the validation rules(in array fromat) inside config/form_validation.php and just require below code to show all messages instead one by one
<?php echo validation_errors()?>
My controls receives some params from the user, I would like to place them inside the view I'm calling, how should I do this without splitting the view into multiple parts?
Thank you.
If I understand correctly, you should be able to do something like this for your controller
<?php
class Blog extends Controller
{
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
And something like this for your view:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
This is from the Codeignitor User guide here
In Controller:
function show_something() {
$data = array();
if ($this->input->post('some_form_field') {
$data['form_value'] = $this->input->post('some_form_field');
}
$this->load->view('some_view');
}
In View:
<html>
<head>
</head>
<body>
<?php if ($form_value): ?>
<h1><?= $form_value; ?></h1>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="some_form_field" />
<input type="submit" value="Show Value on Page" />
</form>
</body>
</html>
in controller
function show_something() {
$data = array();
if ($this->input->post('some_form_field') {
$data['form_value'] = $this->input->post('some_form_field');
}
$this->load->view('some_view', $data);
}
in view
<html>
<head>
</head>
<body>
<?php if ($form_value): ?>
<h1><? echo $form_value; ?></h1>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="some_form_field" />
<input type="submit" value="Show Value on Page" />
</form>
</body>
</html>