Reputation: 37
This code is in Cakephp and I want to check that name and mobile field are empty or not.
This is my ctp file code :-
<div class="form-group">
<?= $this->Form->label('Name','Name',array('class' => 'col-sm-2 control-label')); ?>
<div class="col-sm-4">
<?= $this->Form->text('name', ['class' => 'form-control input-sm', 'style'=>'float:left', 'placeholder' => "Name"]); ?>
</div>
</div>
<div class="form-group">
<?= $this->Form->label('Mob No.','Mob No.',array('class' => 'col-sm-2 control-label')); ?>
<div class="col-sm-4">
<?= $this->Form->text('mobile', ['class' => 'form-control input-sm', 'style'=>'float:left', 'placeholder' => "Mob No."]); ?>
</div>
</div>
This is controller code :-
public function details_data(){
if($this->request->is('post')){
$result = null;
$_Post['Name']= $name;
$_Post['Mobile'] = $mobile;
if(!empty($name) && !empty($mobile)){
echo "Fields are not empty.";
} else{
echo "Fields are empty.";
}
}
}
Help me.
Upvotes: 2
Views: 72
Reputation: 8606
Firstly, when you're using CakePHP, you should never use $_POST. It's better to follow the conventions and use $this->request->data['Modelname']['fieldname'] instead.
Secondly, you could specify your validation rules within your model. It will display the mentioned error messages on form submit.
Coming back to what you've done, you could try this below mentioned code:
if($this->request->is('post')){
$name = $this->request->data['Modelname']['name'];
$mobile = $this->request->data['Modelname']['mobile'];
// Specify your Model name for both. Eg: if your database table is users, your model name should be User.
if(!empty($name) && !empty($mobile)){
echo "Fields are not empty.";
} else{
echo "Fields are empty.";
}
}
Upvotes: 4