codeigniter with MVC pattern - codeigniter-2

I am working on ci and when i am going to delete my data then i want to generate a confirm message box on click delete link and my code is this.
view.php
id);?>">Delete
controller.php
function delete($id)
{
$this->include_user->delete($id);
redirect(site_url('admin_controller/fetch'));
}
Model.php
function delete($id)
{
$this->db->delete("user",array('id' => $id));
}

If you want to show confirmation message that either user really want to delete, then you can use jquery confirm box plugin and when click on delete anchor then on delete show the confirm box.
e.g: in js code
jConfirm('Do you really want to delete?', 'Delete Confirmation', function(r) {
if (r == true) {
}
}
In my case I am using jquery confirm box.
And in case if you want to show success message then you can send back a flag when record is deleted to controller, then checking the flag, if successfully record is deleted then pas success message to your message controller or wise failure message.

Related

send a mail to particular user while clicking on the button from view page in codeigniter

i have one view page called vendor view candidates page..in that page i display all the candidates details from database and i have user_id in that view page..and i gave button called release information..so when user click on the button the page will redirect to edit_candidates page with that particular candidate_id..and they will allow to insert email and phone number..
After they insert email and mobile number and then submit..i want to send mail to particular user_id..
This is my button:(edit candidates page)
<td>Release Contact Info</td>
Controller:
public function edit_candidates($id)
{
$data['editdata']=$this->CandidateModel->candidate($id);
if($this->input->post())
{
$this->CandidateModel->update_candidate($this->input->post(),$id);
redirect(base_url('index.php/Candidate/vendor_view_candidates'));
}
$this->load->view('Candidates/edit_candidate',$data);
}
Can anyone help me...i tried a lot..but i didn't get any idea on how to do this..
Thanks in advance..
Use the following code to fix your issue
public function edit_candidates($id)
{
$data['editdata']=$this->CandidateModel->candidate($id);
if($this->input->post('update_form'))
{
$userEmail = $this->CandidateModel->get_userinfo($data['editdata']->user_id);
//Pass this $userEmail into mail function it will send email
$this->CandidateModel->update_candidate($this->input->post(),$id);
redirect('index.php/Candidate/vendor_view_candidates','refresh');
}
else
{
$this->load->view('Candidates/edit_candidate',$data);
}
}
Let me know if it not fix your issue

Laravel 5: Sending email with attached .pdf file generated from dynamic page

I have a job website. Job seeker users can register and build their profile (online resume), and it is used for one click application.
When job seekers logged in and click "Apply Now" button to any job, I want Laravel to send an email attached with .PDF resume (generated from their online resume building page) to employer's inbox.
Note: I do not want to send their uploaded resume (.pdf/.doc) to employer's inbox because I want to promote my website's branding / resume template.
Please tell me how to achieve this. If any source code provided, I would appreciated.
thank you.
You can use a html to pdf converter (something like http://github.com/barryvdh/laravel-dompdf). This will create the users profile in PDF format.
After the user clicked the button there will be an email sended to the employer with the attached pdf from the user.
It will look something like this:
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->to($user->email, $user->name)->subject('Your Reminder!');
$m->attach($pathToFile);
});
And with custom mail per email:
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
$m->attach($pathToFile);
});
This will create the PDF, save it to the storage directory. You can use the views for the design:
public function createCollageCron($load_id) {
$load = Loads::findOrFail($load_id);
$pdf = PDF::loadView('pdf.collage', compact('load'));
if ($pdf->save(storage_path('app/loadings/'.$load->id.'/collage.pdf'))) {
return true;
} else {
return false;
}
}
Hope this works!

client side validation not working for model window / ajax-loaded-form in yii

