When dropdown list box will be blank, I am trying to view message alert box by jquery in foreach loop but not working what can I do?
my cakephp code is below:
<?php
foreach ($data['customer'] as $index => $d):
$customer = $d;
$package = array();
if (count($data['package']) > 0) {
$package = $data['package'][$index];
}
?>
<tr class="odd gradeX">
<td><?php echo $customer['first_name'] . ' ' . $customer['middle_name'] . ' ' . $customer['last_name']; ?></td>
<td>
<ul>
<li>Cell:<?php echo $customer['cell']; ?></li>
<li>Address:<?php echo $customer['address'] ?></li>
</ul>
</td>
<td>
<?php if (count($package) > 0): ?>
<ul>
<li> Package Name: <?php echo $package['name']; ?></li>
<li> Month: <?php echo $package['duration']; ?></li>
<li> Charge: <?php echo $package['charge']; ?></li>
</ul>
<?php
endif;
?>
</td>
<td>
<?php
echo $this->Form->create('PackageCustomer', array(
'inputDefaults' => array(
'label' => false,
'div' => false
),
'id' => 'form_sample_3',
'class' => 'form-horizontal',
'novalidate' => 'novalidate',
'url' => array('controller' => 'admins', 'action' => 'changeservice')
)
);
?>
<?php
echo $this->Form->input('id', array(
'type' => 'hidden',
'value' => $customer['id']
)
);
?>
<?php
echo $this->Form->input('status', array(
'type' => 'select',
'id' => 'ddlist',
'options' => Array('ticket' => 'Generate Ticket', 'payment' => 'Customer Information', 'history' => 'Ticket History'),
'empty' => 'Select Action',
'class' => 'form-control form-filter input-sm',
)
);
?>
<br>
<?php
echo $this->Form->button(
'Go', array('class' => 'btn blue', 'id' => 'btnddlist', 'title' => 'Do this selected action', 'type' => 'submit')
);
?>
<?php echo $this->Form->end(); ?>
</td>
</tr>
<?php
endforeach;
?>
my jquery code is:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnddlist").click(function () {
var ddlist = $("#ddlist");
if (ddlist.val() == "") {
//If the "Please Select" option is selected display error.
alert("Please select a list data!");
return false;
}
return true;
});
});
By this code when I click first record button code working but others record button when I click not view alert box message!
that's because it is programmed to only respond to the first option:
if (ddlist.val() == "") {
//If the "Please Select" option is selected display error.
alert("Please select a list data!");
return false;
}
if you want to display your message (alert) on every option (for whatever reason, you can just remove the if condition of the above code, so you got only:
//changed: always displays message now.
alert("You selected something in the list!");
return false;
instead.
Related
I am trying to use select2 querying the database because there are 30000 records this is really the only efficient way I could think of.
My problem is if I just submit this form the pole_id doesn't update.
Can someone help either with jQuery getting the id or telling me why the select box doesn't update the pole_id field in the db?
<?php
/* #var $this JpapolesController */
/* #var $model Jpapoles */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'jpapoles-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,'pole_id'); ?>
<?php //echo $form->textField($model,'pole_id',array('id'=>'pole_id')); ?>
<?php
$list = CHtml::listData(Poles::model()->findAll(array('order' => 'pole_number')), 'id', 'pole_number');
//echo $form->dropDownList($model, 'pole_id', $list, array('class'=>"js-example-basic-multiple", 'name'=>'pole_id', 'multiple'=>'multiple'));
//
echo CHtml::hiddenField('selectbox_pole_id', '', array('class' => 'span5'));
$this->widget('ext.select2.ESelect2',array(
'id'=>'myselect',
'selector' => '#selectbox_pole_id',
'options' => array(
'allowClear'=>true,
'placeholder'=>'Select a Pole',
'minimumInputLength' => 3,
'ajax' => array(
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type'=>'GET',
'dataType' => 'json',
'quietMillis'=> 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more };
}
',
),
),
));
?>
<?php echo $form->error($model,'pole_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php //echo $form->textField($model,'member_id'); ?>
<?php
$list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
echo $form->dropDownList($model, 'member_id', $list);
?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'jpa_id'); ?>
<?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
<?php echo $form->error($model,'jpa_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="javascript">
$("#myselect").on("change", function (e) {
var id = $("#myselect").select2("data")[0].id;
// Now you can work with the selected value, i.e.:
//$("#pole_id").val(id);
alert(id);
});
</script>
You can just use CActiveForm::hiddenField() to generate this hidden input:
<div class="row">
<?php echo $form->labelEx($model, 'pole_id'); ?>
<?php
echo $form->hiddenField($model, 'pole_id', ['id'=>'pole_id']);
$this->widget('ext.select2.ESelect2', [
'id' => 'myselect',
'selector' => '#' . CHtml::activeId($model, 'pole_id'),
'options' => [
'allowClear' => true,
'placeholder' => 'Select a Pole',
'minimumInputLength' => 3,
'ajax' => [
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type' => 'GET',
'dataType' => 'json',
'quietMillis' => 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results' => 'js:function(data,page) {
var more = (page * 10) < data.total; return {results: data, more:more };
}',
],
],
]);
?>
<?php echo $form->error($model, 'pole_id'); ?>
</div>
It will correctly handle existing data and no special actions are required in controller to handle this input - $model->attributes = $_POST['Jpapoles'] will load pole_id as any other attribute.
But this extension should also work with models direly, so you can just use:
$this->widget('ext.select2.ESelect2', [
'model' => $model,
'attribute' => 'pole_id',
'options' => [
// ...
],
]);
The you don't need to create hidden filed yourself.
Two parts:
one I decided to var_dump in the controller action:
Which resulted in me discovering that hidden field with id: selectbox_pole_id is where the posted value was.
So, I renamed that id to match the db id pole_id and added the following line before save: $model->pole_id = $_POST['pole_id'];
public function actionCreate()
{
$model=new Jpapoles;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Jpapoles']))
{
$model->attributes=$_POST['Jpapoles'];
$model->pole_id = $_POST['pole_id'];
if($model->save())
//$this->redirect(array('jpas/view','id'=>$_GET['jpano']));
var_dump($_REQUEST);
//echo $_POST['pole_id'];
}
$this->renderPartial('create',array(
'model'=>$model
),false,true);
}
/* #var $this JpapolesController */
/* #var $model Jpapoles */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'jpapoles-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,'pole_id'); ?>
<?php
echo CHtml::hiddenField('pole_id', '', array('class' => 'span5'));
$this->widget('ext.select2.ESelect2',array(
'id'=>'myselect',
'selector' => '#pole_id',
'model'=>$model,
'attribute'=>'pole_id',
'options' => array(
'allowClear'=>true,
'placeholder'=>'Select a Pole',
'minimumInputLength' => 3,
'ajax' => array(
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type'=>'GET',
'dataType' => 'json',
'quietMillis'=> 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more };
}
',
),
),
));
?>
<?php echo $form->error($model,'pole_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php //echo $form->textField($model,'member_id'); ?>
<?php
$list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
echo $form->dropDownList($model, 'member_id', $list);
?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'jpa_id'); ?>
<?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
<?php echo $form->error($model,'jpa_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="text/javascript">
// Or for single select elements:
$("#select").select2({
initSelection : function (element, callback) {
var data = {id: element.val(), text: element.val()};
callback(data);
}
});
</script>
When I submit this form all things are well n good but I want the controller function not shows in url. I want all things are done in same page. Still the url show
localhost/Naveen/CodeIgniter/welcome/insertform
but I don't want the form_open('') show's in url so how it is possible?
controller welcome.php
public function insertform()
{
if (isset($_POST['mysmt']))
{
$this->form_validation->set_rules('fname', 'Name', 'required');
$this->form_validation->set_rules('femail', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('fmobile', 'Mobile', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('welcome_view');
}
else
{
$_POST['fname'];
$_POST['femail'];
$_POST['fmobile'];
if($this->test_model->insert('user_accounts',array('',$_POST['fname'],$_POST['femail'],$_POST['fmobile'])))
{
$success['success']="Thanks For Join Us";
$this->load->view('welcome_view',$success);
}
}
}
else
{
$this->load->view('welcome_view');
}
}
view welcome_view.php
<?php echo form_open('welcome/insertform'); // ?>
<div class="form-group">
<?php
if(isset($success))
{?>
<input type="button" class="form-control btn-success" value="<?php echo $success; ?>">
<?php }
else
{
echo "";
}?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Name <?php echo form_error('fname'); ?></label>
<?php
$entername = array(
'name' => 'fname',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Name',
'class' => 'form-control',
);
echo form_input($entername); ?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Email <?php echo form_error('femail'); ?></label>
<?php
$enteremail = array(
'name' => 'femail',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Email',
'class' => 'form-control',
);
echo form_input($enteremail); ?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Mobile <?php echo form_error('fmobile'); ?></label>
<?php
$entermobile = array(
'name' => 'fmobile',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Mobile',
'class' => 'form-control',
);
echo form_input($entermobile); ?>
</div>
<div class="form-group">
<?php
$f_formsmt = array(
'name' => 'mysmt',
'value' => 'Submit Form',
'class' => 'form-control btn btn-success',
);
echo form_submit($f_formsmt); ?>
</div>
<?php echo form_close(); ?>
You handle it through applications/config/routes.php
consider you url: localhost/Naveen/CodeIgniter/welcome/insertform
$route['add'] = 'welcome/insertform';
now you can use localhost/Naveen/CodeIgniter/add
You can change whatever the string you want for any controller/function using routes.
Hope you understood :)
You need to add the url in route and add link to your link href
In route.php add:
$route['custom_url'] = 'controller/method';
I used Header("location:../{whatever-filename}")
Then gave that "whatever-filename" the proper routing in the config/routes.php.
Routing without using the redirect function would load the appropriate page but the url would still show the controller and method you're using.
Hope it helps
i have been trying to submit a jquery mobile form using ajax in cakephp but the form does not submit, i don't know what could be wrong, here is my controller code:
public function add() {
if ($this->request->is('ajax')) {
$this->request->data['Room']['user_id'] = $this->Session->read('Auth.User.id');
$this->Room->User->updateAll(array("User.room_count"=>'User.room_count+1'),array('User.id'=> $this->Session->read('Auth.User.id') ));
$this->Room->create();
if ($this->Room->save($this->request->data)) {
$this->Session->setFlash(__('The room has been saved.'));
$this->render('AjaxRoom','ajax');
} else {
$this->render('AjaxRoom','ajax');
}
}
$users = $this->Room->User->find('list');
$this->set(compact('users'));
}
and here is my view code:
<div id="sent" style="display:none;">Updating...</div>
<div id="success"></div>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {
$("#RoomIndexForm").bind("submit", function (event) {
$.ajax({
async:true,
beforeSend:function (XMLHttpRequest) {$("#sent").show(500)},
complete:function (XMLHttpRequest, textStatus) {$("#sent").hide(500);$("#TalkViewForm").each (function(){this.reset();});
//$("#refresh").load(location.href + " #refresh");
},
data:$("#RoomIndexForm").serialize(),
dataType:"html",
success:function (data, textStatus) { //$("#success").html(data);
},
type:"POST",
url: "<?php echo $this->Html->url(array('controller' => 'rooms', 'action' => 'add')); ?>"});
return false;
});
});
//]]>
</script>
<div data-role="popup" id="popupRoom" data-theme="a" class="ui-corner- all">
<?php echo $this->Form->create('Room',array('role'=>'form','url'=>array('controller'=>'r ooms', 'action'=>'add'))); ?>
<div style="padding:10px 20px;">
<h3>Add Room</h3>
<?php echo $this->Form->input('name', array(
'label' => false,
'placeholder'=>'Room Name'));
?>
<?php
echo $this->Form->input('description', array(
'label' => false,
'type' => 'textarea',
'maxlength'=>"140",
'placeholder'=>'Describe Your room briefly.'));
?>
<?php
$accountValues = array();
foreach ($categories as $key => $value) {
$accountValues[$value] = $value;
}
$categories = $accountValues;
echo $this->Form->select('category',array(
'options' =>$categories), array(
'label' => false,
'empty' => false
));
?>
<?php
//echo $this->Form->input('filename', array('type' => 'file','label'=>'Upload an Image'));
//echo $this->Form->input('file_dir', array('type' => 'hidden'));
?>
<?php
echo $this->Form->input('is_private', array(
'label' => 'Do you want a private Room ? If No, just ignore this field.',
'type'=>'checkbox',
));
?>
<?php echo $this->Form->submit('Create',array('class'=>'ui-btn ui-corner-all ui-shadow ui-btn-b ui-btn-icon-left ui-icon-check')); ?>
</div>
<?php echo $this->Form->end(); ?>
</div>
please how do i achieve this functionality? any help is welcomed.
The Html Helper does not support url i think:
$this->Html->url(array('controller' => 'rooms', 'action' => 'add'))
Try to use this instead:
$this->Html->link(['controller' => 'Rooms', 'action' => 'add'])
Another way is the url builder (http://book.cakephp.org/3.0/en/views/helpers/url.html):
$this->Url->build(['controller' => 'Rooms', 'action' => 'add'])
I am very new to codeigniter and I was wondering if someone can help me. I want to pass a variable from my controller to my view, but the way I am do it it always give a undefined variable error. Also, if I keep the load->view call give an error too!
So, how can I pass the variable to the view? Thanks in advance to any help!
<?php <!-- view -->
echo form_open('match/setDefafultEmail', array(
'class' => '',
));
?>
<div class="well">
<div class="text-center">
<h4>Set Default Email</h4>
</div>
<?php
echo("<div>");
echo form_label('Email:');
echo form_input(array(
'id' => 'email_address',
'name' => 'email_address',
'type' => 'email',
'placeholder' => 'email#example.com',
'value' => set_value('email_address'),
'required' => '',
'title' => 'Email address'
));
echo("</div>");
echo("<div>");
echo form_submit(array(
'id' => 'btn',
'name' => 'min',
'type' => 'Submit',
'class' => 'btn btn-info',
'value' => 'Set Default Email'
));
echo("</div>");
echo form_close()
?>
<br>
<br>
<table class="table" >
<thead>
<tr>
<th>Name</th>
<th>Default Email</th>
</tr>
</thead>
<tr class="info">
<!--display current default name and email-->
<?php foreach($info as $row):?>
<td> <?php echo $row->full_name ?> </td>
<td> <?php echo $row->email ?> </td>
<?php endforeach;?>
</tr>
</table>
public function setDefafultEmail(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email_address', 'Email Address', 'valid_email');
if ($this->form_validation->run( ) == true){
$name = $this->input->post('full_name');
$default_email = $this->input->post('email_address');
$this->spw_vm_request_model->setEmailToDefault($name,$default_email);
}
$data['info'] = $this->request_model->getDefaultEmailAndName();
$this->load->view('admin/admin_dashboard',$data);
redirect('admin/admin_dashboard');
}
1) After you load the view you don't need to redirect. When you redirect('admin/admin_dashboard') you actually call the admin_dashboard Controller.
2) On your View you are trying to access an array as an object( $row->full_name ) I think you should use $row['full_name'].
3) In case your Model returns an object, you have to serialize it in your Controller and unserialize it in your View.
I am working on job portal,and have a search module for employers to search for application.I want to use ajax to list the application after search, listing the results in the same page.How should I do it.?,Below is the screenshot of the module.Have tried the following things.
//Contoller Code//
public function actionSearch()
{
$model = new SearchEmployee();
/*Getting Data From Search Form For Processing */
if (isset($_POST['SearchEmployee'])) {
$model->attributes = $_POST['SearchEmployee'];
$category = $_POST['SearchEmployee']['category'];
$skills = $_POST['SearchEmployee']['skills'];
$experience = $_POST['SearchEmployee']['experience'];
$ajaxmodel = SearchEmployee::model()->find(array(
'select' => array('*'), "condition" => "category_id=$category AND key_skills like'%$skills%'AND experience=$experience",
));
if($model==null)
{
Yii::app()->user->setFlash('success', "No Results");
$this->renderPartial('search');
}
else
{
$this->renderPartial('search', array('model' => $ajaxmodel));
Yii::app()->end();
}
}
// In views,Not posting full codes just codes to show the ajax results,//
<div class="view">
<h1>Results </h1>
<div class="view" id="id">
<h1> Records Display </h1>
<h4>Name: <?php echo $form->labelEx($model,'skills Required'); ?></h4>
<h4>Skills: <?php echo $form->labelEx($model,'Skills Required'); ?></h4>
<h4>Experience: <?php echo $form->labelEx($model,'Skills Required'); ?></h4>
<h5> <?php echo CHtml::submitButton('VIew Details'); ?></h5>
</div>
</div>
Is this way to do...
In your view just enable ajax, for example:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'client-document-form',
'method' => 'post',
'action' => $url,
'enableAjaxValidation' => false,
'errorMessageCssClass' => 'required_field_text'
)); ?>
This line enableAjaxValidation => true need be true also in your actions add this line $this->performAjaxValidation($model);
UPDATE:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'update-profile-form',
'enableAjaxValidation' => true,
'errorMessageCssClass' => 'required_field_text',
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'hideErrorMessage' => true,
'beforeValidate' => 'js:function(form) {
return validate.beforeValidate("update-profile-form");
}',
'afterValidate' => 'js:function(form, data, hasError) {
validate.afterValidate(data, "update-profile-form");
}'
),