codeigniter - form submission mistake - php

i made a normal form for users to submit data name,email and city in form for this work i use some coding and i still not figure out where i mistaken please suggest.
routes
$route['default_controller'] = "welcome";
welcome.php in controller
public function index()
{
$this->load->view('ar_signup');
$this->load->helper('url');
}
}
ar_signup for the form in view
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Landing Page</title>
<meta charset="utf-8">
<link href="assests/css/ar/ar.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('user'); ?>
<label for="name">Name:</label>
<input type="name" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<label for="city">City:</label>
<input type="city" id="city" name="city">
<input type="submit" value="Login">
</div>
</form>
</body>
</html>
user.php in controller
<?php
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_thanks');
}
else
{
$this->load->model('users_model');
if($query = $this->Users_model->create_member())
{
$this->load->view('ar_landing');
}
}
}
user_model.php mention in models
<?php
function create_member()
{
$this->db->where('user_name', $this->input->post('username'));
$query = $this->db->get('users');
if($query->num_rows > 0){
}else{
$new_member_insert_data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'city' => $this->input->post('city'),
);
$insert = $this->db->insert('users', $new_member_insert_data);
return $insert;
}
}//create_member
}

In Your user.php controller change
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_thanks');
}
to
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_signup');
}
In your view file give this: <?php echo form_open('user/create_member'); ?>
Now hit the ur: localhost/landingpage/index.php/user/create_member

If your controller is 'welcome' then your url should be 'localhost/welcome'. Also check your mod_rewrite.
Edit
I think it will be your php installation, since you said when you removed the php codes, it was working perfectly.

Related

login form - unable to login in codeigniter

i try to login by using this simple code but after i click the login button the else choice "Invalid Username or Password!" always appear even the username and password is match
Controller :
class Auth100 extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('AuthModel100');
}
public function login(){
if($this->input->post('login') && $this->validation('login')){
$login=$this->AuthModel100->getuser($this->input->post('username'));
if($login !=NULL){
if(password_verify($this->input->post('password'), $login->password_100)){
}
}
$this->session->set_flashdata('msg','Invalid Username or Password!');
}
$this->load->view('auth100/form_login_100');
}
public function logout(){
redirect('auth100/login');
}
public function changepass(){
if($this->input->post('change') && $this->validation('change')){
redirect('welcome');
}
$this->load->view('auth100/form_password_100');
}
public function validation($type){
$this->load->library('form_validation');
if($type=='login'){
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
} else {
$this->form_validation->set_rules('oldpassword', 'Old Password', 'required');
$this->form_validation->set_rules('newpassword', 'New Password', 'required');
}
if($this->form_validation->run())
return TRUE;
else
return FALSE;
}
Model :
class AuthModel100 extends CI_Model{
public function getuser($username){
$this->db->where('username_100', $username);
return $this->db->get('users100')->row();
}
}
View :
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Catshop100 | Login</title>
</head>
<body>
<div class="container" style="width:50%;">
<h3>Kitty Fan Shop - Login</h3>
<hr>
<div style="color: red;"><?=validation_errors()?></div>
<form action="" class="form-horizontal" method="post">
<div class="form-group">
<label for="username" class="control-label col-sm-2">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username">
</div>
</div>
<div class="form-group">
<label for="password" class="control-label col-sm-2">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" name="login" value="Login" class="btn btn-default">
</div>
</div>
</form>
</div>
</body>
</html>
Database :
please help me to fix this
You're getting an error because the error code is not in the else condition, I've written the corrected code below with necessary comments. See if it helps you.
public function login(){
if($this->input->post('login') && $this->validation('login')){
$login=$this->AuthModel100->getuser($this->input->post('username'));
if($login !=NULL){
if(password_verify($this->input->post('password'), $login->password_100)){ // success
// do something -- redirect to dashboard -- your logic
}else{ // fail
$this->session->set_flashdata('msg','Invalid Username or Password!');
$this->load->view('auth100/form_login_100');
}
}else{
// do something if $login is NULL
}
}
}
Try this once, you need to handle the else condition so that method can return you the what exactly has happened.
public function login() {
if ($this->input->post('login') && $this->validation('login')) {
$login = $this->AuthModel100->getuser($this->input->post('username'));
if ($login != NULL) {
if (password_verify($this->input->post('password'), $login->password_100)) {
$this->session->set_flashdata('msg', 'Login Successful!');
} else {
$this->session->set_flashdata('msg', 'You have entered an invalid password');
}
} else {
$this->session->set_flashdata('msg', 'You have not entered username');
}
}
$this->load->view('auth100/form_login_100');
}

