Cannot populate field when using form_helper in Codeigniter - php

form were populating working in my previous project but now it isn't. I checked the code on my previous project and copy pasted it but still it doesn't populate it. When I type in it and submit it, it shows blank but when i inspected it in the browser it showed something like this:
check this image
Below is the my view:
<div class="form-group">
<?php echo form_hidden('STUD_ID', #$upd->STUD_ID); ?>
<?php echo form_label('Student name','name'); ?>
<?php echo form_input("STUD_NAME",#$upd->STUD_NAME,set_value('STUD_NAME'),["class"=>"form-control","placeholder"=>"John
Doe"]);?>
<?php echo form_error('STUD_NAME'); ?> </div> <div class="form-group">
<?php echo form_label('Password','password'); ?>
<?php echo form_input('STUD_PASS',#$upd->STUD_PASS,['class'=>'form-control','placeholder'=>'******']),set_value('STUD_PASS')
?>
<?php echo form_error('STUD_PASS'); ?> </div>
Controller
$this->form_validation->set_rules('STUD_NAME','Username','trim|required|is_unique[student.STUD_NAME]');
$this->form_validation->set_rules('STUD_PASS', 'Password', 'trim|required|min_length[5]');
$this->form_validation->set_rules('STUD_EMAIL', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('STUD_ADD', 'Address', 'trim|required');
$this->form_validation->set_rules('STUD_PHONE', 'Phone', 'trim|required|min_length[10]');
$this->form_validation->set_rules('STUD_GENDER', 'Gender', 'trim|required');
$this->form_validation->set_rules('HOBBY[]', 'Hobby', 'trim|required');
$this->form_validation->set_error_delimiters('<small style="color:red">','</small>');
if ($this->form_validation->run() == TRUE) {
.......
}

Try with this code : initialise form_input with an array like this
Form should be like this :
<div class="form-group">
<?php echo form_hidden('STUD_ID', #$upd->STUD_ID); ?>
<?php echo form_label('Student name','name'); ?>
<?php echo form_input(["name" => "STUD_NAME","value" => set_value('STUD_NAME',#$upd->STUD_NAME),"class"=>"form-control","placeholder"=>"JohnDoe"]);?>
<?php echo form_error('STUD_NAME'); ?>
</div>
<div class="form-group">
<?php echo form_label('Password','password'); ?>
<?php echo form_input(["name" => "STUD_PASS","value" => set_value('STUD_PASS',#$upd->STUD_PASS),"class"=>"form-control","placeholder" => "******"]);?>
<?php echo form_error('STUD_PASS'); ?>
</div>
See more : https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_input

Related

Layout of a simple PHP form needs altering

I'm trying to create a simple inline form that displays an entry field followed by a submit button - how can I alter this existing code so that these elements display horizontally in one row? ...I've tried altering the form class but no success. Thank you in advance.
PHP
<div class="d3-mailchimp" data-block-id="<?php echo $bID; ?>">
<?php
if (isset($errors)) {
$errors->output();
}
if (isset($message)) {
echo '<p class="message">'.$message.'</p>';
} else {
?>
<form method="post" action="<?php echo $this->action('submit') ?>" onsubmit="d3_mailchimp_submit(this); return false;">
<?php $token->output('d3_mailchimp.subscribe'); ?>
<div class="form-group field">
<?php
echo $form->email('email_address', '', [
'required' => 'required',
'placeholder' => t(''),
]);
?>
</div>
<?php
if ($showTermsCheckbox) {
?>
<div class="accept-terms">
<label for="accept_terms">
<?php
echo $form->checkbox('accept_terms', 1, 0, [
'required' => 'required',
]);
?>
<?php echo $acceptTermsText; ?>
</label>
</div>
<?php
}
?>
<div class="submit-button">
<?php
echo $form->submit('submit', t('Subscribe'), [
'class' => 'button',
]);
?>
</div>
</form>
<?php
}
?>

Error when trying to store the id of the multiple checkbox in database in yii

I had multiple check box to view the Hobby names from database, and it's working. But i need to store the selected id of the hobby names in the database. But when i select any hobby name it stores only 0 in my database table.
I had one Hobbies table and i created one model for that also. Please anybody help.
This is in views/sample/register.php file
<?php
/* #var $this SampleController */
/* #var $model Sample */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sample-register-form',
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,)
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'username');
echo $form->textField($model,'username');
echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email');
echo $form->textField($model,'email');
echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password');
echo $form->passwordField($model,'password');
echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'confirm password');
echo $form->passwordField($model,'password');
echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'address');
echo $form->textArea($model,'address',array('rows'=>6, 'cols'=>22));
echo $form->error($model,'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'country');
$opts = CHtml::listData(Country::model()->findAll(),'countryid','cname');
echo $form->dropDownList($model,'country_id',$opts,
array(
'prompt'=>'Select Country',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Sample/Substate'),
'update'=>'#state_name',
'data'=>array('country_id'=>'js:this.value'),
)));
echo $form->error($model,'country_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state_id');
echo CHtml::dropDownList('state_name','', array('prompt'=>'Select Country First'),
array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('Sample/Subcity'),
'update'=>'#city_name',
'data'=>array('state_id'=>'js:this.value' ))));
echo $form->error($model,'state_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city_id');
echo CHtml::dropDownList('city_name','', array('prompt'=>'Select State First'));
echo $form->error($model,'city_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'url');
echo CHtml::activeFileField($model, 'url'); // by this we can upload image
echo $form->error($model,'url'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'hobby');
//echo $form->checkBoxList($model,'hobby',CHtml::listData(Hobbies::model()->findAll(),'hobby_id','hobby_name'));
$data = Hobbies::model()->findAll();
foreach($data as $button)
{
//echo $button->course_name;
echo $form->checkBox($model,'hobby');
echo $button->hobby_name .'<br>';
}
echo $form->error($model,'hobby'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender');
echo $form->radioButtonList($model,'gender',array('Male'=>'Male','Female'=>'Female'),array('labelOptions'=>array('style'=>'display:inline'), // add this code
'separator'=>' ',
) );
echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'dob');
$form->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'dob',
'name'=>$model->dob,
'value'=>$model->dob,
'options'=>array('dateFormat'=>'yy-mm-dd',
'altFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'1600:3000',
// which shows for both textfield and button
//'showOn'=>'both',
// 'buttonText'=>'choose date'
),
'htmlOptions'=>array('size'=>'10')
));
echo $form->error($model,'dob');
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Register'); ?>
</div>
<?php $this->endWidget(); ?>
This is in controller/SampleController.php file
public function actionRegister()
{
$model=new Sample;
if(isset($_POST['Sample']))
{
$model->attributes=$_POST['Sample'];
if($model->validate())
{
$model->hobby = implode(",",$model->hobby);
if($model->save())
{
$this->redirect(array('site/login'));
}
return;
}
}
$this->render('register',array('model'=>$model));
}
If you view the html generated by your foreach loop you will see that all your checkboxes have the same name and id. As such only one of them will be present in the $_POST array since the name is used as the key.
To fix this change the name to an array:
echo $form->checkBox($model, 'hobby[]');
Alternatively you can use CActiveForm::checkBoxList as in your commented code and use template to style it:
template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated check box input tag while "{label}" will be replaced by the corresponding check box label.
echo $form->checkBoxList(
$model,
'hobby',
CHtml::listData(Hobbies::model()->findAll(), 'hobby_id', 'hobby_name'),
array('template' => '{input}{label}<br/>')
);

Codeigniter form validation does not show error and redirects to blank page

Given below is controller and view but its not validated and only redirects to about:blank page.
I Have made some changes but nothing happens.
controller:
<?php
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library(array('form_validation','session')); // load form lidation libaray & session library
$this->load->helper(array('url','html','form')); // load url,html,form helpers optional
}
public function index(){
// set validation rules
$this->form_validation->set_rules('name', 'Name', 'required|min_length[4]|max_length[10]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Phone Number', 'required|numeric|max_length[15]');
$this->form_validation->set_rules('subject', 'Subject', 'required|max_length[10]|alpha');
$this->form_validation->set_rules('message', 'Message', 'required|min_length[12]|max_length[100]');
// hold error messages in div
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// check for validation
if ($this->form_validation->run() == FALSE) {
$this->load->view('viewform');
}else{
$this->session->set_flashdata('item', 'form submitted successfully');
redirect('Home');
}
}
}
?>
view:
<?php if(validation_errors()) { ?>
<div class="alert alert-warning">
<?php echo validation_errors(); ?>
</div>
<?php } ?>
<?php if($this->session->flashdata('item')) { ?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('item'); ?>
</div>
<?php } ?>
<?php echo form_open(); ?>
<div class="form-group">
<?php echo form_label('Your Name','name'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "name", "placeholder"=>"Enter Name","value" => set_value('name'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Email address','EmailAddress'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "email", "placeholder"=>"Enter email","value" => set_value('email'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Phone Number','number'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "number", "placeholder"=>"Enter Phone Number","value" => set_value('number'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Subject','subject'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "subject", "placeholder"=>"Enter Subject","value" => set_value('subject'))); ?>
</div>
<div class="form-group">
<?php echo form_label('Message','message'); ?>
<?php echo form_input(array("class"=>"form-control","name" => "message", "placeholder"=>"Enter Message","value" => set_value('message'))); ?>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<?php echo form_close(); ?>
so when any error is there the page is redirected to about:blank even if all fields are proper or improper how to fix this?

How to Update a field based on the submit button - Yii

I'm new to Yii framework. I have to create three buttons in my form (update.php). i.e. (Save, Approve, Reject). I'm using the following fields in my form.
<?php
/* #var $this MessageTemplateController */
/* #var $model MessageTemplate */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'message-template-form',
'enableAjaxValidation'=>false,
)); ?>
<?php
echo $form->errorSummary($model);
?>
<div class="row">
<?php //$model->ReviewedDate=date('Y-m-d H:i:s');?>
<?php echo $form->labelEx($model,'ReviewedDate'); ?>
<?php echo $form->textField($model,'ReviewedDate',array('value'=>'0000-00-00 00:00:00','readonly' => true)); ?>
<?php echo $form->error($model,'ReviewedDate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'SmsText'); ?>
<?php echo $form->textArea($model,'SmsText',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'SmsText'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'UpdatedDate'); ?>
<?php echo $form->textField($model,'UpdatedDate'); ?>
<?php echo $form->error($model,'UpdatedDate',array('value'=>date('Y-m-d H:i:s'),'readonly' => true)); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Approved'); ?>
<?php echo $form->hiddenField($model, 'Approved'); ?>
<?php echo $form->error($model,'Approved'); ?>
</div>
<div class="row">
<div class="row">
<?php echo $form->labelEx($model_al, 'username'); ?>
<?php $identity=Yii::app()->user->name;?>
<?php echo $form->textField($model_al, 'username',array('value'=>$identity,'readonly' => true), array('size' => 60, 'maxlength' => 250)); ?>
<?php echo $form->error($model_al, 'username'); ?>
<div>
<div class="row">
<?php echo $form->labelEx($model_al, 'updatedtime'); ?>
<?php echo $form->textField($model_al, 'updatedtime',array('value'=>date('Y-m-d H:i:s'),'readonly' => true), array('size' => 60, 'maxlength' => 250)); ?>
<?php echo $form->error($model_al, 'updatedtime'); ?>
<div>
<div class="row">
<?php echo $form->labelEx($model_al, 'comments on approval/rejection'); ?>
<?php echo $form->textField($model_al, 'comments',array('size' => 60, 'maxlength' => 250)); ?>
<?php echo $form->error($model_al, 'comments'); ?>
<div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save', array('name' => 'save')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Accept', array('name' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('name' => 'reject')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Now I have created three buttons.There is a field called approved in the above form.
I want this actions to happen when i click on the following buttons :
1. Save - All fields have to be saved but not approved(keep it as default).
2. Accept - All fields have to be saved with Approved changing to 1 in database.
3. Reject - All fields have to be saved with Approved changing to 0 in database.
How can I do this.
EDit
I have added the following in my controller.
public function actionUpdate($id)
{
$model_mt=new Messagesintable;
$model_al=new AuditLogin;
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['MessageTemplate']) && isset($_POST['AuditLogin']))
{
$model->attributes=$_POST['MessageTemplate'];
list($name,$mobile,$email)=retrieve_persondetails($id);
if($model->save())
{
$model_al->attributes=$_POST['AuditLogin'];
if ($model_al->save())
$this->redirect(array('admin','id'=>$model->Id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
You can check whether which submit button is clicked with these conditions and write your action inside.
if($_POST) {
if (isset($_POST['Save'])) {
// your code here
}
if (isset($_POST['Accept'])) {
// your code here
}
if (isset($_POST['Reject'])) {
// your code here
}
}

php yii send error to form on actionCreate

I have the following form which is being used to create new records
<?php
/* #var $this ComponentsController */
/* #var $model Components */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'components-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'component_id'); ?>
<?php echo $form->textField($model,'component_id'); ?>
<?php echo $form->error($model,'component_id'); ?>
</div>
<div class="row">
<label class="required" for="FixedAsset_original_asset_number">
Asset Number
</label>
<input id="Components_original_asset_number" type="text" name="Components[original_asset_number]">
<?php //echo Chtml::textField('fixed_asset_id', FixedAsset::model()->FindByPk($model2->fixed_asset_id)->fixed_asset_id); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'manufacturer'); ?>
<?php //echo $form->textField($model,'manufacturer'); ?>
<?php $manufacturer = Manufacturers::model()->findAll(array("order"=>"name"));
$list = CHtml::listData($manufacturer, 'manufacturer_id', 'name');
echo $form->dropDownList($model,'manufacturer', $list,array());
?>
<?php echo $form->error($model,'manufacturer'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'medium'); ?>
<?php //echo $form->textField($model,'medium'); ?>
<?php $medium = Medium::model()->findAll();
$list = CHtml::listData($medium, 'medium_id', 'type');
echo $form->dropDownList($model,'medium', $list,array());
?>
<?php echo $form->error($model,'medium'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'version'); ?>
<?php echo $form->textField($model,'version',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'version'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'serial_no'); ?>
<?php echo $form->textField($model,'serial_no',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'serial_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'purchase_date'); ?>
<?php //echo $form->textField($model,'purchase_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id'=>'Components_purchase_date',
'name'=>'Components[purchase_date]',
//'value'=>CTimestamp::formatDate('d/m/Y',$item->validFrom),
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo $form->error($model,'purchase_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'disposal_date'); ?>
<?php //echo $form->textField($model,'disposal_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'id'=>'Components_disposal_date',
'name'=>'Components[disposal_date]',
//'value'=>CTimestamp::formatDate('d/m/Y',$item->validFrom),
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<?php echo $form->error($model,'disposal_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'model'); ?>
<?php echo $form->textField($model,'model',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'model'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'nol'); ?>
<?php echo $form->textField($model,'nol'); ?>
<?php echo $form->error($model,'nol'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I want to send an error to the field Components_original_asset_number on the form. How would I achieve this. In the controller function I am checking to see if it exists if not I want to display error on form or message. original asset number is part of a separate model which I am displaying on this form.
public function actionCreate()
{
$model = new Components;
$model_fixedAsset = new FixedAsset;
$model_comAsset = new ComAsset;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Components']))
{
$model->attributes=$_POST['Components'];
$fixedAssetId = null;
// Check if asset exist and get PK
if( $_POST['Components']['original_asset_number'] != "" ){
//print_r($_POST['Components']);
$criteria = new CDbCriteria;
$criteria->condition = "(original_asset_number = :original_asset_number)";
$criteria->params = array(":original_asset_number" => $_POST['Components']['original_asset_number'] );
$fixedAssetRow = FixedAsset::model()->find($criteria);
//print_r($fixedAssetRow);
if($fixedAssetRow){
$fixedAssetId = $fixedAssetRow->fixed_asset_id;
}
//echo $fixedAssetId;
}
if($fixedAssetId){
/*if($model->save())
$this->redirect(array('view','id'=>$model->component_id));*/
// Create com_asset record
}else{
//no asset found return error message to input corect asset number or create asset in navision and run php script to update mysql db
}
}
$this->render('create',array(
'model'=>$model,
'model_fixedAsset'=>$model_fixedAsset,
'model_comAsset'=>$model_comAsset,
));
}
You should add the 'original_asset_number' in your model as variable and set required validation rule. Then you can use the following line to set error
$model->addError('original_asset_number', " no asset found return error message to input corect asset number or create asset in navision and run php script to update mysql ");
Also, you use the following way to display the field and error.
<?php echo $form->errorSummary($model); ?>
or
<div class="row">
<?php echo $form->labelEx($model,'original_asset_number'); ?>
<?php echo $form->textField($model,'original_asset_number',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'original_asset_number'); ?>
</div>

Categories