Form action with class and method adds to existing url - php

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.

Related

form_open() is not working peoperly

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.

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

best way to organize layout page, or multiple include views using one page

Which approach would you recommend me to use in building "Layout" or "Master" page in CodeIgniter.
Should I create master page view like this
<div id="page">
<div id="header"><?php include "_header.php"?>
</div>
<div id="content"><?php echo $content ?>
</div>
<div id="footer">
<?php include "_footer.php"?>
</div>
</div>
_header.php
<?php echo $title ?>
and controller which sends data
function displayData()
{
$data['title'] = "some title";
$data['content'] = "content page";
$this->load->view('header');
$this->load->view('content', $data);
}
or you use some other technique, I'm coming from asp.net mvc world so I'm trying to grab fast as possible some tips.
Thanks
Codeigniter has a layout library. You can access it here
https://github.com/EllisLab/CodeIgniter/wiki/layout-library. Also, you can approach layouts in Codeigniter using hooks.

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

I'm forced to call a controller method from a view, how do I refactor my code?

Okay so, I'm working with CodeIgniter. posts.php is my view that displays all the posts, each post must display its corresponding comments, which is what I'm trying to achieve.
I have a method in my model that takes the postid($postid) and return its corresponding comment($comment), unless I call the model method via a controller method,how do I accompolish this?
This is my view :
<body>
<?php foreach ($post as $key):?>
<div class="container">
<div class="span10">
<div id="box" class="alert-message block-message info">
<div id="post" class="post">
<?php echo $key->content;?><br />
</div>
<div>
<p><?php //echo $comment;?></p> <!--HERE THE COMMENTS OF THE CORRESPONDNING POST MUST BE ECHOED-->
</div>
<div>
<p><em>Comment</em></p>
</div>
<div id="commentarea<?php echo $key->postid;?>">
<?php $name=array('name'=>"form$key->postid");
echo form_open("/welcome/comments/$key->postid",$name);
$data=array(
'id' => 'input',
'name'=> 'content',
'rows' => '2',
'placeholder' => "Write a comment...",
'autofocus' => 'TRUE'
);
echo form_textarea($data);
?>
Comment
<?=form_close();?>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
$("div#commentarea<?=$key->postid;?>").hide();
$('a#commentnow<?=$key->postid;?>').click(function(){
$("div#commentarea<?=$key->postid;?>").slideToggle(250);
});
});
</script>
<?php endforeach;?>
</body>
This is my controller method that returns the comments that corresponds to the postid:
public function comments($postid)
{
//Post Comment
$comment=$this->input->post('content');
$data=array('content'=>$comment,'comment_postid'=>$postid);
$this->comments->postcomment($data);
//Retrieve
$comments['comment']=$this->comments->retrieve($postid);
$this->load->view('posts',$comments);
}
I'm a newbie,pardon me if my code is bad. I'm always looking forward to improving my code> Thanks for being patient. :)
Looking at your code it seems to me that you haven't fully understood how to use the MVC.
Firstly, your controller method comments contains the extraction of comments AND adding comments to a post. This doesn't seem logical when taking a look at the view file.
Instead you should seperate those two.
In your controller, add another metod called *post_comment* and move the adding comment functionality to that method and add a redirection afterwards:
public function post_comment($postid)
{
//Post Comment
$comment=$this->input->post('content');
$data=array('content'=>$comment,'comment_postid'=>$postid);
$this->comments->postcomment($data);
redirect('welcome/comments'); //redirect back to comments
}
Now, remove the adding of a comment from your comment method in the controller, so that you only retrieve the comments:
public function comments($postid)
{
//Retrieve
$comments['comment']=$this->comments->retrieve($postid);
$this->load->view('posts',$comments);
}
And finally change your view file - you need to post the comment to a new URL:
<?php $name=array('name'=>"form$key->postid");
echo form_open("/welcome/post_comment/$key->postid",$name);
This should do the trick.

Categories