form_open() is not working peoperly - php

I have a controller named : user_login_controller.php and view : user_login_view.php
code of user_login_view.php
<?php echo form_open('user_login_controller/login', 'class="form-horizontal" id="userloginform"');?>
<fieldset>
<legend>Student Login</legend>
<div class="form-group">
<div class="col-lg-6 col-md-6 col-xs-10">
<?php echo form_input(['name'=>'rno','class'=>'form-control','placeholder'=>'Roll Number'])?>
</div>
</div>
<div class="form-group">
<div class="col-lg-6 col-md-6 col-xs-10">
<?php echo form_password(['name'=>'pwd','class'=>'form-control','placeholder'=>'Password'])?>
</div>
</div>
<div class="col-lg-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<?php echo form_reset(['name'=>'Reset','value'=>'Cancel','class'=>'btn btn-primary'])?>
<?php echo form_submit(['name'=>'Submit','value'=>'Login','class'=>'btn btn-primary'])?>
</div>
</div>
</fieldset>
</form>
On submit button click I m sending control to user_login_controller/login
here is user_login_controller.php
<?php
class User_login_controller extends MY_Controller
{
public function index()
{
$this->load->view('user/user_login_view');
}
public function login()
{
echo "User login function";
}
}
?>
but it's given me 404 error.however i have both files
and when I m going through url(http://localhost:8090/project/user_login_controller/login) :then its working. and i have loaded all the necessary helpers.
$autoload['helper'] = array('url','form');
what to do now?

You should set config['base_url'] in application/config/config.php
$config['base_url'] = 'http://localhost:8090/project/';
as form_open consider url to be set to http://localhost:80/project/ or http://[::1]/project/ if you didn't set config['base_url']

Follow below steps and it will sort-out the problem,
1. View
when you opening a form tag, if you want to add several attributes, use this method.
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'userloginform');
echo form_open('user_login_controller/login', $attributes);
?>
It's clean and error less.
2. Config.php
Go to application/config/config.php file, and set
$config['base_url'] = 'http://localhost:8090/your_project_folder_name/';
3. routes.php
Go to application/config/routes.php file, and set
$route['user_login_controller/(:any)'] = "user_login_controller/$1";
$route['user_login_controller'] = "user_login_controller";
This will work. Try it and let me know.

Related

Object Not found in xampp server in code igniter?

