CodeIgniter view not loading and white screen displayed instead - php

I've been debugging this piece of code for hours now without success. All I'm trying to do is get a view to load. Instead of it loading, I get a white screen and no error messages anywhere.
Here's the code from the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
//echo "we made it to the function";
public function login_form()
{
$this->load->view('login_form');
}
public function login_submit()
{
print_r( $_POST );
$this->load->model('Usermodel', 'users');
$match = $this->users->authenticate_user( $_POST['email'], $_POST['password'] );
if( $match )
{
echo "User exists in database!";
}
else
{
echo "Email or password is wrong, bretheren!";
}
}
}
The important part is the "function login_form". Here is the code for that view:
<form action="<?php base_url() ?>welcome/login_submit" method="post">
<label>
email:
<input id="email" name="email" />
</label>
<label>
password:
<input type="password" id="password" name="password" />
</label>
<input type="submit" value="Log me in!" />
</form>
This is the link I'm using in my browser to get to the page:
localhost/intranet/index.php/welcome/login_form
The code all looks fine to me and I just can't seem to figure out where the program is breaking. Does anybody have any ideas?
EDIT: I took out the shorthand but I have the same problem.

1) You're missing an echo...
<?php echo base_url() ?>
2) To use base_url(), you also need to load the URL Helper someplace.
https://www.codeigniter.com/user_guide/helpers/url_helper.html
3) I strongly recommend reading the entire documentation, including the simple demos & tutorials, before starting a CodeIgniter project...
https://www.codeigniter.com/user_guide/
4) Although you can use PHP short-tags (as per your server config), they're not recommended.
https://www.codeigniter.com/user_guide/general/styleguide.html#short-open-tags

You have to either enable the URL Helper in your configuration, or in the controller if you want to use the base_url() function:
$this->load->helper('URL');
<form action="<?php base_url() ?>welcome/login_submit" method="post">
should be
<form action="<?php echo base_url() ?>welcome/login_submit" method="post">
OR Shorthand if you configure it properly:
<form action="<?=base_url() ?>welcome/login_submit" method="post">
Noiticed the post about not being able to use shorthand with CodeIgniter and yes you can, I've just completed a project where we used shorthand. It's just how you configure your php.ini
<?= is just short for <?php echo

you are using codeigniter so why not use it's all functionality. you can create form in codeigniter like this
So you no need to use base_url or so and it is the right method to use in codeigniter.
<?php echo form_open("welcome/login_submit",array('method'=>'post')); ?>
<label>
email:
<?php echo form_input(array('name'=>'email','id'=>'email')); ?>
</label>
<label>
password:
<?php echo form_password(array('name'=>'password','id'=>'password')); ?>
</label>
<?php
echo form_button(array('type'=>'submit','value'=>'Log me in!'));
echo form_close(); ?>

Related

URL on CodeIgniter 3 ? newbie need help for tutorial

I am new to CodeIgniter 3, I am trying to create a static page from this tutorial https://codeigniter.com/userguide3/tutorial/static_pages.html
http://localhost/index.php/pages/view It is work.
but my actual is like this C:\xampp\htdocs\application\views\pages
? (and not work)
why the folder is not /views/pages ?
I am getting confuse.
Also need help ! i am new to codeIgniter 3, I want to create a simple form, but they not work in the views
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
welcome.php
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
both added the controllers
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome');
}
}
class Form extends CI_Controller {
public function index()
{
$this->load->view('form');
}
}
I tried these, but they not work.
You have not mentioned form action correctly. The form action should be look like this
action="<?php echo base_url(); ?>form"
and in the route.php add route for your action.
Example:
$route['form'] = 'form/index';
Then you can have $_POST values in your Form controller.

Form method Post not working

My problem seams to be simple, but I am struggling to get this to work.
Here's my html:
<form class="form" role="form" id="form" method="post" action="<?php echo site_url("register"); ?>" onsubmit="" >
<div class="col-md-6 col-sm-6 col-xs-12 fm">
<input type="text" class="form-control" id="email" name="email"
value="<?php if(isset($validation_errors['post_data']['email'])) echo $validation_errors['post_data']['email']; ?>"
placeholder="Email" >
</div>
<div class="text-center">
<input type="submit" id="btn" name="btn" class="btn" value="Quero ganhar um Cartão">
</div>
</form>
I made the code a bit simple, so you could read it fast.
When I submit the form to the register page, I try to see if it is a post or not like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
function Register() {
parent::__construct();
$this->load->model('register_model');
}
public function index() {
if($_POST) { echo 'post'; } else { echo 'not a post'; }
}
}
And the controller keeps printing 'not a post'.
It seams like the form only redirect to the URL and do nothing more.
Edit :
I did echo $_SERVER['REQUEST_METHOD']; in my controller and it prints " GET ", so, I guess something is wrong with the method ? I Already tried method="POST", method="post" and it keeps printing " GET ". Don't know what else to do.
add url helper in controller, then only you can access site_url() in your view page
$this->load->helper('url');
remove unnecessary onsubmit="" in your form tag
i did manage to get this going.
My redirect link didn't have "www" on it, and despite the redirect, it needed the "www" to do the POST.
Thanks to everyone that tried to help !

Base_url() is adding unnecessary segments into my url?

