How to link to another page when clicking button - codeigniter

login.php (controller)
class login extends CI_Controller {
public function index()
{
$this->load->view('login');
}
public function Click()
{
$action = $this->input->post('register'); // $_POST['start']; also works.
if($action)
{
$this->load->view('register');
}
}
}
login.php(views)
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<input type="submit" id="submit" class="form-control" value="Buat Akun" name="register" >
</div>
</form>
how can I change my view to register.php after clicking register button. The error is I keep back to login page after clicking register.

You can try this :
In Login controller :
public function register()
{
$this->load->view('register');
}
In view:
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<a href="<?php echo base_url('login/register');?>" id="submit" class="form-control" name="register" >Register</a>
</div>
</form>

You need to enable mod_rewrite module in apache
Please follow below mention step to enable mod_rewrite module:
1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.
4) Now restart the apache server.
5) You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.

You can try this :
In Login Controller:
class Login extends CI_Controller {
public function index()
{
if($this->input->post('login') == 'Login'){
$this->load->view('login');
}else{
$this->register();
}
}
public function register(){
$this->load->view('register');
}
}
In View :

Related

getting an error while submitting, stating 'object not found!'

I am very new to this laravel framework.
And I am stuck here. I got an error like 'Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.0.2o PHP/5.6.36'
that's my form:i am just trying to insert data in database
#extends('layouts.app')
#section('content')
<form method="posts" action="/posts">
<input type="text" name="title" placeholder="enter the title">
<input type="submit" name="submit">
</form>
#stop
</body>
</html>
and my controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller
{
public function index() {
return "lets see whether it is working or not".$id;
}
}
public function create() {
return view('posts.create');
}
public function show(){
//return view();
}
public function edit(){
//
}
public function update(Request $Request,$id){
//
}
public function destroy($id){
//
}
public function store(Request $request){
Post::create($request->all());
}
}
and finally my route:
Route::resource('posts','PostController');
Change the method to 'post' and the action to {{ route('posts.store') }}. Also be sure to add the #csrf blade directive in your from to include the csrf token.
So your form will look like:
<form method="post" action="{{ route('posts.store') }}">
#csrf
<input type="text" name="title" placeholder="enter the title">
<input type="submit" name="submit">
</form>

Laravel - Upload to Existing Model Record

Maybe it's because I'm tired, but I can't seem to get a simple upload working for one of my models.
On the show details page of my customers (who are NOT users) model, I have a simple form where a user can upload the logo of the customer.
The form:
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
The Controller:
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
}
I know the issue is that I haven't defined $customer in the controller since the file does actually store itself in the correct folder after I click submit, but it does not hit the database at all.
Update
The current customer details url:
http://localhost:8000/customers/i/dsdado9a98w78721
The web definition for the post route:
Route::post('/customers/i/{customer}', 'CustomerController#logoUpload');
You have to create a hidden field in your form that contains the customer id and then use it in your controller to update it with the new file path, here is an example:
View
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<!-- customer_id field -->
<input type="hidden" name="customer_id" value="{{$customer->id}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
Controller
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
// get customer
$customer = Customer::find($request->customer_id);
$customer->car_logo = $path;
$customer->save();
return back();
}
}
You have some options, here is a simple one, with only adjusting that controller method:
public function logoUpload(Request $request, $customer)
{
$customer = Customer::where('url_string', $customer)->firstOrFail();
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
...
}
We have added a parameter to the method signature to accept the route parameter. We then find the model via url_string matching that parameter.
You also could setup route model binding as well to do the resolving of that model based on the route parameter for you.

Post variables in Codeigniter

I have a view in Codeigniter that is a form written in HTML directly.
<form action="http://localhost/index.php/cindice/mostrar" mehtod="post">
<label for="usuario" id="usr">Usuario</label>
<input type="text" name="usuario" /><br /><br />
<label for="contrasenya" id="ctr">Contraseña</label>
<input type="password" name="contrasenya" id="ctr" />
<label for="acceder"></label><br /><br />
<input type="submit" name="acceder" id="bacceder" value="Entrar" />
</form>
The form has two fields: usuario (user) and contraseña (password).
And it is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cindice extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('indice');
}
public function mostrar()
{
$mostrar=$this->input->post('usuario');
print_r($_POST);
echo "<br />";
}
}
?>
When I load the page localhost/index/cindice and I write a username and a password. But the result of print_r($_POST) command is array(), what it means that any value was saved in the form.
How can I fix it?
I think it is empty because you have spelled the the method attribute wrong in your opening form tag. You have put
<form action="http://localhost/index.php/cindice/mostrar" mehtod="post">
When it should be
<form action="http://localhost/index.php/cindice/mostrar" method="post">
Be mindful if you have CSRF switched on in your config - if it is then you need to use the codeigniter form helper and form_open() so that the hidden security value is added.

Loading page gives an error in CodeIgniter

When I login, it gives me "error page not found". It's not loading the controller function.
This is my view file:
<form name="loginform" action="welcome/login" method="post">
Enter User Name:
<input type="text" name="uname">
Enter Password
<input type="password" name="pass">
<input type="submit" name="login" value="Login">
This is my login controller file:
class Login extends Controller {
function Login()
{
parent::Controller();
}
function login()
{
$this->load->view('welcome_message');
}
}
This is my welcome controller file:
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->model('Loginmodel');
$this->load->view('welcome_message');
}
public function login()
{
$this->load->model('Loginmodel','login');
$info = $this->logn->login();
$this->load->view('welcome_message',$info);
}
}
Can you give me any idea what is wrong?
The code you provided seems fine, so I would guess it has to do with this:
<form name="loginform" action="welcome/login" method="post">
The path is relative, which is only going to work if there are no url segments present. If the current URL is /hello/world, you will end up posting to /hello/world/welcome/login
Try a full URL or absolute path. Example:
<form name="loginform" action="/welcome/login" method="post">
<!-- ^^^ Note the leading forward slash -->
Or use base_url() for the full url:
<form action="<?php echo base_url(); ?>welcome/login">
You may also be interested in the form_open() function which will do this automatically.
<?php echo form_open('welcome/login'); ?>
The only other thing I can see would have to do with whatever $this->login->login(); does. If you can confirm what your URL actually is when you see the "page not found" error, that would be a great help.

Codeigniter: simple form function

I'm stuck writing a simple form...I feel dumb.
Here's my controller:
function welcome_message(){
//Update welcome message
$id = $this->session->userdata('id');
$profile['welcome_message'] = $this->input->post('welcome_message');
$this->db->update('be_user_profiles',$profile, array('user_id' => $id));
}
And the html:
<?php print form_open('home/welcome_message')?>
<input type="checkbox" value="0" checked="false">Don't show me this again</input>
<p>
<input class="button submit" type="submit" class="close-box" value="Close" />
</p>
<?php print form_close()?>
Edit
I simply need it to submit to a private function and return to the home page (page submitted from).
You have to name your input...?
<input type="checkbox" value="0" name="welcome_message" checked="false">
Don't show me this again
</input>
EDIT: You can't submit to a private function via the URL (in a default CI installation, anyway).
Do it like this:
function welcome_message()
{
if(isset($_POST['welcome_message']))
{
$this->_my_private_function();
}
}
private function _my_private_function()
{
// do your thing with the $_POST data
redirect('your/uri', 'location');
}
But you really don't have to redirect if your returning to the same page (controller method) that called the private function in the first place. Would be a waste.

Resources