?
Solved

Could you point how to correctly shows a Codeigniter's method reply to a view?

Posted on 2016-11-08
9
Medium Priority
?
32 Views
Last Modified: 2016-11-14
Hi Experts

Could you point how to correctly shows a Codeigniter's method reply to a view?

My try is (with no success)

jQuery code that call the Codeigniter's controller:
 $("body").on("click", ".call-ajax", function() {
          
   var id_rec = $(".id_reclamacao").val();
   var caminho = "<?php print base_url() . 'public/uploads/anexos/'; ?>";

   var postData = {
	 "id_rec": id_rec,
	 "anexo" : caminho + $(this).data("anexo")
	};

	$.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST"
	}).done(function(resp) {
		
	  //Where I guess the reply is obtained
	  $("#result").append($("<li/>").html(resp));
	});
  });

Open in new window


Controller's code:
<?php
public function delete_anexo()
{
	try
	{
		if (!$this->input->is_ajax_request())
		{
			$this->output->set_status_header(404);
			return;
		}

		//EF 2016/Nov - 01 
		$id_reclamacao = $this->input->post('id_rec');
		$anexo = $this->input->post('anexo');


		if (!$this->anexo_model_reclamacao->delete_anexo($id_reclamacao, $anexo))
			throw new Exception("Erro ao excluir", 1);

		$alert = 'Operação Realizada com sucesso.';
		
		//Where I tried to reply to the view:
		$this->output->set_content_type('application/json')->set_output(json_encode($alert));
	}
	catch (exception $e)
	{
		$alert = $e->getMessage();
	}

	bootbox_alert($alert);
}

Open in new window



View code:

<hr>
<div class="row">
	<div class="result">
	</div>
</div>

Open in new window


Thanks in advance!
0
Comment
Question by:Eduardo Fuerte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 total points
ID: 41878613
It looks mostly fine apart from a few minor issues

$this->output->set_content_type('application/json')->set_output(json_encode($alert))

Open in new window

Suggests a JSON return but in your AJAX code you are not telling jQuery the return is AJAX - you should still get a response though.

$.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST"
	}).done(function(resp) {
		
	  //Where I guess the reply is obtained
	  $("#result").append($("<li/>").html(resp));
	});

Open in new window

Should be
$.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST",
          dataType: 'JSON'
	}).done(function(resp) {
		
	  //Where I guess the reply is obtained
	  $("#result").append($("<li/>").html(resp));
	});

Open in new window


What do you see in your Console (F12) when you do the AJAX call?
0
 

Author Comment

by:Eduardo Fuerte
ID: 41878662
Hi @Julian

The jQuery code accordingly you pointed:
 $.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST",
	  dataType: "JSON"
	}).done(function(resp) {
		console.log("deleção OK");

	  $("#result").append($("<li/>").html(resp));
	});
  });

Open in new window


F12 - Console

img002
and

img003
0
 

Author Comment

by:Eduardo Fuerte
ID: 41878663
Still not running...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41878790
As I noted in my earlier emails - the change recommended was minor in terms of the problem at hand. It was something to keep in mind but not a solution to the problem.

Can we see what the POST looks like expanded. Click the triangle to the left to expand the POST and paste screen shot of that.
0
 

Accepted Solution

by:
Eduardo Fuerte earned 0 total points
ID: 41880371
Hi @Julian

By folowing your suggestion (here and in other questions) I did:

$.ajax({
	url: "reclamacao/delete_anexo",
	data: postData,
	type: "POST"
}).done(function (resp) {
   $("#"+postData.id_anexo).parent().remove();
	//$("#result").append($("<li/>").html(resp));
});

Open in new window

0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41880458
Is that working for you? Seems counter intuitive. You replaced an append with a remove?
0
 

Author Comment

by:Eduardo Fuerte
ID: 41880810
Yes. Since when a file's deletion is successful the  link that represents this file is simply removed.
The message  "Successful deletion" isn't necessary - just the link is removed.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41880835
Ah, understood - makes sense.
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41886061
Thank you for the guidance!
0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question