Get multiple values of checkbox in codeigniter - php

I am currently doing a project. I have a checkbox( where the user will choose type of services provided by the company). When I try to post the service that was selected(for example 2 services is checked) in my controller, I am only getting one service. The question is how can I get the multiple values in my checkbox?
Note: I also tried to use foreach within my controller, I am getting some error like "Invalid argument supplied for foreach()".
View
<label>Desired Service</label> <br>
<?php foreach($services as $s):?>
<label><input type="checkbox" name="service_name[]" value="<?= $s->service_name?>"><?= $s->service_name?></label>
<br>
<?php endforeach?>
Controller
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('full_name', 'Fullname', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('contact', 'Contact', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('zip_code', 'Zip Code', 'numeric|required');
$this->form_validation->set_rules('province', 'Province', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('service_name', 'Service', 'required');
if ($this->form_validation->run() == FALSE) {
$this->index();
}
else {
$service_name = implode(', ', $_POST['service_name']);
$event = array(
'full_name' => $this->input->post('full_name'),
'email' => $this->input->post('email'),
'contact' => $this->input->post('contact'),
'address' => $this->input->post('address'),
'zip_code' => $this->input->post('zip_code'),
'state_province' => $this->input->post('province'),
'date' => $this->input->post('date'),
'service' => $service_name
);
$this->EventModel->add_event($event);
echo "<script>
window.alert('Your Desired Date is being Proccessed!');
location.href = '".site_url('/')."';
</script>";
}

Change from
$service_name = $_POST['service_name'];
foreach($service_name as $key =>$value)
{
echo $value;
}
die;
to
$service_name = implode(',',$_POST['service_name']);
echo $service_name;

I hope it will solve your problem
if (!empty($this->input->post('service_name'))) {
foreach ($this->input->post('service_name') as $key => $val) {
$data[] = array(
'service_name' => $_POST['service_name'][$key]
);
}
foreach ($data as $item) {
echo $item['service_name'];
}

Try:
$service_name = $this->input->post('service_name');
for($i=0;$i < count($service_name);$i++){
echo $service_name[$i];
}

Related

multiple $this->form_validation->run() not work in codeigniter

Please help me, to validate address. if i select radio button Office, then Office Address required to submit. if i select radio button Residential, then Resi. Address required to submit.
The radio button name="mailing"
Here i share controller code:
public function addMemberForm(){
if(!$this->form_validation->run('add_form')){
$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
$this->load->view('admin_panel/add-member');
}
elseif($post['mailing'] == 'office'){
if(!$this->form_validation->run('office_address')){
$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
$this->load->view('admin_panel/add-member');
}
}
elseif($post['mailing'] == 'residential'){
if(!$this->form_validation->run('residential_address')){
$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
$this->load->view('admin_panel/add-member');
}
}
else{
$this->session->set_flashdata('add_member_msg','<span class="alert alert-success">Member insert successfully</span>');
return redirect('admin/add_member');
}
}
Here i share config->form_validation.php code:
$config = [
'add_form' =>[
[
'field' => 'mailing',
'label' => 'Mailing Address',
'rules' => 'required'
],
],
'office_address' =>[
[
'field' => 'office_address',
'label' => 'Office Address',
'rules' => 'required'
],
],
'residential_address' =>[
[
'field' => 'resi_address',
'label' => 'Residential Address',
'rules' => 'required'
],
],
];
Below i share form photo:
I got solution above my question.
Remove office_address and residential_address from config->form_validation.php
You can Validate office and residential address in controller, See below Code:
public function addMemberForm(){
$validation_error = array();
if(!$this->form_validation->run('add_form')){
$validation_error[] = $this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
}
elseif($this->input->post('mailing') == 'office'){
$this->form_validation->set_rules('office_address','Office Address','required');
if($this->form_validation->run() == false){
$validation_error[] = $this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
}
}
elseif($this->input->post('mailing') == 'residential'){
$this->form_validation->set_rules('resi_address','Residential Address','required');
if($this->form_validation->run() == false){
$validation_error[] = $this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
}
}
if(count($validation_error)){
$this->load->view('admin_panel/add-member');
}
else{
$this->session->set_flashdata('add_member_msg','<span class="alert alert-success">Member insert successfully</span>');
return redirect('admin/add_member');
}
}

Validation of an API in codeigniter not working properly

I am writing an API in codeigniter. I am validating the fields it has with codeigniter built in function but somehow it's not working as it should.
public function check_validation()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$firstname = mysql_real_escape_string($this->input->post('firstname'));
$lastname = mysql_real_escape_string($this->input->post('lastname'));
$email = mysql_real_escape_string($this->input->post('email'));*/
//$firstname = 'Numaan';
//$lastname = 'sheikh';
//$email = 'test#test.com';
$this->form_validation->set_rules('firstname', 'Username', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$finalResult = array('code' => 100,
'msg'=>'Field Emsdsdspty.',
'data' => array()
);
}
else
{
$finalResult = array('code' => 100,
'msg'=>'Validation Successful.',
'data' => array()
);
}
echo json_encode($finalResult);
}
I am trying to get the posted values but it is not working properly.then i also tried to assign the values to the variables and then passed through the validation it also did not work.
Try this (i not testing)
function check_validation()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//default result
$finalResult = array('code' => 500, 'msg' => 'Unknow Error', 'data' => array());
if($this->input->post()) {
//set validaton
$this->form_validation->set_rules('firstname', 'Username', 'trim|strip_tags|required');
$this->form_validation->set_rules('lastname', 'Lastname', 'trim|strip_tags|required');
$this->form_validation->set_rules('email', 'Email', 'trim|strip_tags|required');
//check form
if($this->form_validation->run() == TRUE) {
$finalResult = array('code' => 200, 'msg' => 'Success', 'data' => array());
}else {
$finalResult = array('code' => 400, 'msg' => validation_errors(), 'data' => array());
}
}
echo json_encode($finalResult);
}
ps. api status code: http://www.restapitutorial.com/httpstatuscodes.html

couldn't insert data in codeigniter

i couldn't insert data to database. i don't know where the problem but when i var_dump($this->mberita->get_berita()); the result is array(0){}. I am a newbie in Codeigniter and couldn't really figure out how to solve this.
model
function get_berita()
{
$this->db->order_by('id_berita','asc');
$data = $this->db->get('berita_ukm');
return $data->result();
}
//untuk menambah berita
function insert_berita($data)
{
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->db->insert('berita_ukm', $data);
}
function validate_berita()
{
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
return TRUE;
}
}
controller
function tambah_berita()
{
if ($this->mberita->validate_berita() == TRUE) {
$this->mberita->insert_berita();
redirect('admin/berita/tambah_berita');
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
Please help me what to do. Thank you.
<?php
#in controller page#
// notes : you should make validation in controller page
if ($this->input->post('send')){ //request submit
// make form validation
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
// request data then put in array
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->mberita->insert_berita($data);
}
}
//model page
function insert_berita($data)
{
$this->db->insert('berita_ukm', $data);
}

Error updating the user settings in ionauth : "This form post did not pass our security checks."

I am using Ionauth library in codeigniter and edited edit_user() method in Auth controller to enable individual user updating his/her own user settings. So when a logged in user goes to : siteurl/auth/edit_user it shows the user settings just fine. But when I hit the save button I got an error: "This form post did not pass our security checks". Though the default url (siteurl/auth/edit_user/userID) works fine, For individual non-admin user I want to keep the url without userID at the end.
here is my edit_user() method:
//edit a user
function edit_user($id=NULL)
{
$this->data['title'] = "Edit User";
if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id) && !($id==NULL )))
//if (!$this->ionauth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
if($id==NULL){
$user = $this->ion_auth->user()->row();
}else{
$user = $this->ion_auth->user($id)->row();
}
$groups=$this->ion_auth->groups()->result_array();
$currentGroups = $this->ion_auth->get_users_groups($id)->result();
//process the phone number
/**if (isset($user->phone) && !empty($user->phone))
{
$user->phone = explode('-', $user->phone);
} **/
//validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('edit_user_validation_fname_label'), 'required|xss_clean');
$this->form_validation->set_rules('last_name', $this->lang->line('edit_user_validation_lname_label'), 'required|xss_clean');
if(!($this->input->post('email')==$user->email)){
$this->form_validation->set_rules('email', $this->lang->line('edit_user_validation_email_label'), 'required|valid_email|is_unique[users.email]');
}else{
$this->form_validation->set_rules('email', $this->lang->line('edit_user_validation_email_label'), 'required|valid_email');
}
/** $this->form_validation->set_rules('phone2', $this->lang->line('edit_user_validation_phone2_label'), 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', $this->lang->line('edit_user_validation_phone3_label'), 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', $this->lang->line('edit_user_validation_company_label'), 'required|xss_clean'); **/
$this->form_validation->set_rules('groups', $this->lang->line('edit_user_validation_groups_label'), 'xss_clean');
//$this->form_validation->set_message('is_unique[users.email]','Email already exists or Invalid');
if (isset($_POST) && !empty($_POST))
{
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
{
show_error($this->lang->line('error_csrf'));
}
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
/** 'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'), **/
);
//if($this->ion_auth->is_admin()){
//Update the groups user belongs to
$groupData = $this->input->post('groups');
if (isset($groupData) && !empty($groupData)) {
$this->ion_auth->remove_from_group('', $id);
foreach ($groupData as $grp) {
$this->ion_auth->add_to_group($grp, $id);
}
}
//}
//update the password if it was posted
if ($this->input->post('password'))
{
$this->form_validation->set_rules('password', $this->lang->line('edit_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('edit_user_validation_password_confirm_label'), 'required');
$data['password'] = $this->input->post('password');
}
if ($this->form_validation->run() === TRUE)
{
$this->ion_auth->update($user->id, $data);
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Saved");
redirect("auth", 'refresh');
}
}
//display the edit user form
$this->data['csrf'] = $this->_get_csrf_nonce();
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
//pass the user to the view
$this->data['user'] = $user;
//if($this->ion_auth->is_admin()){
$this->data['groups'] = $groups;
$this->data['currentGroups'] = $currentGroups;
//}
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email', $user->email),
);
/** $this->data['phone1'] = array(
'name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
);
$this->data['phone2'] = array(
'name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
);
$this->data['phone3'] = array(
'name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
); **/
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password'
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password'
);
$this->load->view('header');
$this->_render_page('auth/edit_user', $this->data);
$this->load->view('footer');
}
and this is my view file (edit_user.php):
<h1><?php echo lang('edit_user_heading');?></h1>
<div id="body">
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_email_label', 'email');?> <br />
<?php echo form_input($email);?>
</p>
<!--
<p>
<?php echo lang('edit_user_phone_label', 'phone');?> <br />
<?php echo form_input($phone1);?>-<?php echo form_input($phone2);?>-<?php echo form_input($phone3);?>
</p>
-->
<p>
<?php echo lang('edit_user_password_label', 'password');?> <br />
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
<?php echo form_input($password_confirm);?>
</p>
<?php //if($this->ion_auth->is_admin()){ ?>
<h3><?php echo lang('edit_user_groups_heading');?></h3>
<?php foreach ($groups as $group):?>
<label class="checkbox">
<?php
$gID=$group['id'];
$checked = null;
$item = null;
foreach($currentGroups as $grp) {
if ($gID == $grp->id) {
$checked= ' checked="checked"';
break;
}
}
?>
<input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
<?php echo $group['name'];?>
</label>
<?php endforeach?>
<?php //} ?>
<?php echo form_hidden('id', $user->id);?>
<?php echo form_hidden($csrf); ?>
<p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>
<?php echo form_close();?>
The csrf check is failing.
Try taking out $id = NULL in the method declaration (you don't need it anyway if you're sending the id via POST). Or explicitly set $id = $this->input->post('id'); before doing the csrf check.
In my case I was using relative URLs for the images and css files used in the site. Using base_url() to all the URLs present in the site fixed the problem. No issue now.

