I am trying to show validation errors beside the form-control but form_error() is not working.
codeigniter form_error() not showing error beside form-control
The validation_errors() is working but form_error() not working beside the input control
Admin Controller
<?php
class Admin extends CI_Controller
{
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('uname','Username','required|alpha');
$this->form_validation->set_rules('pass','Password','required|max_length[12]');
$this->form_validation->set_error_delimiters("<div class='text-danger'>","</div>");
if($this->form_validation->run())
{
echo "Validation successful";
}
else
{
//echo validation_errors();
$this->load->view('Users/articleList');
}
}
}
?>
The view is
application/views/Users
<?php include('header.php'); ?>
<div class="container" style="margin-top:20px";>
<h1> Admin Form </h1>
<?php echo form_open('admin/index');?>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Username">Username</label>
<?php echo form_input(['class'=>'form-control', 'placeholder'=>'Enter Username','name'=>'uname']);?>
</div>
</div>
<div class="col-lg-6">
<?php form_error('uname');?>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Password">Password</label>
<?php echo form_password(['class'=>'form-control','type'=>'password',
'placeholder'=>'Enter Password', 'name'=>'pass'
]);?>
</div>
<div class="col-lg-6">
<?php form_error('pass');?>
</div>
</div>
</div>
<?php echo form_submit(['type'=>'submit', 'class'=>'btn btn-default','value'=>'Submit']); ?>
<?php echo form_reset(['type'=>'submit', 'class'=>'btn btn-default','value'=>'Reset']); ?>
</div>
<?php echo validation_errors();?>
<?php include('footer.php'); ?>
You need to echo the form_error so...
<?php form_error('pass');?>
becomes
<?php echo form_error('pass');?>
or
<?= form_error('pass');?>
I have 2 tables - questions and responses. I want to insert question_ids for every answer given into responses' table(a survey system). I have been able to insert answers, but not corresponding question_ids into responses' table.Here is my controller.
public function store(Request $request)
{
for($i=1; $i<=count($request->answer); $i++)
{
$answers[] = [
'answer' => $request->answer[$i],
'question_id' => $request->question_id];
}
Response::insert($answers);
}
// View
<div class="form">
<div id="successmessage">Survey sent successful. Thank you!</div>
<div id="errormessage">Errors</div>
<form action="{{ route('surveys.store') }}" method="post" role="form" class="surveyForm">
#csrf
<?php
;$count=1;
?>
#foreach($survey->questions as $question)
#switch($question->input_type)
#case('text')
#case('email')
<div class="form-group">
<input type="{{$question->input_type}}" name="answer[{{$count}}]" class="form-control"
id="{{strtolower($question->qtn)}}" placeholder="{{$question->qtn}}"
data-rule="{{$question->data_rule}}" data-msg="{{$question->data_message}}" />
<div class="validation"></div>
</div>
#break
#case('textarea')
<div class="form-group">
<textarea class="form-control" name="answer[{{$count}}]" rows="5" data-rule="{{$question->data_rule}}" data-msg="{{$question->data_message}}" placeholder="{{$question->qtn}}"></textarea>
<div class="validation"></div>
</div>
#break
#endswitch
<?php $count++; ?>
#endforeach
<div class="text-center">
<button type="submit">Send</button>
</div>
</form>
</div>
I am new to the codeigniter framework. When I try to load the parameter in codeigniter its showing an error like Missing argument 1. Can anyone please help solve my issue. Here Is my code.
This is My View Code. In view code i call the parameter in ">Read More
<div class="row m-t-20">
<?php foreach($show as $row) { ?>
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="recent-pro-box">
<div class="pro-img">
<img src="<?php echo base_url()?>uploads/<?php echo $row['img_path']; ?>" alt="" class="img-responsive" />
</div>
<h2 class="title"><?php echo $row['title'];?></h2>
<p><?php echo $row['message'];?> ...</p>
<div class="more-link">Read More</div>
</div>
</div>
<?php }?>
</div>
Controller code
public function viewProjects($id)
{
$data['show']=$this->Selection_Model->fullProjects($id);
$this->load->view('topurl');
$this->load->view('nav');
$this->load->view('fullProjects',$data);
$this->load->view('footer');
}
Model Code
public function fullProjects($id){
$this->db->select('*');
$this->db->where('p_id',$id);
$this->db->from('projects');
$query = $this->db->get();
$result = $query->row_array();
return $result;
}
Final view code
<div class="col-md-7 ab-text">
<?php foreach($show as $row) { ?>
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="recent-pro-box">
<div class="pro-img">
<p><?php echo $row['message'];?> ...</p>
</div>
</div>
<?php }?>
</div>
</div>
In your view.Not need to use foreach loop.Beacuse in codeigniter row_array result set is used to fetch only first matched row. try like this..
<div class="col-md-7 ab-text">
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="recent-pro-box">
<div class="pro-img">
<p><?php echo $show['message'];?> ...</p>
</div>
</div>
</div>
</div>
OR
If you want to use foreach loop.
In model change
$result = $query->row_array();
TO
$result = $query->result_array();
For more see docs Codeigniter Result Sets
All the code's are fine, except one line in Model. Your model should be
function fullProjects($id){
$this->db->select('*');
$this->db->where('p_id',$id);
$this->db->from('projects');
$query = $this->db->get();
$result = $query->result_array(); # Changed
return $result;
}
If you're accessing array data with this pattern $row['message'];, You must use the array as objective array like this $query->result_array();
I have an application where user has to fill a lengthy form, so I split the form into three and created steps - Step 1, Step 2 etc. So it has to be convenient for users. Now I face a problem, after filling out the details, a text field shows an error. When using model->get Errors() the error is 'address cannot be blank'. When I use $_POST the arrays displays value for the field address. I started yii framework last week and I do not know where I have gone wrong. Any help appreciated.
The view code i use forloop to reduce my code -
<fieldset>
<?php if(!Yii::app()->user->hasFlash('success')):
Yii::app()->user->setFlash('warning','All Fields are mandatory!');
endif; ?>
<?php echo CHtml::errorSummary($model, null, null, array(
'class' => 'alert alert-danger col-lg-offset-2',
)); ?>
<?php $rrr = -1;
foreach($model->attributeLabels() as $a){ $rrr++;
if($rrr<27 && $rrr>=12){
?>
<div class="form-group">
<?php echo CHtml::activeLabel($model, $a, array('class' => 'col-lg-3 control-label')); ?>
<div class="col-lg-7">
<?php echo CHtml::activeTextField($model,array_keys($model->attributeLabels())[$rrr],array('value'=>'a','size'=>255,'maxlength'=>255,'class'=>'form-control formtype1')); ?>
</div>
</div>
<?php
}}
?>
<?php $rrr = -1;
foreach($model2->attributeLabels() as $a){ $rrr++;
if($rrr>1 && $rrr<6){
?>
<div class="form-group">
<?php echo CHtml::activeLabel($model2, $a, array('class' => 'col-lg-3 control-label')); ?>
<div class="col-lg-7">
<?php echo CHtml::activeTextField($model2,array_keys($model2->attributeLabels())[$rrr],array('value'=>'a','size'=>255,'maxlength'=>255,'class'=>'form-control formtype1')); ?>
</div>
</div>
<?php
}}
?>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-sm btn-primary" type="submit"><?php echo Yii::t("user", "Submit") ?></button>
</div>
</div>
</fieldset>
</form>
Hi i am new to zend framework , i have created an app which will allow the user to edit an image and put some text inside the image, so i have decided to learn zend framework, by implementing personalmug application,
I have uploaded the app to the hosted server subdomain, but for some reason my application is not working same like on localhost. on localhost it is working fine but on the hosted server i dont know what is worng, but there some questions about redirecting on localhost after ajax success.
The issues are given below.
Question no 1.
when i Open the home page and click the customize button it will display a model which contains a form, with four input fields, and when i type in the 4 lines and click the preview button, it will call controller personalmug and the action is preview, the preview action will create an image from existing image which is in public/img/ folder, now it is not creating the image to the folder public/img/image_uri and the responce is blank page, and in the firebug the html the message is "Reload the page to get the source". and when i reload the page the image is not there and the image is not in the public/img/image_uri folder on the hosted server, and the session is not created i think the preview action has some issues. but dont know where i am worng, i have tried to google it from 4 days but no success.
The same thing is working on localhost.
Question number 2
i want to redirect the page after clicking the save button on preview.phtml page, now i have a save action in controller, which will save the data on localhost mysql database, and it is redirecting but only in the firbug , the actuall page is not redirecting, i have tried to use alerts in ajax success , but no use it is not displaying the message.
Please help and Thanks in advance. i would appreciate my code corrections. and some code samples. thanks again.
Index.phtml
<div class="row top-buffer">
<div class="span9">
<div class="row">
<div class="col-md-4"><img src='<?php echo $this->basePath('img/whitemug toxic fox.jpg') ?>'class="img-responsive"/></div>
<div class="col-md-6">
<h3><strong>Personal Mug</strong></h3>
<h3><small>Show your amazing coach just how great he is with this No.1 Coach Mug. This mug is a great gift for any occasion, make your gift extra special by personalising it with any name of your choice. This thoughtful gift is guaranteed to impress!</small></h3>
<a name="fulldescription">Read Full Description</a>
<div class="row top-buffer">
<div class="col-md-8">
<button class="btn btn-success btn-lg btn-block" data-toggle="modal" data-target="#myModal">Customize</button>
</div>
</div>
<div class="row top-buffer">
<ul>
<li>
The mug has space for a maximum of 48 characters, over a maximum of 4 lines.
</li>
<li>
Free Delivery when you spend over £40
</li>
<li>
Guaranteed Fast, Secure Shipping
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 ">
<h3><strong>Personal Mug</strong></h3>
<h4><small>Is your coach simply the best?</small></h4>
<h4>Reward them with this amazing No.1 Coach Mug. This mug features a fabulous award design with the message COACH (any name) no.1! Make this thoughtful gift extra special and personal by adding your coaches name!</h4>
</div>
<div class="col-md-6 ">
<?php foreach($personalmugs as $personalmug) : ?>
<div class="col-md-3 top-buffer"> <a href="#">
<img src="<?php echo $this->basePath($this->escapeHtml($personalmug->image_uri)); ?>" class="img-thumbnail img-responsive">
</a>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Submit text</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" action="personal-mug/preview" role="form" method="POST">
<div class="form-group">
<label for="inputText" class="col-sm-2 control-label">Text</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="mugtext1" maxlength="12" placeholder=Line1 pattern="^[a-zA-Z0-9\s]+" title="Only Alphabets and Numbers" min="1" max="12" required>
<input class="form-control" type="text" name="mugtext2" maxlength="12" placeholder=Line2 pattern="^[a-zA-Z0-9\s]+" title="Only Alphabets and Numbers" min="1" max="12" required>
<input class="form-control" type="text" name="mugtext3" maxlength="12" placeholder=Line3 pattern="^[a-zA-Z0-9\s]+" title="Only Alphabets and Numbers" min="1" max="12" required>
<input class="form-control" type="text" name="mugtext4" maxlength="12" placeholder=Line4 pattern="^[a-zA-Z0-9\s]+" title="Only Alphabets and Numbers" min="1" max="12" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button name="submit" value="submit" type="submit" class="btn btn-default">Preview</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
The preview phtml file code.
<div class="row top-buffer">
<div class="span9">
<div class="row">
<div class="col-md-4"><img id="mug1" name="mug1" src='
<?php
if(isset($_SESSION['preview_image_id']) && !empty($_SESSION['preview_image_id'])){
$ID = $_SESSION['preview_image_id'];
echo $this->basePath("img/image_uri/$ID.jpg");
} ?>'class="img-responsive"/></div>
<div class="col-md-6">
<h3><strong>Personal Mug</strong></h3>
<h3><small>Show your amazing coach just how great he is with this No.1 Coach Mug. This mug is a great gift for any occasion, make your gift extra special by personalising it with any name of your choice. This thoughtful gift is guaranteed to impress!</small></h3>
<div class="row top-buffer">
<div class="col-md-8">
<a id ="buttonSave" class="btn btn-success btn-lg btn-block">Save</a>
<a class="btn btn-success btn-lg btn-block" href="<?php echo $this->url('personal-mug', array('action'=>'add'));?>">Edit</a>
</div>
</div>
<div class="row top-buffer">
<ul>
<li>
The mug has space for a maximum of 48 characters, over a maximum of 4 lines.
</li>
<li>
Free Delivery when you spend over £40
</li>
<li>
Guaranteed Fast, Secure Shipping
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<script>
$('#buttonSave').click(function (){
var image_uri = $("#mug1").attr("src");
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://toxicfox.com/personal-mug/add',
async: false,
// you can use an object here
data: { image_uri: image_uri},
success: function(json) {
console.log(json.image_uri);
}
});
// you might need to do this, to prevent anchors from following
// or form controls from submitting
});
</script>
The controller.
namespace PersonalMug\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use PersonalMug\Model\PersonalMug;
use PersonalMug\Form\PersonalMugForm;
class PersonalMugController extends AbstractActionController{
protected $personalmugTable;
public function getPersonalMugTable(){
if(!$this->personalmugTable){
$sm = $this->getServiceLocator();
$this->personalmugTable = $sm->get('PersonalMug\Model\PersonalMugTable');
}
return $this->personalmugTable;
}
public function indexAction(){
return new ViewModel(
array(
'personalmugs' => $this->getPersonalMugTable()->fetchAll(),
));
}
public function addAction(){
$data['image_uri'] = $this->getRequest()->getPost('image_uri');
$personalmug = new PersonalMug();
$personalmug->exchangeArray($data);
$this->getPersonalMugTable()->save($personalmug);
return $this->redirect()->toRoute('personal-mug');
}
public function previewAction(){
if(isset($_POST['submit'])){
session_start();
$ID = uniqid();
$_SESSION['preview_image_id'] = $ID;
$text1 = htmlspecialchars($_POST['mugtext1']);
$text2 = htmlspecialchars($_POST['mugtext2']);
$text3 = htmlspecialchars($_POST['mugtext3']);
$text4 = htmlspecialchars($_POST['mugtext4']);
$this->getPersonalMugTable()->preview($ID,$text1,$text2,$text3,$text4);
}
}
The personalmug table file.
namespace PersonalMug\Model;
use Zend\Db\TableGateway\TableGateway;
use Zned\ServerUrl\Helper;
class PersonalMugTable{
protected $tableGateway;
public function __construct(TableGateway $tableGateway){
$this->tableGateway = $tableGateway;
}
public function fetchAll(){
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getImage($image_id){
$image_id = (int) $image_id;
$rowset = $this->tableGateway->select(array('image_id' => $image_id));
$row = $rowset->current();
if(!$row){
throw new \Exception("Could not find row $image_id");
}
return $row;
}
public function save(PersonalMug $personalmug){
$data = array(
'image_id' => $personalmug->image_id,
'image_uri' => $personalmug->image_uri,
);
$image_id = (int) $personalmug->image_id;
if($image_id == 0){
$this->tableGateway->insert($data);
}else{
if($this->getImage($image_id)){
$this->tableGateway->update($data, array('image_id' => $image_id));
}else{
throw new \Exception('Mug id does not exist');
}
}
}
public function deleteAlbum($image_id){
$this->tableGateway->delete(array('image_id' => (int) $image_id));
}
public function preview($ID,$text1,$text2,$text3,$text4){
strtoupper($text1);
strtoupper($text2);
strtoupper($text3);
strtoupper($text4);
// load the image from the file specified:
$im = imagecreatefromjpeg('public/img/whitemug%20toxic%20fox.jpg');
// if there's an error, stop processing the page:
if(!$im){
die("");
}
// define some colours to use with the image
$black = imagecolorallocate($im, 0, 0, 0);
// now we want to write in the centre of the rectangle:
$font ="public/fonts/arial.ttf"; // store the int ID of the system font we're using in $font
// store the text we're going to write in $text
// finally, write the string:
imagettftext($im,60,0,260,460,$black,$font,$text1);
imagettftext($im,60,0,260,560,$black,$font,$text2);
imagettftext($im,60,0,260,660,$black,$font,$text3);
imagettftext($im,60,0,260,760,$black,$font,$text4);
imagejpeg($im, "public/img/image_uri/$ID.jpg");
// tidy up
imagedestroy($im);
}
}
I think you are getting an error in much lower level than the framework. Try to see Apache "error.log" for more details about the error. Please let me know if you find anything.