Controler
//class News
public function update($slug)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['news_item']=$this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/update', $data);
//$this->load->view('save',$save);
$this->load->view('templates/footer');
}
Model new_model.php
//class News_model
public function get_news($slug = FALSE)
{
if($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
public function update_news($slug)
{
$query=$this->db->where('slug', $slug);
$this->db->update('news' ,$query);
return $query->row_array();
}
in update.php view file code given below..
view update.php file
<h2>Update New Item</h2>
<?php echo form_open('news/update') ?>
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $news_item['title']; ?>" readonly/><br>
<label for="text">Text</label>
<textarea name="text" cols="35" rows="16"><?php echo $news_item['text'];?></textarea><br>
save
</form>
data will be fetch but problemb is that when i click on "save" link page not found error generatos why?
how can call this view save.php file..
Change
save
to
<input type="submit" value="save" />
Here you have used link in-place of submit button.
When you use submit button it will post/get data on url that is there in the form action.
Here you can use :
<?php echo form_submit('mysubmit', 'Submit Post!'); ?>
It will produce...
<input type="submit" name="mysubmit" value="Submit Post!" />
For more details : Form Helper
Related
Guys Im new to php and codeigniitor. Im trying to make a post application but it's not working please help.
here is my controller function
public function status(){
$this->load->library('form_validation');
$this->form_validation->set_rules('post', 'Post Something', 'required');
if ($this->form_validation->run() == FALSE) {
$user = $this->session->userdata('user');
$data1['user'] = $user;
$this->load->view('pages/panel',$data1);
}
else{
$this->load->model('reg_model');
$formArray1 = array();
$formArray1['post'] = $this->input->post('post');
$formArray1['posted_by'] = $user['ID'];
$this->reg_model->create1($formArray);
$this->session->set_flashdata('msg', 'Your account is created successfully');
redirect(base_url().'index.php/Registration/panel');
}
here is my form
<?php echo validation_errors();?>
<form action = " <?php echo base_url()?>registration/status" name= "registrationForm1" id="registrationForm1" method = "post">
<label class =" form-control" for="post">Post Something</label></br>
<textarea style =" width:100%" class="form-control" rows="3" id="post" placeholder="Your message..."></textarea>
<p class ="invalid-feedback"><?php echo form_error('posts');?></p></br>
<button type="submit" class="btn btn-primary btn-block">Post</button>
</form>
my model:
public function create1($formArray1)
{
$this->db->insert('posts', $formArray1);
}
You should use $formArray1 not $formArray
$formArray1 = array();
$formArray1['post'] = $this->input->post('post');
$formArray1['posted_by'] = $user['ID'];
$this->reg_model->create1($formArray1);
and in Model:-
public function create1($formArray1)
{
$this->db->insert('users', $formArray1);
}
I am a new student and I am learning Codeigniter. I have a form view and I want to submit name, email, sdt to add.php to check but I can't connect to add.php.
This is my form
<!DOCTYPE html>
<html>
<body>
<h2>Form Dang Ky</h2>
<form name="submitbd" action="add.php/check" method="POST">
Name<br>
<input type="text" name="name" value="">
<br>
Email:<br>
<input type="text" name="email" value="">
<br>
SDT<br>
<input type="text" name="sdt" value="">
<br>
<br>
<input type="submit" value="Submit">
<br>
</form>
</body>
</html>
and I try to call file add.php in controller, but I got **
404 error
Add.php file in controller
<?php
class Add extends IC_Controller{
$data = array(
'$_name' => $this->input->post('name'),
'$email' => $this->input->post('email'),
'$sdt'=>$this ->input-> post('sdt')
);
public function _contruct(){
parent::_contruct();
}
public function check(){
$this->load->database();
$a=$this->db->query("select email from info where
email=.$array($email)");
if($a==""){
echo "ok";
}
echo "not ok";
}
}
?>
Hope this will help you
Your form tag should be like this :
<form name="submitbd" action="<?=site_url('add/check');?>" method="POST">
..........
</form>
Your Controller Add.php should be like this :
class Add extends CI_Controller
{
public function _contruct(){
parent::_contruct();
$this->load->database();
$this->load->helper('url');
}
public function check()
{
$_name = $this->input->post('name');
$email = $this->input->post('email');
$sdt = $this->input->post('sdt');
$sql = "select email from info where email='{$email}'";
$query = $this->db->query($sql);
if($query->num_rows() > 0)
{
echo "ok";
}
else
{
echo "not ok";
}
}
}
See for more: https://www.codeigniter.com/user_guide/general/index.html
I want to display current user info from database after login.
Here is my code.
Controller 1 (Welcome)
public function getValues() {
$this->load->model('display_model');
$result=$this->display_model->authenticateUser();
if($result){
$this->session->set_userdata('id', $result['id']);
redirect('/member/home/');
}
else
redirect('/welcome/');
Controller 2 (member)
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
if($this->session->userdata('id')==NULL)
{
redirect('/welcome/');
}
}
public function home() {
$this->load->view('member_home');
}
Model
public function authenticateUser(){
$name =$_POST['name'];
$password=$_POST['password'];
$where=array('name'=>$name,
'password'=>$password);
return $this->db->select('name, password, id')
->from('member')
->where($where)
->get()->row_array();
View 1 (login_view)
<h1>Member Login Form</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('Welcome/getValues'); ?>
Username: <br/>
<input type="text" name="name" /> <br>
Password: <br>
<input type="password" name="password" /> <br>
<input type="submit" value="Login" name="submit"/>
</form>
View 2: After login view (member_home)
<?php
foreach($result as $row){
?>
<li><?php echo $row['first_name'].' '.$row['last_name'];?>
<br/><strong>Email: </strong><?php echo $row['email'];?>
</li>
Now error shows
Message: Undefined variable: result
How to solve it Need Help.. Thanks in Advance
You are not passing the $result value to your home view update the code as follows:
Controller 1:
public function getValues() {
$this->load->model('display_model');
$result=$this->display_model->authenticateUser();
if($result){
$this->session->set_userdata('id', $result['id']);
redirect(member/home/$result['id']);
}
else
redirect('/welcome/');
Controller 2:
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
if($this->session->userdata('id')==NULL)
{
redirect('/welcome/');
}
}
public function home($id = '') {
$data['id'] = $id;
$this->load->view('member_home',$data);
}
NOTE:
you must change the query result from row_array() to result_array() as you are using foreach on the view
When i login to the admin page i got the error 404 Page Not Found.The page you requested was not found.
I am tring to login to the admin page in the following code:
Controller:
public function index()
{
$data = $this->data;
$this->load->view('admin/index.php',$data);
}
public function login()
{
$data = $this->data;
$username=$this->input->post("username");
$password=$this->input->post("password");
print_r($username); die();
$this->load->model('admin/grocery_crud_model');
$res=$this->grocery_crud_model->check_login($username,$password);
if($res==1)
{
$this->load->view('admin/main.php',$data);
}
else
{
$data['error']="Invalid Username or Password";
$this->load->view('admin/index.php',$data);
}
}
public function home()
{
$data = $this->data;
$this->load->view('admin/index.php',$data);
}
Grocery crud model:
public function check_login($username,$password)
{
$query=$this->db->query(" select * from tbl_register where username='".$username."' and password='".$password."'");
return $query->num_rows() ;
}
View:
<form name="login" action="<?php echo site_url('admin/admin/login');?>" method="post">
<div class="col-md2"></div>
<div class="col-md8 lgmid">
<div class="error" style="margin-left:10px; margin-top:35px;"><?php //echo $error;?></div>
<div class="lg"><label><b>Username</b></label> <input type="text" name="username" class="textbox"></div>
<div class="lg"><label><b>Password</b></label> <input style="margin-left:2px" type="password" name="password" class="textbox"><br/></div>
<div align="center"><input type="submit" class="lgbtn" value="Submit" /></div>
</div>
<div class="col-md2"></div>
</form>
Here in model the table used is tbl_register. Is there any problem with the model .Please provide solution for this issue.
You cant load view like this
$this->load->view('admin/index.php',$data); //wrong
you have to load without file extension
$this->load->view('admin/index',$data); //correct
so folder structure would be
application
controllers
model
view
admin
index
and in Model
public function check_login($username,$password)
{
$query=$this->db->query(" select * from tbl_register where username='$username' and password='$password'");
$result = $query->result_array();
$count = count($result);
return $count;
}
EDIT 01
and in form action
Change this to
action="<?php echo site_url('admin/admin/login');?>"
this
action="<?php echo base_url().'admin/admin/login'?>"
in order to use base_url() load url library in autoload.php
remove this
$config['base_url'] = 'http://localhost/ASoft/Projects/AutoGadi2Amy';
$config['base_url'] = '';
just keep empty
this is my users controller
function login(){
$data['error']=0;
if($_POST){
$this->load->model('user');
$username=$this->input->post('username', true);
$password=$this->input->post('password', true);
$type=$this->input->post('user_type', true);
$user=$this->user->login($username,$password,$type);
if(!$user){
$data['error']=1;
} else {
$this->session->set_userdata('userID', $user['userID']);
$this->session->set_userdata('user_type', $user['user_type']);
redirect(base_url(). 'posts');
}
$this->load->view('header');
$this->load->view('login',$data);
$this->load->view('footer');
}
}
this is my user model
function login($username,$password){
$where=array(
'username'=>$username,
'password'=>sha1($password),
'user_type'=>$type
);
$this->db->select()->from('users')->where($where);
$query=$this->db->get();
return $query->first_row('array');
}
this is my view
<?php if($error==1){ ?>
<p>Your Identity is wrong</p>
<?php } ?>
<form action="<?php echo base_url()?>users/login" method="POST">
<p>User Name: <input type="text" name="username" /></p>
<p>Password: <input type="password" name="password" /></p>
<p><input type="submit" value="login" /></p>
</form>
not the problem is on the view the (form view'login') is not displaying if i only remove
the if($_post) from my user controller then it works but is there any place that i have don mistake on my controller of my model or view,
my table is like ('username','password','email','user_type')
regards, in advance for any kind of tips
You are loading your view page inside the if($_POST) condition. At first, if($_POST) is false, the view is not loading.
So, you need to correct it.
Just Use:
function login(){
$data['error']=0;
$this->load->model('user');
$username=$this->input->post('username', true);
$password=$this->input->post('password', true);
$type=$this->input->post('user_type', true);
$user=$this->user->login($username,$password,$type);
if(!$user){
$data['error']=1;
} else {
$this->session->set_userdata('userID', $user['userID']);
$this->session->set_userdata('user_type', $user['user_type']);
redirect(base_url(). 'posts');
}
$this->load->view('header');
$this->load->view('login',$data);
$this->load->view('footer');
}
And it will work perfectly.