codeigniter submit ajax alert success - ajax

i have this form that submits personal details
here is my view
<?php $attributes=a rray( 'autocomplete'=>"off", 'id' => 'theForm');?>
<?php echo form_open_multipart( 'employee/personaldetails', $attributes); ?>
<div class="row">
<div class="span3 offset1">
<label>First Name</label>
<input onkeypress="return fnAllowAlpha(event);" maxlength="50" type="text" value="<?php echo $info['first_name']; ?>" name="first_name" disabled>
<input type="hidden" value="<?php echo $info['employee_image']; ?>" name="employee_image" disabled>
</div>
<div class="span3">
<label>Middle Name</label>
<input onkeypress="return fnAllowAlpha(event);" maxlength="50" type="text" value="<?php echo $info['middle_name']; ?>" name="middle_name" disabled>
</div>
<div class="span3">
<label>Last Name</label>
<input onkeypress="return fnAllowAlpha(event);" maxlength="50" type="text" value="<?php echo $info['last_name']; ?>" name="last_name" disabled>
</div>
<div class="span1">
<div class="down1">
<button type="submit" class="button-orange" id="btnSave" style="display:none;">Save</button>
</div>
</div>
CONTROLLER
$this->load->model('mdl_employee','emp');
$id = $this->session->userdata('id');
$P1 = $this->input->post('first_name');
$P2 = $this->input->post('middle_name');
$P3 = $this->input->post('last_name');
$this->emp->update_myinfo($id, $P1, $P2, $P3);
model
public function update_myinfo($id, $P1, $P2, $P3){
$employee_data = array(
'first_name' => $P1,
'middle_name' => $P2,
'last_name' => $P3);
$this->db->where('employee_id', $id);
$this->db->update('employee', $employee_data);
}
i dont know how to use ajax.
i want to alert the ajax if success or not. after updating my database.. im just new programmer

Maybe you could try googling since its so common to submit forms using ajax.
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
Submitting HTML form using Jquery AJAX
I would suggest first try these links..and if u still get stuck then let us know, we would surely help you :)

Related

What is the cause of this wrong image upload error message in my Codeigniter 3 application?

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
There is, among others an "Edit account information" form, which has an image upload field. In the controller, the update() method contains the logic for the image upload action:
public function update() {
// Only logged in users can edit user profiles
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$id = $this->input->post('id');
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['author'] = $this->Usermodel->editAuthor($id);
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
// Upload avatar
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '100';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$uerrors = array('uerrors' => $this->upload->display_errors());
$data['uerrors'] = $uerrors;
}
if(!$this->form_validation->run() || !empty($uerrors))
{
print_r($data['uerrors']);
$this->load->view('partials/header', $data);
$this->load->view('dashboard/edit-author');
$this->load->view('partials/footer');
} else
{
$this->Usermodel->update_user($id);
$this->session->set_flashdata('user_updated', 'Your account details have been updated');
redirect(base_url('/dashboard/manage-authors'));
}
}
The (surprising) problem I have is that, even though I am uploading an image, print_r($data['uerrors']); returns You did not select a file to upload. in the browser.
In the view, the same error is returned:
<?php echo form_open(base_url('dashboard/users/update')); ?>
<input type="hidden" name="id" id="uid" value="<?php echo $author->id; ?>">
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<input type="text" name="first_name" id="first_name" class="form-control" value="<?php echo set_value('first_name', $author->first_name); ?>" placeholder="First name">
<?php if(form_error('first_name')) echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<input type="text" name="last_name" id="last_name" class="form-control" value="<?php echo set_value('last_name', $author->last_name); ?>" placeholder="Last name">
<?php if(form_error('last_name')) echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" class="form-control" value="<?php echo set_value('email', $author->email); ?>" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('bio')) echo 'has-error';?>">
<textarea name="bio" id="bio" cols="30" rows="5" class="form-control" placeholder="Add a short bio"><?php echo set_value('bio', $author->bio); ?></textarea>
<?php if(form_error('bio')) echo form_error('bio'); ?>
</div>
<input type="hidden" name="avatar" id="avatar" value="<?php echo $author->avatar; ?>">
<label for="avatar">Upload avatar</label>
<div class="form-group">
<input type="file" name="userfile" id="uavatar" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
<div class="form-group">
<div class="w-50 pull-left pr-1">
<input type="submit" value="Update" class="btn btn-block btn-md btn-success">
</div>
<div class="w-50 pull-right pl-1">
Cancel
</div>
</div>
<?php echo form_close(); ?>
The error message I was expecting, considering that the image I was trying to upload is larger then the specified limit (100KB) is: The file you are attempting to upload is larger than the permitted size.
What am I doing wrong?
Try this
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
<div class="form-group">
<input type="file" name="userfile" id="userfile" size="20">
<p><?php print_r($uerrors); ?></p>
</div>
Try to change your form opening, from :
<?php echo form_open(base_url('dashboard/users/update')); ?>
to
<?php echo form_open_multipart(base_url('dashboard/users/update')); ?>
To change the form encoding type from text/plain to multipart/form-data to support image data upload. Here is the difference between each encoding type.

