CodeIgniter Forums
CI jQUERY ajax POST data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: CI jQUERY ajax POST data (/thread-23342.html)



CI jQUERY ajax POST data - El Forum - 10-07-2009

[eluser]joeizang[/eluser]
Hi y'all,

Please look at this codeSadI AM SORRY IT'S ALOT OF JQUERY BUT Y'ALL BEEN NICE)

$('ul#navigation li ul li>a').click(function(){
var active = $(this).text();
$('#artcontent p').empty();
$.ajax({
type: 'POST',
url: 'homepage/readarticle',
data: active,
success: function(databack){
$('#artcontent').append(databack);
}
})
});

I have a ul li a that are children of parent ul navigation and li. what I am trying to do is to load an article page. This works but it reads the database and loads all the articles in my database instead of the one identified by active. I would like to know what I am doing wrong or is there a better way to go about this?

Ok the 'homepage/readarticle' is the 'controller/method' in my codeigniter controller which loads the view that renders the article.

As for the serialize line, I actually put in active without the serialize and could catch the text of the link with firebug using the usual console.log(). Like I said, it reads the articles alright but it reads all the articles from my database. instead of fetching the one who's title is equal to the name in the active variable(that would be the text of the link)

This is the php that this calls to:
<?php
function readarticle()
{
$articlename = $this->input->post('active');
$output = $this->articles->displayby_name($articlename);

if($output){
$data['article'] = $output;
}
$this->load->view('loadarticle',$data);
}
?>

Any thoughts?


CI jQUERY ajax POST data - El Forum - 10-07-2009

[eluser]jdfwarrior[/eluser]
1. Have you verified that the $articlename variable is being set correctly?
2. Have you verified that the correct SQL is being generated? (use $this->db->last_query() to grab it)


CI jQUERY ajax POST data - El Forum - 10-07-2009

[eluser]joeizang[/eluser]
SELECT `articletitle`, `articlebody`, `articleauthor` FROM (`articles`) WHERE `articletitle` = 0

Thanks jdfwarrior. I did and this is the resultant query. I presume the problem is the articletitle=0. but how do you make the articletitle to be text and not a number then. I thought doing this (from my model) was right?

<?php

function displayby_name($name)
{
$this->db->select("articletitle,articlebody,articleauthor");
$this->db->from('articles');
$this->db->where('articletitle',$name);
$Q = $this->db->get();

if($Q->num_rows() > 0){
$text = $Q->result_array();
}
return $text;
}

?>

or is there another way?

Thanks for the help.


CI jQUERY ajax POST data - El Forum - 10-07-2009

[eluser]jdfwarrior[/eluser]
I think I found your issue. You are using the AJAX function to post to the function. With a POST, you have to specify a name for the data that you are passing.

There are several examples listed in the jQuery documentation.
http://docs.jquery.com/Ajax/jQuery.ajax#options

Basically, where you set your data, it should read something like..
Code:
$.ajax({
  type: ‘POST’,
  url: ‘homepage/readarticle’,
  data: "active="+ active,
  success: function(databack){
    $(’#artcontent’).append(databack);
    }
  })
});

or

Code:
$.ajax({
  type: ‘POST’,
  url: ‘homepage/readarticle’,
  data: ({active : active}),
  success: function(databack){
    $(’#artcontent’).append(databack);
    }
  })
});

If the above code, the first mention of active, would be a field/named reference for the data. Allowing you to use $this->input->post("active"). The second mention would be the value that you are passing to it.
Remember when your posting data, it typically uses the field name as that identifier.. with an ajax post, you have to give it a name.

Try that out and see if it works.

Good luck


CI jQUERY ajax POST data - El Forum - 10-07-2009

[eluser]joeizang[/eluser]
thanks man,

I was thinking already in these lines but was not sure. Thanks your answer was spot on. it now picks just the active like specified in the jquery code.

God bless.


CI jQUERY ajax POST data - El Forum - 10-07-2009

[eluser]jdfwarrior[/eluser]
[quote author="joeizang" date="1254977009"]thanks man,

I was thinking already in these lines but was not sure. Thanks your answer was spot on. it now picks just the active like specified in the jquery code.

God bless.[/quote]

Glad I could help