My base_url() is adding more bits into the url than it should be. What is wrong with my setup? I have the helper set up in config.php:
$autoload['helper'] = array('url');
My base_url() is set to: index.php/
So the problem is that when I activate this form:
<h1>sometimes haikus don't make sense refrigerator</h1>
<form action="<?php echo base_url(); ?>welcome/login_submit" method="post">
<label>
email:
<input id="email" name="email" />
</label>
<label>
password:
<input type="password" id="password" name="password" />
</label>
<input type="submit" value="Log me in!" />
</form>
The first time I access it, I'll type in: localhost/CIintranet/ and the the base_url() gets added next so it becomes: localhost/CIintranet/index.php/
I'm trying to program a login logout system though. So if the user login is incorrect, I want it to redirect back to the login page. Here's the code from the controller for that part:
public function login_submit() {
$this->load->model('LoginChecker', 'users');
//$this->load->view('login_submit');
$match = $this->users->authenticate_user( $_POST['email'], $_POST['password'] );
if( $match )
{
$this->load->view('login_submit');
echo "User exists in database!";
}
else
{
$this->load->view('login_form');
echo "<h1>Email or password is wrong, bretheren!</h1>";
}
}
But when it reloads the login_form, it adds another part into the url and the url fails. So the progression is like this:
Page loads to this url:
localhost/CIintranet/
First failed attempt (page still loads properly):
localhost/CIintranet/index.php/welcome/login_submit
Second failed attempt (page fails to load):
localhost/CIintranet/index.php/welcome/index.php/welcome/login_submit
I've been doing some research on this for a bit now and I'm noting a lot of people talking about the htaccess file in relation to this problem but nobody seems to mention specifically what fixes it or how to configure the base_url() so that it just takes care of this properly. I tried looking at the documentation but perhaps I missed something. I didn't find what I was looking for.
Any ideas?
Can you try setting the base_url to /index.php/? So it has the forward slash at the front too?
Failing that, please try: http://localhost/CIintranet/index.php/
It's usually the simple things!
if you use ... ?
<?php echo form_open('welcome/login_submit') ?>
...
<?php echo form_close(); ?>
activate helper: form

Phalcon does change views

<?php
class IndexController extends \Phalcon\Mvc\Controller {
public function indexAction(){
}
}
?>
<?php
class SignupController extends \Phalcon\Mvc\Controller {
public function indexAction(){
}
}
?>
<?php
echo "<h1>Hello!</h1>";
echo Phalcon\Tag::linkTo( "signup", "Sign Up Here!");
?>
<?php use Phalcon\Tag; ?>
<h2>Sign up using this form</h2>
<?php echo Tag::form( "signup/register" ); ?>
<p>
<label for="name">Name</label>
<?php echo Tag::textfield( "name" ); ?>
</p>
<p>
<label for="email">E-Mail</label>
<?php Tag::textfield( "email" ); ?>
</p>
<p>
<?php echo Tag::submitButton( "Register" ); ?>
</p>
</form>
I was following a tutorial on phalcon framework here but it can't get it to work. I have created the controllers for the index page and the signup page. I also created the views for the index controller and the view for the signup controller.
What happens is that when I click on the link to go to the signup page it shows the url correct which means we should be on the signup page but it shows the index view not the signup view. Basically when i click on the signup link the only thing that changes in the browser is the url but not the page.
anyone know what is going on here?
Ok,
I got this working in the end.
Make sure that (in my case Nginx) you confirm that it has been set up correctly.
The second thing to look at is the code to handle the request. I have used this:
$application = new \Phalcon\Mvc\Application();
$application->setDI($di);
if (!empty($_SERVER['REQUEST_URI'])) {
$pathInfo = $_SERVER['REQUEST_URI'];
} else {
$pathInfo = '/';
}
echo $application->handle($pathInfo)->getContent();
This isn't exactly what I wanted, but for some reason my PATH_INFO was coming out as empty, even when I have set cgi.fix_pathinfo to 1 in the php.ini
Hope this helps.

can seem to submit form in codeigniter and go to next view?

below is my 'mainview.php' view. from here iam attempting to submit and just open the next view which is called 'carerview.php'.
<form action="<?php echo base_url()?>login" method="post">
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" id="" name="" placeholder="your#email.com"></br></br>
<div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span>
<input type="password" id="" name="" placeholder="Password"></br></br>
<button type="submit" class="btn btn-primary"><i class="icon-user icon-white"></i>Sign in</button>
</div>
</div>
</form>
Iam trying to submit this is giving me issues.The Index page loads which contains the above view. but when i submit . i get requested URL not found on this server
. then if i use the full url action="application/controllers/user/login" i get a forbidden, dont have permission to access it.
my method in my controller class is just to load the next view on submit so i dont think there is an issue there . below is the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
if(!$this->isLoggedIn())
{
$this->load->view('mainview');
}
else
{
//do something
}
}
public function login()
{
$this->load->view('carerview');
}
public function isLoggedIn()
{
return false;
}
}
any help would be appreciated thanks.
if you didn't remove index.php from your URL and didn't set anything to base_url in configuration,try this
<?php echo base_url();?>index.php/user/login
localhost/your_app_folder/index.php/controller/action
Your form action is base_url(), which means is the application index route.
Try using form_open() (in the form_helper), which takes care of building the correct url:
<?php echo form_open('user/login');?>
... your form here
<?php echo form_close();?> // since I didn't see a close form tag in your form
Be careful of any routes that might intercept the request.
Alternatively, you could use site_url():
<form method="POST" action="<?php echo site_url('user/login');?>">

Categories