Is it possible to call confirmation dialog from controller in codeigniterr?

I am using codeigniter of version 3. When I submit that form, i have to save the data of that form and a confirmation dialog with yes no option has to appear. If user clicks yes i have to redirect to a page and if use clicks to no i have to redirect to another page .How can i do that?
My View code:
<form id="saveRenewFirm" action="<?php echo base_url() ?>firm/saveFirmInfo" method="post" role="form">
<div class="row">
<div class="col-25">
<label> Name of the firm:</label>
</div>
<div class="col-75">
<input type="text" id="firm_name" name="firm_name" value="<?php echo $firmDetail->firm_name; ?>" <?php echo $status;?> style="width:50%;" />
</div>
</div></p>
<div class="row">
<div class="col-25">
<label>Category :</label>
</div>
<div class="col-75">
<input type="text" name="category" id="category" value="<?php echo $firmDetail->category; ?>" <?php echo $status;?> />
</div>
</div>
<div class="row">
<div class="col-25">
<label>Phone no :</label>
</div>
<div class="col-75">
<input type="text" name="phone_no" id="phone_no" value="<?php echo $firmDetail->phone; ?>" <?php echo $status;?> />
</div>
</div>
<div class="row">
<div class="col-25">
<label>Address :</label></div><div class="col-75"><input type="text" name="address" style="width:400px;" id="address" value="<?php echo $firmDetail->address; ?>" <?php echo $status;?> />
</div>
</div>
<button value="submit" name="action" type="submit" class="button" >Submit</button>
<?php echo form_hidden('renew_id',$firmDetail->renew_id);?>
</form>
My controller:
public function saveFirmInfo() {
$this->setValidation();
if($this->form_validation->run() == FALSE){
$this->index();
} else {
$data = array(
'firm_name' => $this->input->post('firm_name'),
'category' => $this->input->post(category),
'phone_no' => $this->input->post('phone_no'),
'address' => $this->input->post('address'));
$new_id = $this->renewed_firm_model->insert($data);
if($new_id>0){
$this->session->set_flashdata('success', 'Firm Detail submitted. Do you want to continue to payment?');
}else {
$this->session->set_flashdata('error', 'Error during processing, please try again...');
}
redirect('member/profile','refresh');
}
I want to bring a confirmation dialog instead of flash message to continue for payment . If user clicks yes then i have to redirect to payment page and if user clicks no another page has to be opened.
You can either do this 2 ways.
The first way, and the easiest given your current code would be to redirect to a page after the user submits the form that has a question and 2 options (yes or no).
The second way would be to submit the form via ajax and launch a modal on success function. This would be more intensive, and require substantial changes to your current save function.

what am I doing wrong with form_validation messages name field access?

I want to try to valid confirm password matches with my confirm password, but it not then it will show me an error , but when I try to set up my messages for username then show me it cannot be accessed by it what am I doing wrong? but I try to change name filed in my username , but it doesn't work.
new_user controller
public function new_user(){
$first_name = $this->security->xss_clean(strip_tags($this->input->post('first_name')));
$last_name = $this->security->xss_clean(strip_tags($this->input->post('last_name')));
$profile_id = $this->security->xss_clean(strip_tags($this->input->post('profile_id')));
$password = $this->security->xss_clean(strip_tags($this->input->post('password')));
$username = $this->security->xss_clean(strip_tags($this->input->post('username')));
$data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'profile_id' => $profile_id,
'password' => password_hash($password, PASSWORD_BCRYPT),
'username' => $username,
);
$this->form_validation->set_rules('first_name','first_name','trim|required');
$this->form_validation->set_rules('last_name','last_name','trim|required');
$this->form_validation->set_rules('username','username','trim|required|');
$this->form_validation->set_rules('password', 'password', 'trim|required');
$this->form_validation->set_rules('conf_password', 'conf_password', 'matches[password]|required');
$this->form_validation->set_message('first_name', 'Please enter First Name');
$this->form_validation->set_message('last_name', 'Please enter Last Name');
$this->form_validation->set_message('username', 'Please enter Username');
$this->form_validation->set_message('password', 'Please enter Password');
if ($this->form_validation->run() == FALSE) {
$this->json($this->form_validation->error_array());
}else{
$this->user->signup($data);
}
}
form
<form id="signup" method="post" onsubmit="return false;">
<h2 class="sr-only">New User</h2>
<div class="illustration">
<i class="icon ion-ios-locked-outline" style="color: rgb(251, 244, 244);"></i>
</div>
<div class="form-group">
<input type="text" name="first_name" id="first_name" placeholder="First Name" class="form-control"
required="required" />
</div>
<div class="form-group">
<input type="text" name="last_name" id="last_name" placeholder="Last Name" class="form-control"
required="required" />
</div>
<div class="form-group">
<select name="profile_id" id="profile_id" class="form-control" required="required">
<option value="">Select a Profile</option>
<?php foreach ($profile as $value): ?>
<option value="<?php echo $value['id'];?>">
<?php echo $value['profile']; ?>
</option>
<?php endforeach ?>
</select>
</div>
<div class="form-group">
<input type="text" name="username" id="username" placeholder="username" class="form-control"
required="required" />
</div>
<div class="form-group">
<input type="password" name="password" id="password" placeholder="***********" class="form-control"
required="required" />
</div>
<div class="form-group">
<input type="password" name="conf_password" id="conf_password" placeholder="***********" class="form-control"
required="required" />
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit" style="background-color: rgb(41, 54, 78);">Ingresar</button>
</div>
</form>
output error
{username: "Unable to access an error message corresponding to your field name username.()"}
username
:
"Unable to access an error message corresponding to your field name username.()"
I think you should
Replace this
$this->form_validation->set_rules('username','username','trim|required|');
To:
$this->form_validation->set_rules('username','username','trim|required');

