I am trying to get users to fill in much information so I need more than one form and page. I made a main_view.php which has a side bar on the left with links to sub1.php, sub2.php, sub3.php. On the right half of main_view.php, it displays the sub pages with corresponding forms. Part of the main_view.php looks like this:
<?php $view_path = "../../application/views/"?>
<span id="theFormChanger" >
<?php
?>
</span>
var currentPage = 0;
var subviews = ['sub1.php', 'sub2.php','sub3.php'];
$('#sub1').click(function(){
currentPage = 1;
$('#theFormChanger').load(viewpath + subviews[currentPage]);
});
Part of the code of sub view pages:
<?php echo form_open('v_controller'); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'demail', 'name' =>'demail')); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'dname', 'name' => 'dname')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?>
For ../application/controllers/,there is a v_controller.php:
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->helper('form');
$this->load->view('sub1');
$data = array(
'User_Name' => $this->input->post('dname'),
'User_Email' => $this->input->post('demail'));
}?>
Every time when I go to localhost:8000/main/main_view, the left part is fine but the right part says "Fatal error: Call to undefined function form_open() in main_view.php"
I searched around but couldn't find answers. I made sure everything is loaded in autoload.php.
Is this a routing problem? I can't directly go to view files? Please help me. Thank you!
You can load views on to a view file like so
application > views > default.php
<?php $this->load->view('template/common/header');?>
<?php $this->load->view('template/common/navbar');?>
<?php $this->load->view('template/' . $page);?>
<?php $this->load->view('template/common/footer');?>
And then on controller
<?php
class Example extends CI_Controller {
public function index() {
$data['page'] = 'common/example';
$this->load->view('default', $data);
}
}
you need to create NameOfController/NameOfMethod in the form open method in your view page.
replace our code by this one , it will be work for you.
<?php echo form_open('v_controller/index'); ?>
Related
I want to create a link on login form, that is by clinking "Signup" a user will be redirected to sign up form but when I create a link it does not work and come back after refreshing the page shortly.
My login page looks like this:-
v_login.php
<body>
<h1>Simple Login with CodeIgniter</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('c_verifylogin/index');
echo form_label("Username: ");
echo form_input("username");
echo "<br>";
echo form_label("Password: ");
echo form_password("password");
echo "<br>";
echo form_submit("","Login");
echo form_close();
?>
**<a href="<?php echo site_url('c_signup/mylink_to_signup')?> " >Signup</a>**
</body>
The controller for this login id:-
c_login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_login extends CI_Controller {
function index() {
$this->load->helper(array('url','form','html'));
$this->load->view('v_login'); //load view for login
}
}
my signup controller is:-
c_signup.php
<?php
class c_signup extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('m_regt');
}
function mylink_to_signup(){
$this->load->view('v_signup');
}
function index()
{
// Including Validation Library
if ($this->form_validation->run() == TRUE)
{
$this->load->view('v_signup');
}
else
{
// Setting Values For Tabel Columns
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'),
'gender' => $this->input->post('gender'),
'profession' => $this->input->post('profession')
);
// Transfering Data To Model
$this->m_regt->form_insert($data);
// Loading View
//$this->load->view('v_login');
}
}
}
?>
and the Signup view is:-
v_signup.php
<div id="container">
//sign up form here
</div>
Check out URL helper in codeigniter which has an anchor function for creating anchor tags
The tag has three optional parameters:
anchor(uri segments, text, attributes)
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
Note: If you are building links that are internal to your application do not include the base URL This will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.
The second segment is the text you would like the link to say. If you leave it blank, the URL will be used.
The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.
Here are some examples:
echo anchor('news/local/123', 'My News', 'title="News title"');
Would produce: My News
echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
Would produce: My News
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
you don't have any mylink_to_signup function in c_login controller so it will be
<a href="<?php echo site_url('c_signup/mylink_to_signup');?>" >Signup</a>
Please look there was a space on your url:
**<a href="<?php echo site_url('c_signup/mylink_to_signup')?> " >Signup</a>**
Try change it:
Signup
i found the problem. there was actually a helper link for query string enable/disable in cofig file .
$config['enable_query_strings'] = FALSE;
i changed this to TRUE that's why the links were not working.
Thanks all for your help.`
I am trying to insert record using Cakephp.My model name is something like User.php.
And My working controller name is SignupsController.I want to insert record using this two but I cant.I am give my some codes below :
View :
<?php echo $this->Form->create('Signups',array('action' => 'registration'));?>
<div class="row-fluid">
<div class="span5">
<label class="">*First Name</label>
<?php echo $this->Form->input('first_name', array('type' => 'text','label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
<div class="span5">
<label class="">*Last Name</label>
<?php echo $this->Form->input('last_name', array('type' => 'text', 'label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
</div>
<?php echo $this->Form->end(); ?>
My controller code is given below :
class SignupsController extends AppController {
var $name = 'Signups';
var $uses=array("User");
public function registration()
{
$this->layout="reserved";
$this->Club->create();
if (isset($_POST['registration'])) {
echo "This is";
echo "<pre>";print_r($this->request->data);echo"</pre>";
$this->User->save($this->request->data);
//$this->Session->setFlash(__('Promoter registration has been done successfully'));
//$this->redirect('registration');
//$this->redirect(array('action' => 'registration'));
}
}
}
My model name is different which's name is User.php
I want to insert the record using this above code.Any idea how to insert?
you can do this by loading the users model in current controller just write the following line
$this->loadModel('Name of the Model').
then
$this->nameofmodel->save()
As you are unable to understand see this
Controller::loadModel(string $modelClass, mixed $id)¶
The loadModel() function comes handy when you need to use a model which is not the controller’s default model or its associated model:
$this->loadModel('Article');
$recentArticles = $this->Article->find(
'all',
array('limit' => 5, 'order' => 'Article.created DESC')
);
$this->loadModel('User', 2);
$user = $this->User->read();
Above pasted code is taken from CookBook of Cakephp, if you still do not understand just read it it has complete detailed explanation you can also see this to understand
you can use it with $uses variable in SignupController
class SingupController extends AppController
{
public $uses = array('User');
//rest of stuff
}
Or, if you want, you can load it on-demand inside a method:
$this->loadModel('User'); //now model is loaded inside controller and used via $this->User
EDIT: Your data array has to include the name of the model you're saving. So, replace:
$this->Form->create('Signups',array('action' => 'registration')
with:
$this->Form->create('User',array('url' => array('controller' => 'signups', 'action' => 'registration'));
I have a view (_form.php) with fields (name,summary) submit button. If I click on submit button, it should update Name field of One model and Summary field of another model.Both this models are of different databases.
Can anyone help on this. I tried the following for this
In _form.php(Test)
<?php echo $form->labelEx($model, ‘name’); ?>
<?php echo $form->textField($model, ‘name’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->error($model, ‘name’); ?>
<?php echo $form->labelEx(Test1::model(), ‘summary’); ?>
<?php echo $form->textField(Test1::model(), ‘summary’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->error(Test1::model(), ‘summary’); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>
In TestController.php
public function actionCreate() {
$model = new Test;
if (isset($_POST['Test'])) {
$model->attributes = $_POST['Test'];
if ($model->save()) {
$modeltest1 = new Test1;
$modeltest1->attributes = $_POST['Test1'];
$modeltest1->Id = $model->Id;
if ($modeltest1->save())
$this->redirect(array('view', 'Id' => $model->Id));
}
}
$this->render('create', array(
'model' => $model,
));
}
This code is not working. How can I make it work for different databases. I followed the below link for this.
http://www.yiiframework.com/wiki/291/update-two-models-with-one-view/
This code actually should work, but its bad.
I assume that you dont understand at all what is model and what its doing in Yii, also how to render and create forms.
I'll try to explain how it should be.
1st of all dont use Test::model() in views, unless you want to call some function from it(but try to avoid it). It can be done by passing it from controller:
public function actionCreate() {
$model_name = new Name;
$model_summary=new Summary;
//something here
$this->render('create', array(
'name' => $model_name,
'summary'=>$model_summary,
));
}
When you make render you passing variables to your view (name_in_view=>$variable)
2nd. In your view you can use your variables.
<?php echo $form->labelEx($name, ‘name’);
echo $form->textField($name, ‘name’, array(‘size’ => 60, ‘maxlength’ => 250));
echo $form->error($name, ‘name’);
echo $form->labelEx($summary, ‘summary’);
echo $form->textField($summary, ‘summary’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
echo $form->error($summary, ‘summary’); ?>
echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>
3rd. You need to understand what is model. It's class that extends CActiveRecord in this case. Your code in controller should loo like:
public function actionCreate() {
$model_name = new Name;
$model_summary=new Summary;
if (isset($_POST['Name']))
$model_name->attributes=$_POST['Name'];
if (isset($_POST['Summary']))
$model_name->attributes=$_POST['Summary'];
if ($model_name->save()&&$model_summary->save())
$this->redirect(array('view', 'Id' => $model->Id));
$this->render('create', array(
'name' => $model_name,
'summary'=>$model_summary,
));
}
$model->attributes=$_POST[] here is mass assignment of attributes, so they must be safe in rules. You always can assign attributes with your hands (1 by 1), or form an array and push it from array.
i have a simple question i have a form where user enters his details and when the submit button is clicked whit his details submitted to database it will take the user to a different page i am using codeigniter and i am new to this is there an easy way to do this ? tnx for you help. here is my cmv:
controller
<?php
class Info extends CI_Controller{
function index(){
$this->load->view('info_view');
}
// insert data
function credentials()
{
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
}
}
?>
model
<?php
class Info_model extends CI_Model {
function get_records()
{
$query = $this->db->get('credentials');
return $query->result();
}
function add_record($data)
{
$this->db->insert('credentials', $data);
return;
}
}
?>
view
<html>
<head>
</head>
<body>
<?php echo form_open('info/credentials'); ?>
<ul id="info">
<li>Name:<?php echo form_input('name')?></li>
<li>Second Name: <?php echo form_input('second_name');?></li>
<li>Phone: <?php echo form_input('phone');?></li>
<li>Email: <?php echo form_input('email');?></li>
<li><?php echo form_submit('submit', 'Start survay!!' );?></li>
</ul>
<?php echo form_close();?>
</body>
</html>
If all you need is a simple redirect upon submission of the form:
$this->info_model->add_record($data);
redirect('controller/method');
You could use the redirect() function from the URL Helper to actually redirect the user
(http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html)
Like this:
$this->load->helper('url');
redirect('/some/other/page');
Note that this has to be called before any data is outputted to the browser.
Another way of doing it is to simply have two different views that you load depending on the context. Normally you want some form validation as well so you can use that to direct the user. I usually end up with something like this in my function, which is used both for posting the data, inserting it to the database and "redirecting":
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
/* More validation */
if ($this->form_validation->run() !== FALSE) {
$data = array(
'name' => $this->input->post('name'),
'second_name' => $this->input->post('second_name'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
);
$this->info_model->add_record($data);
$this->load->view('some_other_view');
} else {
$this->load->view('info_view');
}
You can also use refresh as a second parameter:
$this->info_model->add_record($data);
redirect('controllerName/methodName','refresh');
In AppController:
public $helpers=array("Session","Html","Form");
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'MainPages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'MainPages', 'action' => 'front')
)
);
In MainPagesController:
public function front()
{
$this->Session->setFlash('Your stuff has been saved.');
debug($this->Session->read('Message'));
//etc...
In default layout (default.ctp)
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash(); ?>
The correct flash message shows in the debug but not on the view. What am I missing? PS It's not because of space after the ?>.
Edit: I have discovered that CakePHP is calling the session helper before the session component for some reason. Still trying to figure out how to fix it.
Simple way to create flash messages is to create their ctp file in app/view/element dir
Try
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Session->flash(); ?>
You need to define flash('auth') in your view to see authentication session flash messages.
My setflash works in this way..
Keep this in the App controller
function setFlash($message, $type = 'blue') {
//what ever the color you need..
$arr = array('red' => 'red', 'green' => 'green', 'yellow' => 'yellow', 'gray' => 'gray', 'blue' => 'blue');
$this->Session->setFlash($message, 'default', compact('class'), $arr[$type]);
}
and then call it from the controller action where you need to make the flash message as below..
$this -> setFlash("This is flash message","red");
now you need to make the flash in the layout(if you are using) or in your ctp file..
<?php echo $this->Session->flash() ?>
<?php echo $this->Session->flash('red') ?>
I think it is because you have an error here:
<?php echo "Flash:" ?>
You are missing the semi-colon
<?php echo "Flash:"; ?>