Codeigniter: Controller cannot load view - php

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.

Related

Redirect whole website to one url

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.

CakePhp: add a customized URL in $content_for_layout

I am working in cakephp application.
Here all the calls happening with Ajax. So user wants to keep one button or link, when user click on the link it will show one with contains help info of that page. I'm able load div with right content. But dynamically I'm not able to display one link (Show Help) on the page dynamically.
The reason for the "Show Help" link dynamic is based on which page loads I have to pass value to JavaScript function.
Plz help me how to add this link in $content_for_layout so that it will display dynamically in all the pages.
Step1: This is the code to create helper. Put the following code in app/views/helpers/LinkHelper.php
class LinkHelper extends AppHelper {
var $helpers = array('Html');
function showHelp() {
$url = '';//create your url dynamically here
$title = 'Show Help';
return $this->Html->link($title, $url, array('class' => 'help'));
}
}
Step2: Load the Link Helper in layout
$HelpLink = $this->Helpers->load('Link');
Step3: Call showHelp method of LinkHelper where Show Help link to be shown.
echo $HelpLink->showHelp();

codeigniter after submission if refresh submit again

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'])){}

can't show data when refreshing page with jquery-ajaxy

I try to create an ecommerce website with codeignitier. i'm using ajax to load the picture into div and jquery-ajaxy to show the url, it works well for backward and forward button, but
i got a problem when i refreshed the page, the data can't be show in the div.
is there anyone know why this happen??
controller
function tesa() {
$this->load->view("apricots.html");
}
function tesb() {
$this->load->view("bananas.html");
}
function tesc() {
$this->load->view("coconuts.html");
}
view
a href="./index.php/index_con/gambar/Kategori/Sarung/Apple/K001" class="ajaxy ajaxy-page"
Learn about Apricots
i forget to replace the state on Page Controller's "Matches" in the script ..
problem solved :D
thanks

Load the view user was at

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.

Categories