Validate with codeigniter

I dont know what am i doing wrong with the validation.
here is my Controller
function update_user() {
$this->load->library('form_validation');
$this->form_validation->set_rules('sirname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('name', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('update_view');
}
else
{
$data = array(
'surname' => $this->input->post('sirname'),
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
);
$this->Account_model->update_user(31,$data);
$this->show_user();
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Updated!</div>');
redirect('home');
}
}
here is my view
<form method="post" action="<?php echo base_url() . "account/update_user"?>" >
<div class="form-group">
<label for="name">First Name</label>
<input class="form-control" name="sirname" placeholder="Your First Name" type="text" value="<?php echo $user->surname; ?>" />
<span class="text-danger"><?php echo form_error('sirname'); ?></span>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input class="form-control" name="name" placeholder="Last Name" type="text" value="<?php echo $user->name; ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo $user->email; ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-default" onclick="account.php">Update</button>
<button name="cancel" type="reset" class="btn btn-default">Cancel</button>
</div>
<?php echo form_close(); ?>
<?php endforeach; ?>
<?php echo $this->session->flashdata('msg'); ?>
i need to display the value already in the database as im doing an update info for user.. so how do i implement the set_value('name') as well.
First preference is for form_error(),
so in value for each input fields,
value="<?php form_error("surname") ? echo set_value("surname") : echo $user->surname; ?> ?>"
Which means, if form error, then echo set_value() else echo database value.
in home controller where you redirect to success load :
$this->load->library('form_validation');
it will be ok!
You can use CI's built-in set_value function, which lets you set the values of a form field. It has a second (optional) parameter, to set the default value for the field.
<? echo form_label('Last Name', 'name'); ?>
<input class="form-control" name="name" id="name" type="text" value="<? echo set_value('name', $user->name) ?>">
<? echo form_error('name', '<span class="text-danger">','</span>');?>
In the code above, when loaded for the first time, the form will show the value from the database. But when returned after failing form validation, it will show the user input.