i am new to code igniter and i am developing simple login system for that i am using xampp , i uploaded code igniter in folder code/ and the following are the codes in mvc
controller login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
//load the login model
$this->load->model('login_model');
}
public function index()
{
//get the posted values
$username = $this->input->post("txt_username");
$password = $this->input->post("txt_password");
//set validations
$this->form_validation->set_rules("txt_username", "Username", "trim|required");
$this->form_validation->set_rules("txt_password", "Password", "trim|required");
if ($this->form_validation->run() == FALSE)
{
//validation fails
$this->load->view('login_view');
}
else
{
//validation succeeds
if ($this->input->post('btn_login') == "Login")
{
//check if username and password is correct
$usr_result = $this->login_model->get_user($username, $password);
if ($usr_result > 0) //active user record is present
{
//set the session variables
$sessiondata = array(
'username' => $username,
'loginuser' => TRUE
);
$this->session->set_userdata($sessiondata);
redirect("index");
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
redirect('login/index');
}
}
else
{
redirect('login/index');
}
}
}
}?>
MOdel is login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//get the username & password from tbl_usrs
function get_user($usr, $pwd)
{
$sql = "select * from tbl_usrs where username = '" . $usr . "' and password = '" . md5($pwd) . "' and status = 'active'";
$query = $this->db->query($sql);
return $query->num_rows();
}
}?>
And View is login_view
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
<!--link the bootstrap css file-->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
.colbox {
margin-left: 0px;
margin-right: 0px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-6 col-sm-6">
<h1>COLORS</h1>
</div>
<div class="col-lg-6 col-sm-6">
<ul class="nav nav-pills pull-right" style="margin-top:20px">
<li class="active">Login</li>
<li>Signup</li>
</ul>
</div>
</div>
</div>
<hr/>
<div class="container">
<div class="row">
<div class="col-lg-4 col-sm-4 well">
<?php
$attributes = array("class" => "form-horizontal", "id" => "loginform", "name" => "loginform");
echo form_open("login/index", $attributes);?>
<fieldset>
<legend>Login</legend>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="txt_username" class="control-label">Username</label>
</div>
<div class="col-lg-8 col-sm-8">
<input class="form-control" id="txt_username" name="txt_username" placeholder="Username" type="text" value="<?php echo set_value('txt_username'); ?>" />
<span class="text-danger"><?php echo form_error('txt_username'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="txt_password" class="control-label">Password</label>
</div>
<div class="col-lg-8 col-sm-8">
<input class="form-control" id="txt_password" name="txt_password" placeholder="Password" type="password" value="<?php echo set_value('txt_password'); ?>" />
<span class="text-danger"><?php echo form_error('txt_password'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-12 col-sm-12 text-center">
<input id="btn_login" name="btn_login" type="submit" class="btn btn-default" value="Login" />
<input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-default" value="Cancel" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
<!--load jQuery library-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--load bootstrap.js-->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>
IN config i used
$config['index_page'] = '';
and in routes i used
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
while accessing localhost/code/ it is working fine but when click login button it is going to url http://localhost/code/localhost/login/index and showing object not found ERROR 404
Open application/config/config.php and set your base_url(). E.g: $config['base_url'] = 'http://localhost/code/';
Create .htaccess file under /code folder (Where application and system folder is) like below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
create .htaccess file in \code\
as
RewriteEngine On
RewriteBase /code/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
and in config/config.php
$config['base_url'] = 'http://localhost/code';
Hope it will help you.
I got same error as shown below. I am using php/codeigniter. I have defined $config['base_url'] = 'http://localhost/******'; in my config file. still it was showing same issue.
Object not found! The requested URL was not found on this server. If
you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
How could I solve this.
Step 1: 1st i checked httpd.conf file. there mod_rewrite was imported.
Step 2: checked file is located at actual location or not.
Step 3: checked spelling mistake in redirection or anchor tag.
Step 4: checked $config['base_url'] variable in application/config.php file.
Step 5: checked $config['index_page'] variable in application/config.ph file. here was my actual problem. I set it to blank and there was something wrong with my mod_rewrite module. when I put $config['index_page']='index.php' it started working for me.

Form action with class and method adds to existing url

I have sign in form which has URL - "http://localhost/ci/signin". After click on submit, it goes to - "http://localhost/ci/login/userLogin", which affects hyperlink opening problem because it searches for that page in 'login', which is actually on "http://localhost/ci/home".
How to solve this problem?
my signin form page code is:
<?php echo form_open('login/userLogin'); ?
<div class="top-margin">
<label>Email <span class="text-danger">*</span></label>
<?= form_input(['name'=>'email','class'=>'form-control']); ?>
</div>
<div class="top-margin">
<label>Password <span class="text-danger">*</span></label>
<?= form_password(['name'=>'password','class'=>'form-control']); ?>
</div>
<hr>
<div class="row">
<div class="col-lg-8">
<b>Forgot password?</b>
</div>
<div class="col-lg-4 text-right">
<?= form_submit(['name'=>'submit','value'=>'Sign in','class'=>'btn btn-action']); ?>
</div>
</div>
</form>
My "login" controller code is:
class Login extends MY_Controller
{
public function userLogin()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
if($this->form_validation->run() == FALSE)
{
$data['title'] = ucfirst('signin'); // Capitalize the first letter
$this->load->view('user/header', $data);
$this->load->view('user/nav', $data);
$this->load->view('user/signin', $data);
$this->load->view('user/footer', $data);
}
else
{
echo "Success";
}
}
}
?>
Routes:
$route['(:any)'] = 'user/view/$1';
$route['default_controller'] = 'user/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
If I understand your question and your code correctly (never used codeigniter), you have the form loading from the login directory.
<?php echo form_open('login/userLogin'); ?
What happens when you try:
<?php echo form_open('home/userLogin'); ?
Hope this helps! :)
I don't use CodeIgnite myself, but it probably has helper functions which can correctly output an URL.
In Laravel it is:
url('this/url/is/not/added/to/the/current/url');
Even when you're at 'localhost:8000/somepage' this outputs:
"localhost:8000/this/url/is/not/added/to/the/current/url"
It is a PHP (helper)function, so output it in PHP.
Yes. I found solution of my problem. It was my mistake actually. Hyperlinks was not working because i didn't write full address for that hyperlinks.
Previously, My hyperlinks was-
Home
Now, I changed it to...
Home
Now, It Works. Enjoy.

How to flush output using Codeigniter 3?

I'm writing a CodeIgniter 3 application. My goal is to have a view, which is constantly (as the code goes along) flushed with the output content. I read some answers in stackoverflow, but I am not sure, how to do this.
I have a Controller wich renders the view, in the update method.
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class Dataupdate extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->render('dataupdate_list_view');
}
public function update() {
$this->render('dataupdate_update_view');
}
}
?>
Here is the class "MY_Controller".
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
$this->data['page_title'] = 'Team Portal';
$this->data['before_head'] = '';
$this->data['before_body'] = '';
}
/**
* Render method is used to render a view using a template.
* The given template delivers the HTML header and footer.
* The view contains the actual page content.
*
* #param string $the_view The view to be rendered
* #param string $template The template to render the view
*/
protected function render($the_view = NULL, $template = 'master') {
if ($template == 'json' || $this->input->is_ajax_request()) {
header('Content-Type: application/json');
echo json_encode($this->data);
} else {
// Get current user from database
$this->load->model('user_model');
$user = $this->user_model->get_record_by_name($_SERVER['REMOTE_USER']);
// Data to pass to view
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->data['user'] = $user;
// Load view with data
$this->load->view('templates/' . $template . '_view', $this->data);
}
}
}
?>
Then I have a view, which outputs the content.
<div>
<!-- PAGE TITLE -->
<div class="page-title">
<h3>Daten Update</h3>
<div class="title_right">
</div>
</div>
<div class="clearfix"></div>
<!-- ALERTS -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Fehler<small>Kleiner Text</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content bs-example-popovers">
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<strong>Strong text.</strong>What to do now?
</div>
</div>
</div>
</div>
</div>
<!-- CONTENT -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Neues Daten Update ausführen</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<?php echo form_open('dataupdate/update'); ?>
<input type="text" name="test" />
<button>Ausführen</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
This works fine. So far.
Now, what I try to achieve is the following:
I click on the "Ausführen" (Execute) button, which submits the form to the same url as this page
Then I want a php script to execute, which continously outputs content to the page. Not all content at once, but adds output after output to the page.
I hope I made myself clear.
Any suggestions or tutorials, on how to do that?
You'll need to force the output by calling ->_display method on the output class
For example for JSON output you'll do something like this:
$response = array('status' => 'OK');
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
Same thing goes when you load a view, if you're 100% sure you won't need anything else, You may call _display manually to get the output; just be sure to call exit after that otherwise you'll end-up with a duplicated output (one from your manual call & the other from the automatic call).
Have a look at the user's guide for output class: http://www.codeigniter.com/user_guide/libraries/output.html