Codeigniter set_value not working

I'm querying the DB and sending sending a $data array from my controller to my view, where I'm using CI's form helper with set_value(field_name, default) but the data is not being loaded.
This is what I'm currently doing in my view:
<input type="hidden" id="artist-id" name="record_artist_id" value="<?php echo set_value('record_artist_id', $record_artist_id); ?>">
I thought I had to use the input helper so I tried:
<label for="record-name"><span class="required">*</span>Name:</label>
<?php
echo form_input([
'type' => 'text',
'name' => 'record_name',
'id' => 'record-name' ,
'class' => 'form-control',
'value' => set_value('record_name')
]);
?>
But still not working.
This is working though:
<input id="record-name" name="record_name" type="text" value="<?php echo (isset($record_name)) ? $record_name : ''; ?>" class="form-control">
The form helper is being loaded in autoload.php
$autoload['helper'] = array('url', 'file', 'form', 'base');
I don't know if this is related but I'm getting the view as a string and then passing it to a template, something like:
public function add_content($view, $content = NULL){
$this->content = $this->load->view($view, $content, TRUE);
return $this->content;
}
and later on in a different method:
// render content
$this->load->view('partials/content', ['content' => $this->content]);
Any idea about what I am doing wrong?
Try This
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(1);
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function test()
{
$this->load->helper('form','url');
$this->load->library("form_validation");
// echo "vijay";
$post=$this->input->post();
if($post)
{
// echo "<pre>";print_r($post);die;
$this->form_validation->set_rules('record_name', 'Record Name', 'trim|required');
$this->form_validation->set_rules('quantity', 'quantity Name', 'trim|required|numeric');
if($this->form_validation->run()==true)
{
redirect('/');
return 0;
}
}
$data['message']= validation_errors();
$this->load->view('test',$data);
}
}
View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
</div>
<p>
<?= !empty($message) ? $message : '' ?>
</p>
<form method="post">
<label for="record-name"><span class="required">*</span>Name:</label>
<?php
$record_name_input = array('type' => 'text',
'name' => 'record_name',
'id' => 'record-name',
'class' => 'form-control',
'value' => set_value('record_name')
);
echo form_input($record_name_input);
?>
<h5>Username</h5>
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
<?php
echo form_submit("submit", "submit data");
?>
</form>
</div>
</body>
</html>
Until you won't use $this->form_validation->run() til that set_value('field_name') won't work

Feedback page not responding in codeigniter

I am trying to submit a feedback after logging in the user. The Data in not being inserted in the database nor is it displaying any message if the form is left empty.
This is my controller:
function dashboard(){
$this->load->model('stud_model');
$this->form_validation->set_rules('name','Name', 'required');
$this->form_validation->set_rules('email', 'Email Id', 'required');
$this->form_validation->set_rules('feedback', 'Feedback', 'required');
$data['username'] = $this->session->userdata('username');
if ($this->form_validation->run()) {
$data = array(
'Name' => $this->input->post('name'),
'Email' => $this->input->post('email'),
'Feedback' => $this->input->post('feedback'),
);
$submit = $this->stud_model->insert_feedback($data);
if($submit) {
$this->session->set_flashdata('message', 'Feedback Successfully Submitted !');
redirect('Students/enter');
}else{
$this->session->set_flashdata('message', 'Feedback could not be Submitted !');
redirect('Students/enter');
}
}else{$this->load->View('dashboard', $data);}
}
function enter(){
if ($this->session->userdata('username') != '') {
$data['username'] = $this->session->userdata('username');
$this->load->View('dashboard', $data);
}else{
redirect('Students/log');
}
}
function logout(){
$this->session->unset_userdata('username');
redirect('Students/index');
}
This is my view:
<body>
<h1 align="center">My Dashboard</h1><br><br>
<h3 align="left">Welcome <?php echo $username ?> !</h3>
<form action="http://localhost/ci/index.php/Students/dashboard" align="center">
<?php echo validation_errors(); ?>
<br>
<input type="hidden" name="name" value="<?php echo $username ?>"><br>
Email Id : <br><input type="text" name="email"><br><br>
Feedback:<br><br>
<textarea name="feedback" rows="10" cols="50"></textarea><br><br>
<input type="submit" value="Submit"><br><br>
<?php echo $this->session->flashdata("message"); ?>
Logout
</form>
</body>
This is my Model:
function insert_feedback($data){
$this->load->database();
$this->db->insert("feedback", $data);
return $this->db->insert_id();
}
I think you use
$this->load->library('database');
Instead
$this->load->database();
Change setting on config folder in autoload.php
$autoload['libraries'] = array('database','form_validation', 'session');
$autoload['helper'] = array('url','form');
Got the Error myself.
I had forgot to add 'method="post"' to my form.