Email Class in Codeigniter Using Form

Disclaimer: I'm new to web develoment.
Scenario: I've created a contact form, and I'm trying to pass the input into the email class functions used in CodeIgniter. I am receiving undefined variable errors when the form is submitted. Email library is autoloaded for reference. It's late, and I may be overlooking something easy, but it doesn't hurt to post. Thanks a lot for all of your help!
Controller:
public function index()
{
//form validation
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['subject'] = array(
'name' => 'subject',
'id' => 'subject',
'type' => 'text',
'value' => $this->form_validation->set_value('subject'),
);
$this->data['message'] = array(
'name' => 'message',
'id' => 'message',
'type' => 'text',
'value' => $this->form_validation->set_value('message'),
);
if ($this->form_validation->run() == true)
{
$this->email->from($email);
$this->email->to('support#example.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect('contact/success');
}
else
{
$this->data['error_message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error_message');
$title['title'] = 'Contact';
$this->load->view('public/head_view', $title);
$this->load->view('public/header_view');
$this->load->view('public/contact_view', $this->data);
$this->load->view('public/footer_view');
}
View:
<div id="infoMessage"><?php echo $error_message;?></div>
<?php $attritubes = array('class' => 'nice'); ?>
<?php echo form_open('contact'); ?>
<p>Email Address:<br />
<?php echo form_input($email); ?>
</p>
<p>Subject:<br />
<?php echo form_input($subject); ?>
</p>
<p>Message:<br />
<?php echo form_textarea($message); ?>
</p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<?php echo form_close(); ?>
Well, you barely define any variable here:
if ($this->form_validation->run() == true)
{
$this->email->from($email);
$this->email->to('support#example.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
redirect('contact/success');
}
Where do $email, $subject, $message come from?
You might want to use something like (but I suggest you do better :))
$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
Also, make sure you've loaded the email library before calling it, and that you load the view in your else{} part (since you omitted it I cannot tell)

Categories