I am a newbie to Codeigniter. As part of studying it, I created a form which inserts data to the database. After insertion it is not redirecting properly to the form view page (formvalidation.php).
Form Action
public function submitform()
{
$formdata['username']=$this->input->post('username');
$formdata['address']=$this->input->post('address');
$formdata['state']=$this->input->post('state');
$formdata['country']=$this->input->post('country');
$data['state']=$this->getstatearray();
$data['country']=$this->getcountries();
if($this->userprofile_model->setdata($formdata)=='success')
{
$this->load->view('formvalidation/formvalidation',$data);
}
else
{
$this->load->view('formvalidation/formvalidation',$data);
}
}
Form view page (formvalidation.php)
echo form_open('formvalidation/submitform');
echo form_input('username');
echo "<br>".form_textarea("address");
echo "<br>".form_dropdown('state',$state);
echo "<br>";
echo form_dropdown('country',$country);
echo "<br><br>".form_submit('submit','Submit');
echo form_close();
Model (userprofile_model.php)
class userprofile_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function setdata($data)
{
$this->db->insert('userprofile',$data);
if($this->db->affected_rows()>0)
{
echo "success";
}
else
{
echo "fail";
}
}
}
Now the value is getting inserted into the database. But after insertion I want to redirect it to the url http://www.example.com/codeigniter/index.php/formvalidation/. But instead, it is now redirecting to http://www.example.com/codeigniter/index.php/formvalidation/submitform where submitform is the form action method.
How can I solve this problem? I tried with putting exit; after the page redirect function. But it won't work. Please help me to fix this.
Thanks in advance.
yes it will, because there is no redirect code. When the form is submitted your submitform function is executed and in that function you are loading the view. So if you want to redirect
to something else you must use the redirect() function.
public function submitform()
{
$formdata['username']=$this->input->post('username');
$formdata['address']=$this->input->post('address');
$formdata['state']=$this->input->post('state');
$formdata['country']=$this->input->post('country');
$data['state']=$this->getstatearray();
$data['country']=$this->getcountries();
if($this->userprofile_model->setdata($formdata)=='success'){
redirect("Your URL");
}
else{
redirect("Your URL");
}
}
Also you must properly sanitize the user input. You must validate your form inputs before passing it to model. You can find more info here
First Load the view page in controller index function.
function index()
{
$this->load->view('v_bank_master');
}
Then in model page, after insertion done. give the below code.
redirect('give your controller page name here','refresh');
Your page will now redirect to the view page after insertion.
Hope it will help you...
I use redirect('yourControllerName', 'refresh').
Read more here: Redirect()
After insertion give the below code
redirect('controller_name/function_name');
Related
What I am trying to do is that, whenever the user clicks on a specific assignment, they will redirect to the detail page. The detail page will have different information according to what assignment they clicked on. However, I have no idea how to implement this feature.
My home function in Controller/project is as follows
public function home() {
/*if($this->input->cookie('login_status')=='0'){
header("location: Welcome/login");
} else{
*/
$this->load->database();
$this->load->model('scheduler_model');
$this->load->view('header.php');
$data['username'] = $this->input->cookie('username');
$data["getnames"]=$this->scheduler_model->get_users();
$this->load->view('home/homepage',$data);
$data["getassignments"]=$this->scheduler_model->get_assignment();
$data["getselectedcourses"]=$this->scheduler_model->get_select_course();
$this->load->view('home/assignments',$data);
//}
}
My assignment.php in view is as follows:
foreach($getassignments as $getassignment){
if(in_array($getassignment->courseID,$courseArray)){
if($getassignment->due>date("Y-m-d")){
?><a href="detail">
<?php
$assignName='assignment'.$i;
echo $getassignment->name," ",$getassignment->due;
//$this->input->set_cookie('assignName',$getassignment->name,time()+3600);
$assignList =array(
$assignName=>$this->input->set_cookie($assignName,$getassignment->name,time()+3600)
);
?></a>
<?php
echo "<br>";
$i=$i+1;
}
}
}?>
What I am trying to do is that, whenever the user clicks on a specific
assignment, they will redirect to the detail page. The detail page
will have different information according to what assignment they
clicked on. However, I have no idea how to implement this feature. My
home function in Controller/project is as follows
This is how it works!
You will have a method in a controller that list all assignments for your example
function home() {
$data["getassignments"]=$this->scheduler_model->get_assignment();
$this->load->view('home/homepage',$data);
}
You should have a view to listing all those assignments.
foreach($getassignments as $getassignment){
echo "<p><a href = 'controller/details/".$getassignment->id."'>".$getassignment->name."</p>"
}
you should have another method inside a controller to view that selected assignment
function detail($id){
$data["getassignment"]=$this->scheduler_model->get_assignment($id);
$this->load->view('details', $data);
}
Finally, you need a detail view to display the one you need for the selected assignment
I am Trying To Reload Contents Of Page. Through link button Filter In PHP
By Passing Some Value Through Query String To Controller Method.
The Controller Method Loads The Same Page From Where I am Passing Query String.
But The View Is Not Reloading.
But If I Call The Controller Method Some Other View It Works Fine.
here is my code
mainview.php
English
maincontroller.php
Here Is The Index Method Of Controller.
$movielanguage=$this->input->get('language');
if(!empty($movielanguage))
{
$moviedata['popularmovies']=$this->main_model-
>getmoviebylanguage($movielanguage);
}
$moviedata['allmovies']=$this->main_model->getAllMovies();
$this->load->view('home/mainview.php',$moviedata);
}
Here View Does Not Refresh Its Contents
How Can I Solve This
Hope this will help you :
First if you want a single view ,should put you code it in if else condition and second assign your data in same variable
Your code should be like this :
public function index()
{
$movielanguage=$this->input->get('language');
if(! empty($movielanguage))
{
$moviedata['movies']=$this->main_model->getmoviebylanguage($movielanguage);
}
else
{
$moviedata['movies']=$this->main_model->getAllMovies();
}
$this->load->view('home/mainview.php',$moviedata);
}
Second : and if you want to add different view for different category of movies
You can do like this :
public function index()
{
$movielanguage=$this->input->get('language');
if(! empty($movielanguage))
{
$moviedata['popularmovies']=$this->main_model->getmoviebylanguage($movielanguage);
/*popularview is just an example*/
$this->load->view('home/popularview.php',$moviedata);
}
else
{
$moviedata['allmovies']=$this->main_model->getAllMovies();
/*allview is just an example*/
$this->load->view('home/allview.php',$moviedata);
}
}
I want go to the home pages as the privilege of user that current logged in.
When i am trying to open the page that having the link it goes to the home page. What changes that i need on my code
<body>
Home
</body
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
header('location:admin_home.php');
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
header('location:home.php');
}
else
{
header('location:user_home.php');
}
}
Your function is being used inside an <a href="...">, so it's clearly supposed to return a URL (also you need to echo it)
Your current code is trying to redirect the user immediately, which won't work because you've already sent <a href=".
Try:
function home() {
if($_SESSION['privillage'] == "ADMIN") return "admin_home.php";
if($_SESSION['privillage'] == "SUPERVISOR") return "home.php";
return "user_home.php";
}
header('location is used if you want a PHP script to redirect the browser to another page. What you are trying to do is simply changing the href based on certain conditions.
function home()
{
if($_SESSION['privillage']=="ADMIN")
{
return 'admin_home.php';
}
elseif($_SESSION['privillage']=='SUPERVISOR')
{
return 'home.php';
}
else
{
return 'user_home.php';
}
}
For readability, you could consider using PHP's switch statement
may be you have to capitalize the first letter of "location" use "Location" in header('Location:user_home.php'); :)
I am trying to send the id data from a button back to one of my controllers on button click. Depending on the id sent, I want the controller function to redirect the user to different views.
Button:
<a href="/oauth/facebook" id="{{$artist->id}}" class="sign">
/oauth/facebook route:
Route::get('oauth/{provider}', 'Oauth2Controller#action_session')->before('guest');
Function action_session in Oauth2Controller:
public function action_session($provider) {
$id=Input::get('id');
if($id > 5) {
return Redirect::to('/fans');
}
else {
return Redirect::to('/artists');
}
}
I tried using ajax but it seems that the oauth/facebook route is called on button click first, before the ajax request can go through (so the $id field is blank when the controller runs). Is there any way to do this? Thank you.
You won't find the id in the input using this approach, but you could extend the route with an optional (unless you want it for all providers) parameter like this:
Route
Route::get('oauth/{provider}/{id?}', 'Oauth2Controller#action_session')->before('guest');
Controller
public function action_session($provider, $id = null) {
if ($id > 5) {
return Redirect::to('fans');
} else {
return Redirect::to('artists');
}
}
Button
...
I need little Active record help. I am trying to update page views which i store in database. Each time post() function runs, add +1 . Here is my code:
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
Please help!
Correct your code like this for example:
public function post($id) {
if(!isset($id) || (int) $id<=0){
header('Location: /blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1');
$this->db->update('entry');
}
Mark the header('Location: /blog') - you should put here your real server path to the blog.
If you use a custom redirection function, like your redirect() then check if the is an output before calling her. Header redirection only works if no other output is sent to the browser before invoking it.
public function post($id) {
if($id == NULL){
redirect('blog');
}
else{
$this->db->where('entry_id',$id);
$this->db->set('views','views+1',FALSE);
$this->db->update('entry');
FALSE turns off Active record escaping,by default it ignores +1.