Error In Login In Codeigniter

Hi I am trying to make login code in codeigniter it gives following error
Unable to access an error message corresponding to your field name
username.
Unable to access an error message corresponding to your
field name password.
Class Verifylogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
$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_check_database');
if($this->form_validation->run()== FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('Home', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
echo $username = $this->input->post('username');
//query the database
echo $result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
View:
<html xmlns="w3.org/1999/xhtml">;
<head>
<title>Admin Panel</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open( 'verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" />
<br/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="password" name="password" />
<br/>
<input type="submit" value="Login" /> </form>
</body>
</html>

Codeigniter form validation is not showing errors

I'm new to Codegniter so go easy on me. I'm building a simple login form and I have successfully redirected to a page when the login credentials are correct. However, if I submit an empty form I get no error messages. I'm also using set_value in the form field and codeigniter does not refill what the user inputted once the form is submitted. Somehow that data is being cleared. Here are a few things i've done for clarity sake.
Auto-loaded the form_validation library
Auto-loaded form helper
Echoed validation_errors above form
account.php (controller)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Account extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user');
}
public function index() {
$this->load->view('home');
}
public function validate() {
$this->form_validation->set_rules('username', 'username', 'xss_clean|required');
$this->form_validation->set_rules('password', 'password', 'xss_clean|required|md5');
$user_data = array(
'username' => $this->input->post('username'),
'password' => MD5($this->input->post('password'))
);
if ($this->form_validation->run() == FALSE)
{
$data['title'] = "Login Fool";
$this->load->view('templates/header', $data);
$data['contents'] = $this->load->view('login_view', $data, TRUE);
$this->load->view('page', $data);
$this->load->view('templates/footer');
}
else
{
$validated = $this->user->validate($user_data['username'], $user_data['password']);
if($validated){
redirect(base_url() . 'account');
}
else{
$this->session->set_flashdata('LoginError', 'Sorry, your credentials are incorrect.');
redirect(base_url() . 'account/login');
}
}
}
public function login() {
$data['title'] = "Login Fool";
$this->load->view('templates/header', $data);
$data['contents'] = $this->load->view('login_view', NULL, TRUE);
$this->load->view('page', $data);
$this->load->view('templates/footer');
}
}
login_view.php (view)
<h1>Login Now</h1>
<?php
if(validation_errors() != false) {
echo "<div id='errors'>" . validation_errors() . "</div>" ;
}
?>
<?= ($this->session->flashdata('LoginError')) ? '<p class="LoginError">' . $this->session->flashdata('LoginError') . '</p>' : NULL ?>
<?php echo form_open('account/validate'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username" value="<?php echo set_value('username'); ?>"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password" value="<?php echo set_value('password'); ?>"/>
<br/>
<input type="submit" value="Login"/>
</form>
Any ideas why the errors won't show above the form?
Turns out my model was the culprit. It was extending CI_Controller not CI_Model. Thanks to everyone who took a look at this. This code works.

Categories