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.
Related
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.
<?php
$role_class = new RoleModel;
$role_name = "";
foreach ($role_class->getInfoById("role_id") as $row) {
# code...
$role_name = $row->role_name;
}
?>
<fieldset class="g12 go pb_s">
<legend>Basic Info</legend>
<div class="g12 go">
<div class="g7 go">
<div class="g2">
<label for="groupName">Role Name:</label>
</div>
<div class="g4">
<input type="text" placeholder="Type Role Name here..." name="role_name" value="<?php echo $role_name; ?>" required="required">
</div>
</div>
</div>
</fieldset>
You can use PHP's $post to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
In your Html
<form name="form" action="" method="post">
<input type="text" name="role_name" id="role_name" value="<?php echo $role_name;?>">
</form>
In your .php, you can get the value like below
$role_name=$post['role_name'];
Since the editor edited his questions, i will add more example.
For example, i got a list of data want to show in the html, in controller, i will put into an array first.
$form_fields = array('username', 'ref', 'password', 'password_confirm')
after that in html, i will do like this
<?php foreach($__form_fields as $field) {?>
<input type="text" placeholder="<?php echo $field?>" name="<?php echo $field? >" value="" required="required">
<?php }?>
which the above code will loop and the input field will have he name
I am working on CodeIgniter custom form, In this form, I have used CSRF token after form submit I want to check in controller after form submit if CSRF token is valid or not, can anyone please help me for this issue?
PHP Form code
<form role="form" data-parsley-validate="" novalidate="" class="mb-lg" action="?c=
<?php echo isset($controller) ? $controller : "welcome"; ?>&m=login" method="post">
<div class="form-group has-feedback">
<input name="email" id="exampleInputEmail1" type="email" placeholder="Enter email" autocomplete="off" required class="form-control">
<span class="fa fa-envelope form-control-feedback text-muted"></span>
</div>
<div class="form-group has-feedback">
<input name="password" id="exampleInputPassword1" type="password" placeholder="Password" required class="form-control">
<span class="fa fa-lock form-control-feedback text-muted"></span>
</div>
<div class="clearfix">
<div class="pull-right">
Forgot your password?
</div>
</div>
<?php
$csrf = array(
'name' => $this->security->get_csrf_token_name(),
'hash' => $this->security->get_csrf_hash()
);
?>
<input type="hidden" name="<?php echo $csrf['name'];?>" value="<?php echo $csrf['hash'];?>" />
<button type="submit" class="btn btn-block btn-primary mt-lg">Login</button>
</form>
CodeIgniter does this for you automatically. If it is not valid it will do a show_error with show_error('The action you have requested is not allowed.', 403);
Relevant functions can be found in the /system/core/security class, functions: csrf_verify() and csrf_show_error() (if invalid).
If you have understood by now, if you have csrf enabled in the config and you do not have the appropriate csrf hidden field or use form_open (which adds the field for you) then when you post the request it will fail with the above message. The same goes for AJAX requests - to automate this for ajax requests you can add this to the top of you page wherever there is an ajax request:
<script type="text/javascript">
token["<?php echo $this->security->get_csrf_token_name(); ?>"] = "<?php echo $this->security->get_csrf_hash(); ?>";
jQuery.ajaxSetup({data: token, type: "POST"});
</script>
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.
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 :)