Home » Topics » Pods 1.x » Help adding ajax to a pod page
Help adding ajax to a pod page
This topic contains 1 reply, has 2 voices, and was last updated by logikal16 3 years, 4 months ago.
-
AuthorPosts
-
January 20, 2010 at 3:50 pm #162182
I am trying to build function to vote for a particular essay and I just need a little help figuring out how to incorporate ajax so it updates right on the page. Right now I have a pod page that pulls the current number of votes for each essay and displays it above the essay. There is a form button that when pressed pulls up a php file that adds one to the vote count and displays the new vote count on the update page.
Can anyone point me in the right direction on how to incorporate ajax to have the result sent back to the original pod page?
This is my pod page:
<pre>
<?php
$orderby = ‘t.date ASC’;
$limit = 100;
$where = ‘t.featured_essay = 1′;
$featuredEssay = new Pod(‘essay’);
$featuredEssay->findRecords($orderby,$limit,$where);
while ($featuredEssay->fetchRecord()) {
$id = $featuredEssay->get_field(‘id’);
$title = $featuredEssay->get_field(‘name’);
$date = $featuredEssay->get_field(‘date’);
$date = date("m/d/Y",strtotime($date));
$essay = $featuredEssay->get_field(‘essay’);
$votes = $featuredEssay->get_field(‘votes’);
$votes = round($votes);
?>123456789<form id="recommend" method="post" action="../update"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input id="submit" type="submit" name="vote" value="Recommend this essay" /></form><p id="total_votes">Total number of votes: <?php echo $votes; ?></p><h3><?php echo $title; ?></h3><p><?php echo $date; ?></p><div><?php echo $essay; ?></div><?php } ?>;
</pre>and my update page:
<pre>
<?php
if(isset($_POST['vote'])) {
$id=$_POST['id'];12345678910111213// Add a vote to the database$result = pod_query("UPDATE wp_pod_tbl_essay set votes=votes+1 WHERE id=$id");$orderby = 't.id';$limit = 100;$where = "t.id ='$id'";$vote_pod = new Pod('essay');$vote_pod->findRecords($orderby,$limit,$where);while ($vote_pod->fetchRecord()) {$votes = $vote_pod->get_field('votes');$votes = round($votes);}echo ($votes);}?>
</pre>January 20, 2010 at 3:50 pm #162183Laura,
The best example for getting AJAX working is to look at the code for Pods itself. Look in core/manage_pods.php. For starters, here’s a really good example:
<pre>
function dropPage() {
if (confirm("Do you really want to drop this page?")) {
jQuery.ajax({
type: "post",
url: "<?php echo PODS_URL; ?>/ajax/api.php",
data: "action=drop_page&id="+page_id,
success: function(msg) {
// "msg" is whatever is returned back from the AJAX call
// Do something here (like update some HTML elements)
}
});
}
}
</pre>The above function passes 2 variables to the file "api.php": action, and id.
-
AuthorPosts
You must be logged in to reply to this topic.