On my codeigniter site I have a list of users which an admin can create content for. When creating new content for a user the url is example.com/content/new/5. 5 being the ID of the user that the content is being created for. When I submit the form and form_validation fails I reload the view but the problem is that it loses that last segment on the url with the ID number. Is there a way I can reload the view with the errors and also keep that ID in the url?
Here is my code...
if ($this->form_validation->run() == FALSE){
$this->load->view('create_content', $data); //reload if validation fails
} else {
//prepare data for the database here
}
One of the ideas is to post to the same URL e.g. use current_url()
<?php echo form_open(current_url()); ?>
https://www.codeigniter.com/user_guide/helpers/url_helper.html
Related
I have multiple views in my website, some code:
$this->load->view('templates/header');
$this->load->view('main');
$this->load->view('templates/footer');
I have an div in the main where I load another view when you click on a button.
The js code:
$("#side").load("/admin/side/loadNewContact", function(e) {
$("#side").removeClass('hide');
});
A separate function checks if the user is stil valid and logged in everytime A request is done.
When the user is not valid the system needs to go to the login page.
I have this code for it:
redirect('/', 'location');
This works. But now the problem.
When I load an page inside the main with the js, and a user is not valid. The system redirects to / (the login page). But the view what you get is loaded in the side, that is not what I want. I want that the whole system is redirected to the login instead of that specific view.
If I understand problem corectly, you would need if than else that wraps current jQuery code:
//pseudo code
var valid_user = <?php echo $valid_user;//this should be value or FALSE/NULL?>
if (valid_user) {
$("#side").load("/admin/side/loadNewContact", function(e) {
$("#side").removeClass('hide');
});
} else {
// similar behavior as an HTTP redirect
//window.location.replace("<?php echo base_url();?>");
// similar behavior as clicking on a link
//window.location.href = "/";
}
You want to check JavaScript variable at the beginning of the file/document so you can make similar conditions due the file accordingly. I am saying that in case of buttons/classes need to be shown regarding valid_user state.
I am trying to load a view from a controller but i am not able to do so? I am not getting any error but the view doesn't appear. In the search input field of my page i use ajax to get suggestions from the db for the typed in text:
function get_users($hint)
{
if($hint!="")
{
$this->db->select('email,first_name');
$this->db->like('email',$hint,'after');
$query=$this->db->get('users');
$row=$query->result_array();
$name=$row[0]['first_name'];
echo "<a href=/codeigniter/index.php/user_view/view_profile/$name >".$name."</a><br>";
}
else
{
echo "";
}
}
This sends the name of user as a link to the view. When i click the link it calls a controller which then loads the profile of the user (say whose name is contained in $name).
the code of that controller is
function view_profile($name)
{
$data['user']=$name;
$this->load->view('profile',$data);
}
A new page opens but the profile doesn't load. It's just a blank page. Can anybody help?
Why not redirect to user_view/view_profile right from get_users function?
I mean instead of echo "".$name."";
you may try to write redirect('user_view/view_profile/'.$name);
And one more thing in config/config.php enable logs (If you haven't enabled them yet) , log files are so usefull in troubleshooting.
This is simple I guess but I am new to codeigniter, so please help. I have a form page that takes data and when submitted stores that data in database. I then tried two ways to redirect back to main page:
first: $this->load->view('home');
second: redirect('/login/form/', 'refresh');
They successfully redirect or load the home page, but url is still /property/new_property which is the view for data input. also when I click refresh on the home page with url as stated above, data is resubmitted so multiple records in database. How can I redirect to home and make the url property/home?
Thanks
$this->session->set_flashdata('message', 'New Post has been added');redirect('somefunction', 'refresh')
set_flashdata after save successful will help you to destroy your submitted data and redirect will change your url.
$this->load->view() will only load the view and the url will not change .
When you use redirect() it will redirect to a different page and your url will change.
And for your solution You can select the data which you are going to insert in your model before inserting and if it exists then don't run the insert query simply redirect to a different page.
You need url helper to use
redirect('/home')
This is the correct way to redirect at CI.
http://ellislab.com/codeigniter/user_guide/helpers/url_helper.html
the second param. is not 100% required
To load that helper you can use $this->load->helper('url');
or to write it inside your autoload.php at config folder (this way it will be loaded to all page).
For example here is one simple submition page at my site.
public function save_tips(){
if($this->input->post('save_tips')){
//Form validation and contact with model
$res = $this->model->save_tips();
if(count($res['errors'])==0){ //no errors means success
redirect('/my_tips');
} else{
$errors = $res['errors'];
}
}
$data = array();
if(isset($errors)) {
$data["errors"] = $errors;
}
$this->load->view('save_tips',$data);
}
i have faced same issue with codeigniter 3.1.7 , i have just simply validate the submit button value with name , example as below
in view side
<input type="submit" value="save" name = "submit">
in controller side
if(isset($_POST['submit'])){}
I have a login controller, which is suppose to redirect to my index page when the user is valid. The redirect works, but at the index page the url is still that of the validation method ex: login/validate_login/. If i click on a link on the index page, and then try and go back in the brower history, the browser points me to the validate method and not the index page.
How do i fix this?
I have tried using redirect with both refresh and location, but both with no luck.
I suspect this is a problem with the ajax call of jQuery mobile, but i'm not sure.
Any help appreciated.
Kind regards
NOTE: I would post an image of the url at the index page, but i'm not allowed to because i'm a new user.
My validate method:
function validate_login($controller='',$method='') {
$this->load->library('form_validation');
$this->load->model('workout_model');
$this->form_validation->set_rules('ex_password','Koden','trim|required|min_length[4]|max_length[4]|callback_pw_check');
if($this->form_validation->run() == FALSE) {
$this->index();
} else {
if($query = $this->workout_model->validate()) {
$data = array(
'is_logged_in' => true,
'user_id' => $query->id,
'current_exercise' => '1'
);
$this->session->set_userdata($data);
if($controller=='' || $method=='') {
redirect("workout");
} else {
redirect($controller."/".$method);
}
} else {
$this->index();
}
}
}
Two things seem to be necessary for the URL to be correctly updated: use redirect(...) instead of method calls AND disable jQuery Mobile ajax call. Two ways of doing that: add and attribute to the link that initially points to your method,
<a href='.../validate_login/...' data-ajax='false'>...</a>
or, disable ajax calls globally by editing the settings (not tested).
There are two ways to handle this and it has nothing to do with AJAX it's the way CI does things. The quickest and easiest way is to change
$this->index();
to
redirect(index);
The way you're doing it you're not actually redirecting, you're calling the index function on the current URL which is validate_login. The problem with doing it this way is if the login fails it will still remain on the validate_login URL for the next try.
The best way to handle it is to have the actual validate_login function called from your index function in the controller rather than the form itself. So send the form back to index, have the index controller check for the form data and if true call validate_login(). That way you're never actually leaving the index page, it just handles whether or not the login form has been submitted. Solving the URL issue. I actually do this with all my pages that submit forms for any kind of validation.
when you submit via HTML form, such as login register .then you have also some kind of dashboard redirection, you need to update your form tag like this.this occurs when I'm using Codeigniter and jquery mobile use data-ajax="false"
<form id="login-box" method="post" action="<?php echo base_url('index.php/auth/login_user'); ?> " data-ajax="false">
In my CodeIgniter based website, my sidebar has the login box.
In the login controller, I have the following code:
if ($this->form_validation->run() == FALSE)
{
$this->load->view('loginfail');
}
else
{
$this->load->view('loginsuccess');
}
Currently, in case of a failed login, it redirects to loginfail view.
What I want to do is send the user back to the view he was at. But since the login box is in a sidebar which is loaded onto every other view, how can I know which view the user was at?
You could try passing it the output of current_url(), which returns the segments of the URI currently viewed; it's a function of the URL helper.
As you suggested, you could place it inside a hidden input field and retrieve the value in the controller/library which handles the authentication.