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.
Related
I have a page in view that has two parts actually which are accessed through # tags, like login#signin and login#signup. When the page loads for the first time it shows login form without having #signin without a problem.
So signin is not causing a problem as it loads at folder/login. But when I try to put folder/login#signup to load directly signup part it gives an error that there is no view login#signup.php. How to cope with this situation?
$this->load->view('workers/login#signup'); is not working.
When I don't put #signup it loads login form that is weird.
I'll expand more on my initial comments for the cause of this error, and how to fix things.
The cause of the issue
As mentioned throughout the comments, you cannot a view using an anchor point. For example, this does not work:
view('workers/login#signup'); // The #signup should not be here.
The documentation states:
Loading a View
To load a particular view file you will use the following method:
$this->load->view('name');
Where name is the name of your view file.
The name is the file is "name", not "name#signup".
Further down,
The .php file extension does not need to be specified unless you use something other than .php.
This implies, that when you use view('name'), CodeIgniter will, by default, load the file name.php. If you include a #signup in it, then CodeIgniter will not be able to find name#signup.php because that file does not exist.
Correct way to handle things
You mentioned you're using the form validation, so we need to ensure no value is lost during the transition process.
Here's a simplified explanation for how to handle it:
function login() {
// Data to be passed to the view (you may or may not already have this)
// More info: https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view
$data = array();
// Validation has failed...
$this->form_validation->run() == FALSE ) {
// Set variable to redirect to #signup upon page load
$data['redirect_to_signup'] = true;
}
// Load view with $data which contains values to be passed to the view
$this->load->view('workers/login', $data);
}
In your workers/login view file, we just need to check if the redirect_to_signup value exists. If it does exist, then we can use some simple JavaScript to scroll down the #signup form:
<?php if (isset($redirect_to_signup) && $redirect_to_signup === true): ?>
<script>
var top = document.getElementById('signup').offsetTop;
window.scrollTo(0, top);
</script>
<?php endif; ?>
Because your validation object is still valid, you can use the built-in CodeIgniter functions to preload your form elements with the set_value() helper functions. For example:
<input type="text" name="email" value="<?php echo set_value('email'); ?>">
That hopefully explains how to achieve what you're after:
Validate user submitted form; and
If there are errors, reload the form with validation messages; and
Scroll down to the #signup form on the page.
One alternative is using redirect('login#signup'), but I would not recommend this method. You would need to save your form values and validation errors to the session to show them on the next page. You also run into the issue that the user might click the refresh button and all values would be lost then.
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.
Ok so I have a basic website and the links on the page for about, contact and services load through ajax content from about.php, contact.php and services.php into a main div on my page. All works well.
The way I have it coded is when someone clicks, say, #about I have the following code:
$.address.change(function(event) {
$('#main').load('../' + event.value + '.php');
});
$('a').click(function() {
$.address.value($(this).attr('href'));
});
and in about.php I have:
<?php
// render form
require("../templates/about_form.php");
?>
All works well, however lets say a crafty user types in domain.com/about.php then they just get served up the content of about_form.php without the rest of my page around it.
Can I determine if a user does this and if so redirect the page to domain.com#about ...
Any advice appreciated.
Wrap your existing pages with
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
/*page code goes here*/
}else
{
header('Location: http://www.domain.com/#yoursection');
}
What this does is check if the request is an ajax request if not redirects the user to the proper page.
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">