I am using Yii-user extension in the main layout i have a sign up link which is common to all the Cmenu
view/main layout
echo CHtml::link('Signup','#',array('id'=>'regi'));
$("#regi").click(function(){
$.ajax({
type:'GET',
url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/user/registration',
success:function(res){
$("#dispdata").show();
$("#dispdata").html(res);
}
});
});
<div id="dispdata"><div>
**yii user extension **renders this perfectly and even submit its correctly if form values a re valid.
but if the values are incorrect and blank it redirect to url .../user/registration
which is not what my need .I need guidance what do i do such that if the values are incorrect or blank it should not redirect and display the errors in model window.
I did tried but hardly could get the satisfied results
if i place the following the model window itself doesnt appear what do i do
module registrationController i placed
....//some code here (**in yiiuser register controller**)
if ($model->save()) {
echo CJSON::encode(array(
'status'=>'success',
));
}
....//some code here...
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderPartial('registration',array('model'=>$model,),false,true);
in module view registration
<?php echo CHtml::ajaxSubmitButton(Yii::t('registration'),CHtml::normalizeUrl(array('user/registration','render'=>false)),array('dataType'=>'json',
'success'=>'function(data) {
if(data != null && data.status == "success") {
$("#registration-form").append(data.data);
}
}')); ?>
can anyone please guide me am working past 10 ten days tried every hook or crook method but could not obtain the results......how can the model window with client side validation be done appear..... Please guide me or let me know something better can be done
rules in registration model
if (!(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')) {
array_push($rules,array('verifyCode', 'captcha', 'allowEmpty'=>!UserModule::doCaptcha('registration')));
as well was not with attributes for reqired field
have changed to
array_push($rules,array('verifyCode', 'captcha','message' => UserModule::t("captcha cannot be blank.")));
and added the verifycode to required field
yet not working,
The simple way is using render method in your Ajax action and creating empty layout for this action. If you do so, validation scripts will be included in the server response. Also you need to exclude jquery.js and other script with Yii::app()->clientScript->scriptMap and include them in main layout always.

How to repopulate form after form validation and also keep the URI?

I have a problem repopulating my form after validation fails. Problem is my url contains an additional uri which I access by clicking a link. This is what it looks like:
http://www.example.com/admin/trivia/add/5
At first trouble was that the segment 4 of the uri completely disappeared, so even though the validation errors showed and the form was repopulated, I lost my added uri.
Then I found in another question that the solution was to set form open like this:
echo form_open(current_url());
Problem is now it isn't showing any validation errors and the form is not repopulated. Is there a way to achieve this?
This is what my controller looks like:
function add()
{
$data = array('id' => $this->uri->segment(4));
if($_POST)
{
$this->_processForm();
}
$this->load->view('admin/trivia_form', $data);
}
And inside _processForm() I got all the validation rules, error message and redirecting in case success.
[edit] Here is my _processForm() :
function _processForm()
{
$this->load->library('form_validation');
//validation rules go here
//validation error messages
$this->form_validation->set_rules($rules);
$this->form_validation->set_error_delimiters('<div style="color:red">', '</div>');
if ($this->form_validation->run())
{
//get input from form and assign it to array
//save data in DB with model
if($this->madmin->save_trivia($fields))
{
//if save is correct, then redirect
}
else
{
//if not show errors, no redirecting.
}
}//end if validation
}
To keep the same url, you can do all things in a same controller function.
In your controller
function add($id)
{
if($this->input->server('REQUEST_METHOD') === 'POST')// form submitted
{
// do form action code
// redirect if success
}
// do your actual stuff to load. you may get validation error in view file as usual if validation failed
}
to repopulate the form fields you are going to need to reset the field values when submitting it as exampled here and to meke it open the same page you can use redirect() function as bellow:
redirect('trivia/add/5','refresh');
i don't know what you are trying to do, but try this to repopulate the form with the values user entered
<?php
echo form_input('myfield',set_value('myfield'),'placeholder="xyz"');
?>

Using browser back button in triggers flash message in CakePHP again

I just came across a bit of a puzzling situation. My controller action looks like this:
public function myaction($eventId = false) {
if(!$eventId) {
//list all events
$data = foo;
} else {
if(!$this->Event->findById($eventId) )
{
$this->Session->setFlash('myerror message', 'flash_frontend_message');
$this->redirect(array('controller' => 'events', 'action' => 'myaction'));
} else {
// display event information
$data = foo;
}
}
$this->set($data);
}
If I call the function with an $eventId that is not found in the database, it outputs an error message and the user gets redirected back to the list of all events. However lets say I then select an eventId which is valid, view the relevant information and then press the browser back button, the previous flash message gets out put again, even though the URL in this case does not contain the $eventId.
I suppose what happens is that the page gets loaded from the browser cache rather then reloaded. I have tried to avoid storing the view in the cache like this:
<!--nocache-->
<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash('auth'); ?>
<!--/nocache-->
but still, the flash message gets displayed.
Any idea how to prevent this behavior?
Is there perhaps a way to clear the flash after it has been displayed?
you are talking about the right cache but trying to fix it with the wrong cache.
<!--nocache-->
is php/html no cache statements in the file itself. it has nothing to do with the browser cache.
what you need to be doing is setting the right headers.
there is a convinience method in the request object for this exact thing:
http://book.cakephp.org/2.0/en/controllers/request-response.html#interacting-with-browser-caching
I use it in all my normal frontend actions via AppController:
public function beforeRender() {
$this->response->disableCache();
...
}

Resources