Codeigniter Validation errors work on localhost not on server

I have problem same code running on local and on server, but i dont get validation errors when i upload code online. I pass validatio errors via flashdata
Controller
function send_mail($lang)
{
$this->load->library(array('email','form_validation'));
$this->form_validation->set_rules('fullname', 'Full Name','trim|required|xss_clean');
$this->form_validation->set_rules('address', 'Permanent Home Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('posta', 'Email Address', 'required|valid_email|xss_clean');
$this->form_validation->set_rules('country', 'Country of Citizenship', 'trim|required|xss_clean');
$this->form_validation->set_rules('date', 'Date of Birth','trim|required|xss_clean');
$this->form_validation->set_rules('tel', 'Telephone Number','trim|required|xss_clean');
$this->form_validation->set_rules('inst', 'Name of Institution','trim|required|xss_clean');
$this->form_validation->set_rules('study', 'Field of Study', 'trim|required|xss_clean');
$this->form_validation->set_rules('degree', 'Degree awarded/expected','trim|required|xss_clean');
$this->form_validation->set_rules('dates1', 'Dates attended', 'trim|required|xss_clean');
$this->form_validation->set_rules('dates2', 'Dates attended', 'trim|required|xss_clean');
$this->form_validation->set_rules('employment', 'Employment', 'trim|required|xss_clean');
$this->form_validation->set_rules('language', 'Language Skills', 'trim|required|xss_clean');
$this->form_validation->set_rules('statement', 'Statement', 'trim|required|xss_clean');
if($this->form_validation->run()==FALSE)
{
$this->session->set_flashdata('errors', validation_errors());
$this->set_fleshdata();
header('Location: '.$_SERVER['HTTP_REFERER']);
}
else
in view
<?php
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$attributes = array('id' => 'intership_application_form',
'name' => 'intership',
);
echo $this->session->flashdata('success');
echo form_open_multipart('pages/send_mail/'.$lang, $attributes);
?>
<section>
<div > All fields required, only files in doc, docx or pdf format are allowed.</div><br/>
<div class="form_item">
<label class="form_item_name">Full Name:</label>
<input type="text" name="fullname" placeholder="Full Name" value="<?php echo $this->session->flashdata('name'); ?>" >
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Permanent Home Address:</label>
<input type="text" name="address" placeholder="Permanent Home Address" value="<?php echo $this->session->flashdata('address'); ?>"/>
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Email Address:</label>
<input type="email" name="posta" placeholder="Email Address" value="<?php echo $this->session->flashdata('posta'); ?>"/>
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Country of Citizenship:</label>
<input type="text" name="country" placeholder="Country of Citizenship" value="<?php echo $this->session->flashdata('country'); ?>" />
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Date of Birth:</label>
<input type="text" name="date" placeholder="Date of Birth" value="<?php echo $this->session->flashdata('date'); ?>" />
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Telephone Number:</label>
<input type="text" name="tel" placeholder="Telephone Number" value="<?php echo $this->session->flashdata('tel'); ?>"/>
</div><!--end .form_item-->
</section>
<section>
<p><strong>University Level Education</strong> <span>[ Please give details of your final school leaving qualification ]</span></p>
<div class="form_item">
<label class="form_item_name">Name of Institution:</label>
<input type="text" name="inst" placeholder="Name of Institution" value="<?php echo $this->session->flashdata('inst') ?>"/>
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Field of Study:</label>
<input type="text" name="study" placeholder="Field of Study" value="<?php echo $this->session->flashdata('study'); ?>"/>
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Degree awarded/expected:</label>
<input type="text" name="degree" placeholder="Degree awarded/expected" value="<?php echo $this->session->flashdata('degree'); ?>"/>
</div><!--end .form_item-->
<div class="form_item">
<label class="form_item_name">Dates attended:</label>
<input type="text" name="dates1" placeholder="Dates attended" value="<?php echo $this->session->flashdata('dates1'); ?>" />
<input class="below" type="text" name="dates2" placeholder="Dates attended" value="<?php echo $this->session->flashdata('dates2'); ?>"/>
</div><!--end .form_item-->
</section>
<section class="third">
<div class="form_item">
<label class="form_item_name" />Employment<span>[ Please outline details of employment history or relevant professional experience ]</span></label>
<textarea cols="" rows="" name="employment" placeholder="Your text here" ><?php echo $this->session->flashdata('employment'); ?></textarea>
</div>
<div class="form_item">
<label class="form_item_name" />Language SKills:<span>[ Proficiency in English is required for all CIRSD internship applicants, who are encouraged to indicate knowledge of any additional languages ]</span></label>
<textarea cols="" rows="" name="language" placeholder="Your text here" ><?php echo $this->session->flashdata('language'); ?></textarea>
</div>
</section>
<section class="third">
<div class="form_item">
<label class="form_item_name">References:<span>[Please include three academic/professional references or letters of recommendation ]</span></label>
<div class="right">
<p>Please attach your reference here:</p>
<div class="upload_buttons">
<p class="form">
<input type="text" id="path" class="path" />
<label class="add-photo-btn">BROWSE<span><input type="file" id="myfile" name='file1' /></span>
</label>
</p>
<p class="form">
<input type="text" id="path1" class="path" />
<label class="add-photo-btn">BROWSE<span><input type="file" id="myfile1" name='file2' /></span>
</label>
</p>
<p class="form">
<input type="text" id="path2" class="path" />
<label class="add-photo-btn">BROWSE<span><input type="file" id="myfile2" name='file3' /></span>
</label>
</p>
</div><!--end .upload_buttons-->
</div><!--end .form_item-->
</section><!--end .third-->
<section class="third">
<div class="form_item">
<label class="form_item_name" />Personal Statement:<span>[ Compose a brief personal statement in English outlining your interests, why you are applying for the CIRSD internship program, and what you hope to gain from it. Include an explanation of what makes you a suitable candidate for an internship at CIRSD. All statements should not exceed the limit of 750 words ]</span></label>
<textarea name="statement" cols="" rows="" class="personal_statement" placeholder="Your text here"><?php echo $this->session->flashdata('statement'); ?></textarea>
</div>
<div class="form_item cv">
<label class="form_item_name" />Cirriculum Vitae:</label>
<div class="right cv">
<p class="form">
<input type="text" id="path3" class="path cv" value="Please attach your CV here:" />
<label class="add-photo-btn">BROWSE<span><input type="file" id="myfile3" name='file4' multiple /></span>
</label>
</p>
</div>
</div>
</section><!--end .third-->
<section class="third">
<div class="form_item spam">
<label>Spam Check:</label>
<div class="spam_form">
<img src="<?php echo site_url(); ?>captcha_code_file.php?rand=<?php echo rand(); ?>" name="6_letters_code" id='captchaimg' >
<input type="text" class="spam_input" name="6_letters_code"/>
<input type="submit" class="send_btn" value="SEND" id="button"/>
</div>
</div><!--end .form_item-->
</section><!--end .third-->
<?php form_close(); ?>
<div><?php var_dump($this->session->flashdata('errors'));?></div>
Last line is important. Strange why my code works on xampp not on live server?
Please check your code you are setting 'errors' variable for setting errors messages and on view part you are accessing 'success' variable. Please use same variable at both places.
Controller:
$this->session->set_flashdata('errors', validation_errors());
View:
echo $this->session->flashdata('success');
And what is use of this function in controller $this->set_fleshdata(); the above method (set_flashdata) is enough to set your flashdata.
Also in the last line you forgot to write 'echo' before the form
<?php form_close(); ?> should be <?php echo form_close(); ?>

Resources