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
Related
please help me... My controller for registration and login is not working. Whenever I input the data in either login or register it will back to register and login view and not the index/home nor the data that I input enter mysql.
I create it like when I success in inputting the data on register it will direct to login then when you login it will direct to home. Other is login, when I login it will direct to home if it can login.
Controller: Member.php
class Member extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function index() {
$this->load->view('front/login');
}
public function Login() {
$this->load->view('front/login');
}
public function Register() {
$this->load->view('front/register');
}
public function profile() {
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error","Please login first to view");
redirect('Member/Login');
}
$this->load->view('front/home');
}
}
Controller: Register.php
class Register extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function registerMember() {
//validate the data taken through the register form
$this->form_validation->set_rules('username','Username','required|is_unique[member.username]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5|min_length[6]');
$this->form_validation->set_rules('conf_password', 'Confirm Password', 'trim|required|min_length[6]|matches[password]');
if ($this->form_validation->run() == TRUE) {
//load the model to connect to the db
$this->load->model('Member_model');
$this->Member_model->insertMember();
//set message to be shown when registration is completed
$this->session->set_flashdata('success','You are registered!');
redirect('Member/Login');
} else {
$this->load->view('front/register');
}
}
}
Controller: Login.php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library(array('session', 'form_validation'));
$this->load->helper(array('url', 'form'));
$this->load->model("Member_model");
}
public function loginMember() {
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('front/login');
} else {
$this->load->model('Member_model');
$reslt = $this->Member_model->checkLogin();
if ($reslt != false) {
//set session
$username = $_POST['username'];
$password = sha1($_POST['password']);
//fetch from databse
$this->db->select('*');
$this->db->from('member');
$this->db->where(array('username' => $username , 'password' => $password));
$query = $this->db->get();
$member = $query->row();
//if use exists
if ($member->username) {
//login message
$this->session->set_flashdata("success","You are logged in");
//set session variables
$_SESSION['user_logged'] = TRUE;
$_SESSION['username'] = $member->username;
//redirect
redirect('Member/profile','refresh');
}
} else {
//wrong credentials
$this->session->set_flashdata('error','Username or Password invalid!');
redirect('Member/Login');
}
}
}
//logging out of a user
public function logoutMember() {
unset($_SESSION);
redirect('Member/Login');
}
}
Model: Member_model.php
class Member_model extends CI_Model {
public function insertMember () {
//insert data
$data = array(
//assign data into array elements
'username' => $this->input->post('username'),
'email' =>$this->input->post('email'),
'password' => sha1($this->input->post('password'))
);
//insert data to the database
$this->db->insert('member',$data);
}
public function checkLogin() {
//enter username and password
$username = $this->input->post('username',TRUE);
$password = sha1($this->input->post('password',TRUE));
//fetch data from database
$this->db->where('username',$username);
$this->db->where('password',$password);
$res = $this->db->get('member');
//check if there's a user with the above inputs
if ($res->num_rows() == 1) {
//retrieve the details of the user
return $res->result();
} else {
return false;
}
}}
View:
Register.php
<body class="background-login">
<div class="main-w3layouts wrapper">
<h1> SignUp </h1>
<div class="main-agileinfo">
<div class="agileits-top">
<form method="post" action="<?php echo site_url('register/registerMember'); ?>" >
<input class="text" type="text" id="username" name="username" placeholder="Enter a username">
<input class="text email" type="email" id="email" name="email" placeholder="Enter your email">
<input class="text" type="password" id="password" name="password" placeholder="Enter a password">
<input class="text w3lpass" type="password" id="conf_password" name="conf_password" placeholder="Confirm your password">
<div class="wthree-text">
<label class="anim">
<input type="checkbox" class="checkbox" required="">
<span>I Agree To The Terms & Conditions</span>
</label>
<div class="clear"> </div>
</div>
<input type="submit" value="SignUp">
</form>
<p>Already have an Account? Login Now!</p>
</div>
</div>
Login.php
<body class="background-login">
<div class="main-w3layouts wrapper">
<h1> SignIn </h1>
<div class="main-agileinfo">
<div class="agileits-top">
<form method="post" action="<?php echo site_url('Login/loginMember'); ?>" >
<input class="text" type="text" id="username" name="username" placeholder="Your username"><br>
<input class="text" type="password" id="password" name="password" placeholder="Your password">
<input type="submit" value="Login"/>
</form>
<p>Don't have an Account? SignUp NOW!</p>
</div>
</div>
I even chop my code into part like this, but the problem still the same... Is just like the if form_validation->run is not running and just got cut to else...
So the problem is:
When I input data it will not enter the data or redirect to another page.
*register -> it will direct to register after i submit the data
What I want is when I submit the data it will direct to login.
*login -> it will direct to login after i submit the data
What I want is when I submit the data it will direct to home.
-> result ->
I also had this case . I solved it by passing $this in to run()
if ($this->form_validation->run($this) == TRUE)
You can pass array in where condition in checkLogin function
$where_array = array('username' => $username,'password' => $password);
$this->db->where($where_array);
$res = $this->db->get('member');
Hope it help you too!
You can not use the form_open function and html form tag at the same time, so remove anyone from it and pass the whole path in the action.
If you use form_open function then please add the form_close function.
in your register and login view
<?= form_open() ?>
<form action="#" method="post">
you added this. this is wrong.at a same time you open two form you need to remove a line.and add some action to form for example
<form action="<?= base_url('yourControllerName/YourMethodname')?>" method='post'></form>
and second thing in Member.php Controller you added this line
if ($this->form_validation->run() === FALSE){
Some code
}
this is wrong you need this
if ($this->form_validation->run() == FALSE)
please check and ping me......
Make sure your base_url in config.php is not null.
In register.php remove <?=form_open('member/login')?> and <?= form_close() ?>
Instead add, <form method="post" action="<?php echo site_url('Member/register'); ?>" >
and
</form>
redirect your page:
//login
if ($this->form_validation->run() === TRUE)
{
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
$user= $this->member_model->create_user();
if($user >0){
redirect('front/home');
} else {
redirect('front/login');
}
}
you can same as in singup
I am new one to codeigniter. I need to insert the data in the database for that i create a controller on the name Home.php, below is that code;While i am running i am getting the error like Message: Undefined property: Home::$Yes.Kindly help please,thank in advance
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index()
{
$this->load->database();
$this->load->model('yes');
}
public function savedata()
{
$this->load->view('demo');
if($this->input->post('submit'))
{
$name=$this->input->post('name');
$email=$this->input->post('email');
$content=$this->input->post('content');
$this->Yes->saverecords($name,$email,$content);
echo "Records Saved Successfully";
}
}
}
Here the view pahe at name - demo.php
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data"
id="User">
<h2 align="center">Student Marks</h2>
<h3>Name:</h3>
<input type="text" name="name" />
<h3>Email ID</h3>
<input type="text" name="email" />
<h3>Content </h3>
<input type="text" name="content" />
<button type="submit" value="submit" name="submit" >Submit</button>
<button type="reset" value="Reset" >Reset</button>
</form>
</body>
</html>
here is the model page at Yes.php
<?php
class Yes extends CI_Models
{
function saverecords($name,$email,$content)
{
$query="insert into news values('$name','$email','$mobile')";
$this->db->query($query);
}
}
first of all you have to load model in your controller.
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->model('Yes');
}
public function savedata()
{
if($this->input->post('submit'))
{
insert_array=array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'content' => $this->input->post('content')
);
$data_id = $this->Yes->saverecords($insert_array);
if($data_id >0){
echo "Records Saved Successfully";
}
}
}
}
In Model
function saverecords($insert_array) {
if ($this->db->insert('news', $insert_array)) {
return $this->db->insert_id();
}
return 0;
}
Hope this will help you
In controller load database and model in __construct like this , your are adding it to only index method
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('yes');
}
public function index()
{
}
public function savedata()
{
if($this->input->post('submit'))
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$content = $this->input->post('content');
$data = ['name' => $name,'email' => $email,'content' => $content];
$insert_id = $this->yes->saverecords($data);
if ($insert_id)
{
echo "Records Saved Successfully";
}
}
$this->load->view('demo');
}
Second
Is $this->load->view('demo'); your form in savedata() method then the form url is ok but if not
Form action should redirect to savedata method because you are accessing post value to in it
<form method="post" action="<?=base_url('home/savedata');?>" enctype="multipart/form-data">
Third Your Model should be like this:
<?php
class Yes extends CI_Models
{
function saverecords($data)
{
$this->db->insert('table_name', $data);
return $this->db->insert_id();
}
}
This question already has answers here:
How to Inserting Form values into mysql database using codeigniter [closed]
(3 answers)
Closed 6 years ago.
I have a contact list on my mySQL database. The columns on my database are name, email and phone_number. Using codeigniter. I know the code I need to insert the data from the form into the database but I can't figure out where to put it into the code. I had the code below in a separate document called form.php but that just kept giving me errors anytime I hit submit. Hence why form.php is in the view.php form.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone_number'];
$sql = "INSERT INTO data (name, email, phone_number) VALUES ('$name', '$email', '$phone')";
controller.php:
<?php
class controller extends CI_Controller
{
public function index()
{
$this->load->model('model');
$data['info'] = $this->model->getData();
$this->load->view('view', $data);
}
}
?>
model.php:
<?php
class model extends CI_Model
{
public function getData()
{
$query = $this->db->get('data');
return $query->result();
}
}
?>
view.php:
<!DOCTYPE html>
<html>
<head>
<title>Contact List</title>
</head>
<body>
<form action="form.php" method="post">
<p>Name: <input type="text" name="name"></p>
<p>Email: <input type="text" name="email"></p>
<p>Phone Number: <input type="text" name="phone_number"></p>
<input type="submit" value="Submit"><br><br>
<input type="button" onClick="history.go(0)" value="Refresh">
</form>
<br>
<?php
foreach ($records as $rec) {
echo "Name: ".$rec->name."<br>";
echo "Email: ".$rec->email."<br>";
echo "Phone Number:".$rec->phone_number."<br><br>";
}
?>
</body>
</html>
Any help would be appreciated.
Your main problem is bad understanding of MVC arhitecture. If you are new, take time and explore it.
-View
<!DOCTYPE html>
<html>
<head>
<title>Contact List</title>
</head>
<body>
<?php echo form_open('controller/insert');?>
<p>Name: <input type="text" name="name"></p>
<p>Email: <input type="text" name="email"></p>
<p>Phone Number: <input type="text" name="phone_number"></p>
<input type="submit" value="Submit"><br><br>
<input type="button" onClick="history.go(0)" value="Refresh">
<?php echo form_close();?>
<br>
<?php
foreach ($info as $rec) {
echo "Name: ".$rec->name."<br>";
echo "Email: ".$rec->email."<br>";
echo "Phone Number:".$rec->phone_number."<br><br>";
}
?>
</body>
</html>
CONTROLLER
<?php
class controller extends CI_Controller
{
function __CONSTRUCT(){
parent::_CONSTRUCT();
$this->load->helper('file');
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('model');
}
public function index()
{
$this->load->model('model');
$data['info'] = $this->model->getData();
$this->load->view('view', $data);
}
public function insert() {
$data = array (
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'phone_number' => $this->input->post('phone_number')
);
$this->model->insert_data($data);
$this->load->view('view');
}
}
?>
MODEL
<?php
class model extends CI_Model
{
function __CONSTRUCT(){
parent::__CONSTRUCT();
$this->load->database('your_databse_name');
$this->load->library('db'); // optionali you could set it on autoload
}
public function getData()
{
$query = $this->db->get('data');
return $query->result();
}
public function insert_data($data){
$this->db->insert('your_data_table', $data);
}
}
?>
I think this should help you.
If I understand your question correctly, you're trying to submit a form. But you're submitting it to form.php which does not exist.
To elaborate further: In traditional PHP you would submit your form to a new page or the same page and check for $_POST values in that page. Since Codigniter is an MVC framework, you would post your data back to a controller. Which controller doesn't matter.
In your controller you load a model where you create a function to write or get data from or to the database. However, the model is less used nowadays. I'm not sure why because it keeps things organized.
After the controller has processed the data, a new view is loaded. In your case you would create a new view named form.php and load the view via the controller method.
The correct way to do this would be something like this:
<!DOCTYPE html>
<html>
<head>
<title>Contact List</title>
</head>
<body>
<form action="controller/form" method="post">
<p>Name: <input type="text" name="name"></p>
<p>Email: <input type="text" name="email"></p>
<p>Phone Number: <input type="text" name="phone_number"></p>
<input type="submit" value="Submit"><br><br>
<input type="button" onClick="history.go(0)" value="Refresh">
</form>
<br>
<?php
foreach ($records as $rec) {
echo "Name: ".$rec->name."<br>";
echo "Email: ".$rec->email."<br>";
echo "Phone Number:".$rec->phone_number."<br><br>";
}
?>
</body>
</html>
Your controller should like this:
<?php
class controller extends CI_Controller
{
public function index()
{
$this->load->model('model');
$data['info'] = $this->model->getData();
$this->load->view('view', $data);
}
public function form() {
$this->load->model('model');
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone_number' => $_POST['phone_number'] // should be exact table column name for this to work
);
$this->model->insertData($data);
$this->load->view('view', $data);
}
}
?>
And in your model:
<?php
class model extends CI_Model
{
public function getData()
{
$query = $this->db->get('data');
return $query->result();
}
public function insertData($data)
{
return $this->db->insert('data', $data);
}
}
?>
**After you copied my answer, you supplied invalid argument into foreach statement.
Should be:
foreach ($info as $rec) {
Instead of yours
foreach ($records as $rec) {
Create method in model public function insert($data)
public function insert($data){
$sql = "INSERT INTO data (name, email, phone_number) VALUES ('$data['name']', '$data['email']', '$data['phone_number']')";
$this->db->query($sql);
}
I am new to CodeIgniter, so I just follow tutorial to create login system to get myself familiar with CodeIgniter MVC. While working on it, I got HTTP Error 404.0 - Not Found on IIS after I click on the login button.
Controller
<?php
class LoginController extends CI_Controller{
public function index(){
$this->load->helper(array('form'));
$this->load->view('login');
}
public function checkLogin(){
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_verifyUser');
if($this->form_validation->run() == false){
$this->load->view('login');
}else{
redirect('HomeController/index');
}
}
public function verifyUser(){
$name = $this->input->post('username');
$pass = $this->input->post('password');
$this->load->model('LoginModel');
if($this->LoginModel->login('$name, $pass')){
return true;
}else{
$this->form_validation->set_message('verifyUser','Incorrect email or password. Please try again');
return false;
}
}
}
?>
Model
<?php
class LoginModel extends CI_Model{
function login($name, $pass){
$this->db->select('name, pass');
$this->db->from('members');
$this->db->where('name', $name);
$this->db->where('pass', $pass);
$query = $this->db->get();
if($query->num_rows == 0){
return true;
}else{
return false;
}
}
}
?>
View
<head>
<title>Login System with CI</title>
</head>
<body>
<h1>Login</h1>
<?php echo validation_errors();?>
<?php echo form_open('LoginController/checkLogin');?>
Username: </br>
<input type='text' name='username'/></br>
Password: </br>
<input type='password' name='password'/><br/><br/>
<input type='submit' name='submit' value='Login'/>
</form>
</body>
I don't know what I am missing here, any help would be greatly appreciated.
In your view, try this one:
<head>
<title>Login System with CI</title>
</head>
<body>
<h1>Login</h1>
<?php echo validation_errors();?>
<?php echo form_open('LoginController/index.php/checkLogin');?>
Username: </br>
<input type='text' name='username'/></br>
Password: </br>
<input type='password' name='password'/><br/><br/>
<input type='submit' name='submit' value='Login'/>
</form>
</body>
Try this:
Login
<?php echo validation_errors();?>
<form method="POST" action="<?php echo site_url('LoginController/checkLogin');?>">
Username: </br>
<input type='text' name='username'/></br>
Password: </br>
<input type='password' name='password'/><br/><br/>
<input type='submit' name='submit' value='Login'/>
</form>
</body>
Your controller and modal seems true. You can try this on your view ;
<?php echo validation_errors();?>
<form method="POST" action="<?php echo base_url('LoginController/checkLogin');?>">
Username: </br>
<input type='text' name='username'/></br>
Password: </br>
<input type='password' name='password'/><br/><br/>
<input type='submit' name='submit' value='Login'/>
</form>
</body>
And what is your $config['base_url'] in config.php ?
Did you change .htaccess or remove index.php ?
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