So I am having trouble with the form_submit in codeigniter. I have a controller named welcome with a method named email. Whenever I hit my submit button the page refreshes but then just appends ?firstname=&email=&message=&submit=Submit to my url but doesn't carry out the method. Any reason this may be happening?
below is my html code.
<form role="form">
<div class="form-group">
<label>Name</label>
<?php $this->load->helper("form"); ?>
<?php echo validation_errors('<p class = "error">'); ?>
<?php echo form_open('welcome/email');
$data = array('type'=>'text','class'=>'form-control', 'name'=>'firstname');
echo form_input($data);
?>
</div>
<div class="form-group">
<label>Email</label>
<?php
$data = array('type'=>'email','class'=>'form-control', 'name'=>'email');
echo form_input($data);
?>
</div>
<div class="form-group">
<label>Message</label>
<?php
$data= array('type'=>'text','name'=>'message','class'=>'form-control','rows'=>7);
echo form_textarea($data);
?>
</div>
<div class="pull-right">
<!--<button type="submit" class="btn btn-custom btn-lg" action="welcome/email">Submit</button> -->
<?php
echo form_submit('submit','Submit');
echo form_close();
?>
</div>
This is my controller
<?php /*if ( ! defined('BASEPATH')) exit('No direct script access allowed');*/
class Welcome extends CI_Controller {
function index(){
$this->load->view('index-sidebar');
}
function email(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email Address','required|valid_email');
$this->form_validation->set_rules('firstname', 'Name', 'required|min_length[2]|max_length[30]');
$this->form_validation->set_rules('message', 'Message', 'required|min_length[5]|max_length[200]');
if ($this->form_validation->run()==FALSE){
$this->load->view('message not sent');
}
else{
$this->load->library('email');
$this->email->from(set_value('email'),set_value('name'));
$this->email->to("mhansen1989#gmail.com");
$this->email->subject('tutoring');
$this->email->message(set_value('message'));
$this->email->send();
echo $this->email->print_debugger();
$this->load->view('success');
}
}
}
there is no action parameter in your form tag.how do you submit the form?
try this
<form action="<?= base_url().'welcome/email'?>" role="form" method="post">
so here
if ($this->form_validation->run()==FALSE){
$this->load->view('message not sent');
}
put in your correct view name where it says 'message not sent'
$this->load->helper("form");
load your helpers in the controller -- better yet do it config/autoload
and this
$data = array('type'=>'text','class'=>'form-control','name'=>'firstname');
echo form_input($data);
in the form you repeat $data over and over. technically it will work but its going to mess up at some point so make them different names
$first = array('type'=>'text','class'=>'form-control','name'=>'firstname');
echo form_input($first);
and you need something to show the values in the form again if the validation is false. check out http://www.codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform
Related
I'm trying to set validation error to the my form using flashdata in codeigniter but i cannot see the error in a view here is my code of view section. I had already read other relative questions close to my this problem but i'm not found a solution!
<?php if($this->session->flashdata('errors')): ?>
<?php echo $this->session->flashdata('errors');?>
<?php endif; ?>
<?php $attributes = array('id'=>'admin_login','class'=>'form-horizontal'); ?>
<?php echo form_open('admin/login', $attributes); ?>
<div class="form-group">
<?php
$lbl = array('class'=>'col-sm-12 col-lg-12 col-md-12 col-xs-12 control-label');
echo form_label('Email','admin_login',$lbl); ?>
<div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
<?php $data = array('
class'=> 'form-control col-sm-12 col-lg-12 col-md-12 col-xs-12',
'name'=> 'email',
'placeholder'=>'Enter your email'
); ?>
<?php echo form_input($data); ?>
</div>
</div>
<div class="form-group">
<?php
echo form_label('Password','admin_login',$lbl); ?>
<div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
<?php $data = array('
class'=> 'form-control',
'name'=> 'password',
'placeholder'=>'Password'
); ?>
<?php echo form_password($data); ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 col-lg-12 col-md-12 col-xs-12">
<?php $data = array('
class'=> 'btn btn-success btn-md center mt-2',
'name'=> 'submit',
'placeholder'=>'Password',
'value'=>'LOGIN',
'type'=>'submit'
); ?>
<center>
<?php echo form_submit($data); ?>
</center>
</div>
</div>
<?php echo form_close(); ?>
This is my controller part it is name of 'admin' method name is login
public function login(){
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
// check the form has any error (any miss of rules we set in above codes
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('errors',
validation_errors());
// $this->session->set_flashdata('name','Your message');
// $errors = validation_errors();
// $this->session->set_flashdata('form_error', $errors);
// $this->session->set_flashdata('name','Your message');
// $data = array('errors'=> validation_errors());
// set_userdata() is regular way to set session, it need manually unset, there for we use flashdata
// $this->session->set_flashdata($data);
}else{
echo "its all good";
}
}
i tried so many codes, i commented out what i used so far. I set in autoload section session library also $autoload['libraries'] = array('database','form_validation','session');
Please tell me what is the wrong here.
Please try with the below code.
public function login(){
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == true){
echo "its all good";
}else{
$this->session->set_flashdata('errors', validation_errors());
}
$this->load->view('Your View Page');
}
I can access the registration form which appears in a model window via ajax on any page as coded in the menu layout .
I need some thing on click of submit button of registration form it should get display a thank you message which is also via ajaxin the same div as that of wherein the registration form appears
registratiincontrooler
if ($model->save()) {echo "thank you for registration";return; }
registration view
<?php
/* #var $this UserProfileController */
/* #var $model UserProfile */
$this->breadcrumbs=array(
'User Profiles'=>array('index'),
'Create',
);
?>
<div id="ajax101" >
<div class="form" >
<?php $form=$this->beginWidget('UActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>true,
'disableAjaxValidationAttributes'=>array('RegistrationForm_verifyCode'),
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>
<?php echo $form->errorSummary(array($model/*,$profile*/)); ?>
<div class="row">
<div class="rlabel">
<?php echo $form->labelEx($model,'username', array("style"=>"display:inline")); ?>
</div>
<div class="rtextfield">
<?php echo $form->textField($model,'username', array("style"=>"margin-left:43px")); ?>
</div>
<div class="rerror">
<?php echo $form->error($model,'username', array("style"=>"margin-left:113px")); ?>
</div>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password', array("style"=>"display:inline")); ?>
<?php echo $form->passwordField($model,'password', array("style"=>"margin-left:43px")); ?>
<?php echo $form->error($model,'password', array("style"=>"margin-left:113px")); ?>
<p class="hint">
<?php echo UserModule::t("Minimal password length 4 symbols."); ?>
</p>
</div>
<div class="row">
<?php echo $form->labelEx($model,'verifyPassword', array("style"=>"display:inline")); ?>
<?php echo $form->passwordField($model,'verifyPassword'); ?>
<?php echo $form->error($model,'verifyPassword', array("style"=>"margin-left:113px")); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email', array("style"=>"display:inline")); ?>
<?php echo $form->textField($model,'email', array("style"=>"margin-left:65px")); ?>
<?php echo $form->error($model,'email', array("style"=>"margin-left:113px")); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'user_type', array("style"=>"display:inline")); ?>
<?php echo $form->dropDownList($model,'user_type',$model->getUType()); ?>
<?php echo $form->error($model,'user_type', array("style"=>"margin-left:113px")); ?>
</div>
<?php if (UserModule::doCaptcha('registration')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
<?php echo $form->error($model,'verifyCode'); ?>
<p class="hint"><?php echo UserModule::t("Please enter the letters as they are shown in the image above."); ?>
<br/><?php echo UserModule::t("Letters are not case-sensitive."); ?></p>
</div>
<?php endif; ?>
<div class="row submit">
<?php echo CHtml::ajaxSubmitButton(UserModule::t("Register"),array('/user/register'),array('update'=>'#ajax101')); ?>//div for form
</div>
<?php $this->endWidget(); ?>
</div>
</div><!-- form -->
<?php endif; ?>
I think the problem exists here
<?php echo CHtml::ajaxSubmitButton(UserModule::t("Register"),array('/user/register'),array('update'=>'form')); ?>//div for form
</div>
example if I have 3 menu items
home
aboutus
contact
suppose visited home when I click on register a model window with registration form appears and when i click on submit button model->save as well make another call using ajax to update that model window with thank you message
similarly with other items in menu
whats happening is it saves and redirects to /user/registration with thank you but i need same model window and not to be updated with thank message and not redirected thank message
I did try this but it did not save
<?php echo CHtml::ajaxSubmitButton(UserModule::t("Register"),'',array('update'=>'form-content')); ?>
I am calling ajax within ajax .
try with
<?php
echo CHtml::ajaxSubmitButton(signup , CHtml::normalizeUrl(array('<controller>/<action>', 'render' => true)), array(
'dataType' => 'json',
'type' => 'post',
'success' => 'function(data) {
}',
'beforeSend' => 'function(){
}','complete' => 'function(){
}',
), array('id' => 'signup', 'class' => ''));
?>
The problem is in your controller, you have to stop Yii, return; alone won't cut it.
if ($model->save()) {
if($this->isAjaxRequest)
{
ehco 'thank you for registration';
Yii::app()->end(); // to stop yii
}
else
{
// probably redirect
}
}
<?php echo form_open_multipart('AdminHome/InsertMovieschedule'); ?>
<?php echo form_input('MovieTitle',set_value('MovieTitle')); ?>
<?php echo form_error('MovieTitle'); ?>
<?php echo form_upload('MoviePic'); ?>
<input type="submit" class="form_submit" name="submit" value="Submit" />
<?php echo form_close(); ?>
unable post above form values to controller when i submit form it show empty array when i print array in controller some body help me
Use attributes to pass to form_open like that
<?php
$attributes = array(
'method' => 'post'
);
echo form_open_multipart('AdminHome/InsertMovieschedule',$attributes);
?>
Try with
if($_POST)
{
//Here your post values
}
I have created a form using CActiveForm in yii. I am using ajax to submit the form using CHtml::ajaxSubmitButton. Form submission using ajax is working correctly and I am able to get the values in the control using $_POST['modelname']['fieldname']. But the ajax form validation is not working correctly. Below is the code
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'item-brand-form',
'enableAjaxValidation' => true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array(
'class'=>'form-horizontal',
),
)); ?>
<div class="form-group">
<?php echo $form->labelEx($model,'Brand Name',array('class'=>'control-label col-lg-4','for'=>'ItemBrand_brand_name')); ?>
<div class="col-lg-6">
<?php echo $form->textField($model,'brand_name',array('class'=>'form-control')); ?>
<?php echo $form->error($model,'brand_name'); ?>
</div>
<!-- End col-lg-6 -->
</div><!-- End Form Group -->
<div class="form-group">
<?php echo $form->labelEx($model,'Brand Code',array('class'=>'control-label col-lg-4','for'=>'ItemBrand_brand_code')); ?>
<div class="col-lg-6">
<?php echo $form->textField($model,'brand_code',array('class'=>'form-control')); ?>
</div>
<!-- End col-lg-6 -->
</div><!-- End Form Group -->
<div class="form-group">
<div class="col-lg-4"></div>
<div class="col-lg-2">
<?php echo CHtml::ajaxSubmitButton('Apply',array('class'=>'btn btn-lg btn-primary')); ?>
</div>
</div>
<?php $this->endWidget(); ?>
Also in the controller action following code is not working.
if(isset($_POST['ajax']) && $_POST['ajax']==='item-brand-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
so instead I am using
if(Yii::app()->getRequest()->getIsAjaxRequest())
{
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
While checking in the browser I am getting the response
{"ItemBrand_brand_name":["Brand Name cannot be blank."]}
But the error message is not displaying in the form. Also why the $_POST['ajax'] is not working ? Please help.
Thanks in advance.
I have solved this by using CHtml::submitButton instead of CHtml::ajaxSubmitButton and then submitting the form using jQuery $.ajax function in the form 'afterValidate' option. Please see the details here
I am a fairly new user of Yii.. Have been using it for less than 2 months..
I am trying to do a registration form that upon loading, it only shows 2 radio buttons(say choice 1 is Person, choice 2 is Organization), when the user selects a choice i want to dynamically display the fields corresponding to his selection using ajax.
Note: I am using the yii-user extension, and I am intending to use the same table to store both Person & Organization users, since they only differ in 3 attributes.
I have been stuck with this problem for 2 days and am not able to solve it..
This is a snippet of my code:
registration.php (view)
<?php $this->pageTitle=Yii::app()->name . ' - '.UserModule::t("Registration");
$this->breadcrumbs=array(
UserModule::t("Registration"),
);
?>
<h1><?php echo UserModule::t("Registration"); ?></h1>
<?php if(Yii::app()->user->hasFlash('registration')): ?>
<div class="success">
<?php echo Yii::app()->user->getFlash('registration'); ?>
</div>
<?php else: ?>
<div class="form">
<?php $form=$this->beginWidget('UActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>true,
'disableAjaxValidationAttributes'=>array('RegistrationForm_verifyCode'),
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>
<?php echo $form->errorSummary(array($model,$profile)); ?>
<div class="row">
<?php echo $form->radioButtonList($model, 'profile_type', array(0=>'Person',1=>'Company'),array('separator'=>'', 'labelOptions'=>array('style'=>'display:inline'), 'onclick' =>CHtml::ajax(array("type"=>"GET", "url"=>array("registration/add"), "data"=>array('test'=>'js:this.value'), "update"=>"#test")))); ?>
</div>
RegistrationController.php
public function actionAdd() {
if(Yii::app()->request->isAjaxRequest){
$this->renderPartial("/user/test",array('model'=>$model),false,true);
Yii::app()->end();
}
}
test.php
<?php
$val = $_GET['test'];
if($val == 1){
echo "yes";
//nothing added yet
}
else{
echo "no";
?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
<p class="hint">
<?php echo UserModule::t("Minimal password length 4 symbols."); ?>
</p>
</div>
<div class="row">
<?php echo $form->labelEx($model,'verifyPassword'); ?>
<?php echo $form->passwordField($model,'verifyPassword'); ?>
<?php echo $form->error($model,'verifyPassword'); ?>
</div>
<?php
}
?>
The erros am recieving when I select Person (choice 0) is the following:
Fatal error: Call to a member function labelEx() on a non-object in test.php
Any help is much appreciated!
The smoking gun is the error Fatal error: Call to a member function labelEx() on a non-object in test.php - it's telling you that labelEx() is not a method. Looking at your view.php, you can see labelEx() is called as a method of $form.
The issue is that when the view test.php is being rendered, it has no idea about the $form you've created in registration.php. test.php only knows about parameters you've passed to it (in this case, only $model).
Instead of using $form->method(), just use CActiveForm::method in your view, e.g.:
<div class="row">
<?php
echo CActiveForm::labelEx($model,'username')
echo CActiveForm::textField($model,'username');
echo CActiveForm::error($model,'username');
?>
</div>
I also combined your consecutive PHP blocks into a single block for easier reading/parsing/etc.