I have installed a complete PHP system that is based on CodeIgniter. Its first view is a form with action set to
"<?php echo current_url(); ?>"
and this view is shown from a file that defines a class with some methods, a method for each subsequent view. This setup method exists in the directory: setup/controllers/Setup.php, while the views shown are in the directory: setup/views.
The code that shows the first view is:
if ($this->input->post('licence_agreed')) {
$this->session->set_tempdata('setup_step', 'requirements', $this->setup_timeout);
redirect('requirements');
}
if ( ! file_exists(VIEWPATH .'/license.php')) {
show_404();
} else {
$this->load->view('header', $data);
$this->load->view('license', $data);
$this->load->view('footer', $data);
}
The license form is defined as:
<form accept-charset="utf-8" method="POST" action="<?php echo current_url(); ?>" />
<input type="hidden" name="licence_agreed" value="1" />
<div class="terms"><?php echo lang('text_license_terms'); ?></div>
<div class="buttons">
<p class="text-right"><?php echo lang('text_license_agreed'); ?></p>
<div class="pull-right">
<button type="submit" class="btn btn-success"><?php echo lang('button_continue'); ?></button>
</div>
</div>
When I load the first view and post the form, it reloads the same view again. I have tried to debug the method above and it seems that it only executes the same if condition each time with the same result, regardless of the value of licence_agreed parameter.
I can't figure out what is the problem. It works fine on the server. But locally it reloads the same very first view all the time with no action taken.
Related
I have an issue while login . when i click on a link and if i am not loggedin than it will redirect me to login page and after success login it should redirect me requested url but it redirect me to profile page.
here is my view file:
<!--Left cols-->
<?php error_reporting(0);?>
<div class="col-lg-8 col-sm-8 col-md-8">
<div class="reg-form">
<h2>Member's Login</h2>
<title>Login</title>
<form method="post" action="<?php echo base_url();?>index.php/register_step1/login">
<div class="row">
<label for="name">Email</label>
<span><i class="icon-name"></i><input type="email" placeholder="example#gmail.com" required id="name" name="email"/></span>
</div>
<div class="row">
<label for="pass">Your Password</label>
<span><i class="icon-pass"></i><input type="password" placeholder="*************" id="pass" required name="password"/></span>
</div>
<!--input type="hidden" name="redirurl" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" /-->
<div class="row">
Forgot Password click here / Register new user
</div>
<div class="row">
<input type="submit" value="Login" name="submit"/>
</div>
</form>
</div>
</div>
<?php $this->load->view("home_template/rightbar");?>
And this is my controller :
public function login(){
$this->load->model("User_model");
$this->load->library('user_agent');
if(isset($_POST["submit"])){
//$url = $this->input->post("redirurl");
$data = $this->User_model->login();
if($data){
foreach($data as $dta){
$id = $dta->id;
$email = $dta->email;
}
$session_array = array("id"=>$id,"email"=>$email);
$this->session->set_userdata("logged_by_login",$session_array);
redirect("profile");
}
}
else{
}
$data["main_content"] = "login_view";
$this->load->view("home_template/template",$data);
}
for page redirection we used redirect() statement in codeigniter.
redirect() sends the user to the specified web page using a redirect
header statement.
redirect() statement resides in the URL helper so we need to load url helper:
$this->load->helper('url');
profile page redirection we write bellow statement and pass first parameter as "Profile" and second last parameter is "refresh/location".
redirect('profile','refresh');
The redirect function loads a local URI specified in the first
parameter of the function call and built using the options specified
in your config file.
The second parameter allows the developer to use different HTTP
commands to perform the redirect "location" or "refresh".
According to the Code Igniter documentation: "Location is faster, but
on Windows servers it can sometimes be a problem." so better to use "refresh" as second parameter
You can use redirect like this. Might be controller not get correct path,
$this->load->helper('url');
redirect(base_url('profile'));
Let me know if it not works.
change your code to
un comment your code
" /-->
change to
<!input type="hidden" name="redirurl" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
and in your controler change code as shown below
if($data){
foreach($data as $dta){
$id = $dta->id;
$email = $dta->email;
}
$session_array = array("id"=>$id,"email"=>$email);
$this->session->set_userdata("logged_by_login",$session_array);
redirect("profile");
if(!empty($this->input->post('redirurl'))){
redirect($this->input->post('redirurl'),'refresh');
}else{
redirect('profile','refresh');
}
}
I just started CodeIgnitor, that's the first time I use MVC structure though, and I have a problem that I've never seen before... It's mainly in the "form" part, but also in the database display.Also I use Xampp.
I've got a form to create an item to insert in the database, but whenever i click the submit button, things gets wrong in the url section.
My base URL is : localhost/CodeIgniter-3.1.1/ (CodeIgniter-3.1.1 is the directory that contain every php folder).
So the form page URL is : localhost/CodeIgniter-3.1.1/index.php/news/create
And when i submit, it is : localhost/CodeIgniter-3.1.1/index.php/news/localhost/CodeIgniter-3.1.1/index.php/news/create
It just repeat the entire URL after the controller (news).
I don't think it has to be with config.php, my base URL seems good, I just don't know.
Make your base url http://localhost/Codeigniter-3.1.1/index.php/ then in your <form> tag set the url like this <form method="post" action="<?= base_url('news/create') ?>">
In /application/config/config.php set $config['base_url'] like this
$config['base_url'] = http://localhost/Codeigniter-3.1.1/
In your view do either one of the following to create the <form> tag
<form method="post" action="<?= base_url('news/create'); ?>">
of if you have loaded the "Form Helper" (documented here) use this line in the view
<?php echo form_open('news/create'); ?>
It's handle by the framework, as so:
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
Also, the problem occure when I put a link to a view, like :
<a href="<?php echo 'news/'.$news_item['slug']; ?>">
Instead of building the right URL it copy itself along the bar.
I haven't used CodeIgniter in nearly a year and I seem to have forgotten a lot of the fundamentals.
I am trying to retrieve the post variables from a form and pass them into a model which inserts them into mysql.
The controller function my form submits to looks like this:
public function validation() {
$this->load->helper("form");
$this->load->model("contact_form");
$data = array(
"name" => $this->input->post("name"),
... etc. etc. ....
);
if ($this->contact_form->new_form($data)) {
$this->load->view("header");
$this->load->view("submitted");
} else echo "Sorry, there was a problem adding the form to the database.";
}
The form in the view is structured like so:
<? echo form_open("form/validation");?>
<div id="one" style="display: block;">
<h1>A Heading</h1>
<p>Some Text</p>
<p class="bold">Name: <input type="text" name="name" class="single" value="<?php echo set_value('name'); ?>"></p>
<p class="bold">Email: <input type="text" name="email" class="single" value="<?php echo set_value('email'); ?>"></p>
<p class="bold">And then some radio buttons</p>
<p> yes <input type="radio" name="registered" value="yes"> no <input type="radio" name="registered" value="no"></p>
<p class="bold">And a textarea...</p>
<textarea name="description" class="fill" value="<?php echo set_value('description'); ?>"></textarea>
next
</div>
<div id="two" style="display:none;">
<h1>Another Heading...</h1>
<p class="bold">And some more textareas</p>
<textarea name="audience" class="fill"></textarea>
... There are four divs in total with further textarea fields ...
<p class="bold"><input type="submit" name="submit" value="submit" class="center"></p>
back
</div>
<? echo form_close();?>
And finally my model is very simple:
class contact_form extends CI_Model {
public function new_form($data) {
$query = $this->db->insert("contact", $data);
if ($query) {
return true;
} else return false;
}
}
The form processes without any errors, but the data just appears as 0's in MySQL. If at any point I attempt to output the value of $_POST it returns BOOL (false), or with $this->input->post('something'); it returns NULL.
You will notice that no actual validation takes place. Initially I was using $this->form_validation->run() and getting the same results. I thought perhaps I was having trouble with the validation so I stripped it out and now I'm fairly certain my problem is that I'm not passing the $_POST variables correctly.
Can anyone explain why I am failing so hard?
I have now resolved this problem.
For some reason <? echo form_open("form/validation");?> was implementing GET and not POST. Replacing that line with <form method="post" accept-charset="utf-8" action="form/validation"/> resolved the issue.
According to the CodeIgniter documentation, by default, form_open should use POST - I have no idea why in my case it decided to use GET.
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');?>">
I'm having trouble getting the form validation library to send my form errors back to my form in this case.
I have a controller that handles uploading images called addImage.php. This controller only handles the do_upload processing. I have a view called uploadimage.php that contains the upload form and submits to /addImage/do_upload.
The upload form is loaded on the front page of my website using a template in code igniter using
<?php $this->load->view('uploadimage'); ?>
The front page controller is contained in home.php
Right now after validation fails, I'm just redirecting to the homepage which clearly doesn't load the errors back (in addImage.php)
if($this->_submit_validate() == FALSE)
{
redirect('/', 'location');
return;
}
How can I redirect to my template_front.php while keeping those errors. Can I somehow call my home.php controller from the uploadimage.php controller to do this? I've confused myself trying to explain it! If this is totally unclear, let me know and I'll try to clarify.
Per the Documentation, you are suppose to simply re-load the view file on failure.
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
a redirect generates a new server request which flushes the validation error information.
on validation failure you should reload the form. May be you want to add a button to concel uploading.
On the view, you should add some tag to show errors (there are lots of info about validation helpares) like in:
<?=form_open_multipart("/personas/savefoto", array('class' => "form3") )?>
<h3><?=$heading?></h3>
<div class="center">
<?php echo '<strong>'.mb_convert_case($record['nombre'].' '.$record['apellido1'].' '.$record['apellido2'], MB_CASE_TITLE).'</strong><br/>';
if( file_exists("fotos/e".MATRIZ."/b".$record['id'].".jpg")){
?>
<img class="foto" src="<?php echo base_url()."fotos/e".MATRIZ."/b".$record['id']?>.jpg"/>
<br/><br/>
<?php
} ?>
</div>
<div class="form-row">
<label for="imagen">Nueva imagen <br/>(jpg, gif o png)</label>
<input type="file" name="userfile" size="20" />
<br/>
<?php if(isset($error_image)) echo '<p class="error">'.$error_image.'</p>'; ?>
</div>
<div class="form-row center">
<input type="submit" value="Aceptar" />
<input type="button" value="Cancelar" onclick="location.href='/system.php/personas/admin';">
</div>
<?=form_close();?>
look for the if(isset($error_image))
You could utilize the validation_errors() function and set them to a session variable
$this->session->set_userdata(array('form_errors', validation_errors()));
then access them on your redirect page.
echo $this->session->userdata('form_errors');
$this->session->unset_userdata('form_errors'); // prevent them from being stored past use