How to hide someparts in otherpage except home in codeigniter

I want to show a slider module on home page but hide it on rest of the site. Could anyone help me hide it. I am using HMVC in Codeigniter
<div class="col-xs-12 col-md-11 col-lg-11 navdiv" id="services_menu">
<?php
echo Modules::run('slider');
?>
</div>
You can try
<?php
if($this->router->fetch_class()=='home_controller_name'){
?>
<div class="col-xs-12 col-md-11 col-lg-11 navdiv" id="services_menu">
<?php
echo Modules::run('slider');
?>
</div>
<?php
}
?>
Create an seperate view for slider as slider.php. Put your following code into it
<div class="col-xs-12 col-md-11 col-lg-11 navdiv" id="services_menu">
<?php
echo Modules::run('slider');
?>
</div>
And while loading the Home page, Load this slider.php also like as follows :
$this->load->view("header.php");
$this->load->view("slider.php");
$this->load->view("home.php", $data);
$this->load->view("footer.php");
For other pages don't load the view slider.php.
Cheers!!

Zend framework 1 submit form url / action

I'm working on a AJAX sign up form which will be submitted to the Zend Framework. Right now, the form's action doesn't seem to be executing. The response text is a duplicate of the current HTML document. Can someone please inform me of what I'm doing wrong, and what the correct URL should be? I've set the project up using modules. For example, should the correct url from the example below be: '/account/register/auth'
#application.ini
resources.router.routes.register.route = /register
resources.router.routes.register.defaults.module = account
resources.router.routes.register.defaults.controller = register
resources.router.routes.register.defaults.action = index
#application/modules/accout/controllers/RegisterController.php
<?php
class Account_RegisterController extends Zend_Controller_Action{
public function init(){
$this->view->register = new Account_Form_Register();
}
public function indexAction(){
}
public function authAction(){
$request = $this->getRequest();
$form = new Account_Form_Register();
if($request->isPost()){
print('submit');
}else{
print('nothing yet');
}
}
}
?>
#application/modules/account/views/scripts/register/index.phtml
<div class="register">
<form id="registerForm">
<h2 class="formHeader">Register</h2>
<div class="floatLeft">
<?php echo $this->register->fName; ?>
</div>
<div class="remaining">
<?php echo $this->register->lName; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->uEmail; ?>
</div>
<div class="remaining">
<?php echo $this->register->aEmail; ?>
</div>
<div class="">
<?php echo $this->register->phone; ?>
</div>
<div class="floatLeft">
<?php echo $this->register->oPassword; ?>
</div>
<div class="remaining">
<?php echo $this->register->cPassword; ?>
</div>
<div id="formButton">
<?php echo $this->register->submit; ?>
</div>
</form>
</div>
As I said, maybe you should disable the view processing when doing AJAX action in order to return a json_encode($some_data) instead of HTML content.
Try
$this->_helper->viewRenderer->setNoRender(true);
Take a look at this article

Categories