Trying to add data to sql from link click and return results via jquery or ajax

Posted by Jay Schires on Stack Overflow See other posts from Stack Overflow or by Jay Schires
Published on 2011-01-01T22:25:09Z Indexed on 2011/01/02 0:53 UTC
Read the original article Hit count: 179

Filed under:
|
|
|

I am not familiar with jquery or ajax, but i do know it is whats needed to perform the action I want. I have created a wordpress plugin that updates a database table based on the users click. Right now it refreshes the page to return the results, but I want to stop the page refresh and return data via ajax I believe. If anyone is interested in helping me figure this out I would be very appreciative or even willing to pay.

Thanks!

Here is the plugin code:

function BoardLikeItGetDelim($postid)
{
  global $wp_rewrite;

  if($wp_rewrite->using_permalinks())
  {
    if(isset($_GET['mbpost']))
      return "?mbpost=".$postid."&";
    return "?";
  }
  else
  {
    if(isset($_GET['mbpost']))
      return "&mbpost=".$postid."&";
    return "&";
  }
}

function AddBoardLikeItButton($postid)
{
  global $user_ID;


  if(isset($_GET['board-like-it-action']) && $_GET['board-like-it-action'] == "like" && $_GET['bpid'] == $postid)
    BoardLikeItLike($user_ID, $_GET['bpid']);


  if(isset($_GET['board-like-it-action']) && $_GET['board-like-it-action'] == "unlike" && $_GET['bpid'] == $postid)
    BoardLikeItUnLike($user_ID, $_GET['bpid']);

  $num_likes = BoardLikeItGetNumLikes($postid);

  if(!BoardLikeItIsLiked($user_ID, $postid))
    echo "<HREF LINK='".BoardLikeItGetDelim($postid)."board-like-it-action=like&bpid=".$postid."#mngl-board-post-message-".$postid."'>Like</a>  ".$num_likes."" . "<br/>";
  else
    echo "<HREF LINK ='".BoardLikeItGetDelim($postid)."board-like-it-action=unlike&bpid=".$postid."#mngl-board-post-message-".$postid."'>Un-Like</a>  " . "<br/><span style='display: inline-block; padding: 0px; bottom: -5px; position: relative; border: 0px;'><IMAGE='". get_bloginfo('wpurl')."/wp-content/plugins/board-like-it/top-up.png' /></span><div style='-moz-border-radius: 4px;
    -khtml-border-radius: 4px;
    -webkit-border-radius: 4px;
    font-family: Verdana, Geneva, sans-serif;
    font-size: 10px;
    color: #000;
    background-color: #B8C9DB;
    width: 90%;
    margin: 0px;
    display: block;
    padding-top: 4px;
    padding-right: 5px;
    padding-bottom: 4px;
    padding-left: 6px;'>" . "<IMAGE='". get_bloginfo('wpurl')."/wp-content/plugins/board-like-it/thumb_up.png'/>  " .BoardLikeItShowLikers($postid). "like this." . "</div>";
}

function BoardLikeItShowLikers($postid)
{
  global $wpdb;
  $result = $wpdb->get_var($wpdb->prepare("SELECT `likers` FROM ".BoardLikeItGetDBName()." WHERE `mngl_id` = {$postid}"));
  $results = explode(',', $result);
  $names = "";

  if($results[0] != "")
    foreach($results as $r)
    {
      $userinfo = get_usermeta($r, 'user_login');
      $names .= $userinfo.", ";
    }

  return $names;
}

function BoardLikeItGetNumLikes($postid)
{
  global $wpdb;
  $result = $wpdb->get_var($wpdb->prepare("SELECT `likers` FROM ".BoardLikeItGetDBName()." WHERE `mngl_id` = {$postid}"));
  $results = explode(',', $result);

  if($results[0] != '')
    return count($results)."<br/><span style='display: inline-block; padding: 0px; bottom: -5px; position: relative; border: 0px;'><IMAGE='". get_bloginfo('wpurl')."/wp-content/plugins/board-like-it/top-up.png' /></span><div style='-moz-border-radius: 4px;
    -khtml-border-radius: 4px;
    -webkit-border-radius: 4px;
    font-family: Verdana, Geneva, sans-serif;
    font-size: 10px;
    color: #000;
    background-color: #B8C9DB;
    width: 90%;
    margin: 0px;
    display: inline-block;
    border: 0px;
    padding-top: 0px;
    padding-right: 5px;
    padding-bottom: 1px;
    padding-left: 6px;'>" . "<IMAGE='". get_bloginfo('wpurl')."/wp-content/plugins/board-like-it/thumb_up.png'/>  " .BoardLikeItShowLikers($postid). "likes this." . "</div>";
  else
    return "";
}

function BoardLikeItLike($user_ID, $postid)
{
  global $wpdb;
  $likers = array();
  $likersnew = array();
  $result = $wpdb->get_var($wpdb->prepare("SELECT `likers` FROM ".BoardLikeItGetDBName()." WHERE `mngl_id` = {$postid}"));
  $results = explode(',',$result);

  if($results[0] != "")
  {
    if(!in_array($user_ID, $results))
      $results[] = $user_ID;
    $likers = implode(',',$results);
    $wpdb->query($wpdb->prepare("UPDATE ".BoardLikeItGetDBName()." SET `likers` = '{$likers}' WHERE `mngl_id` = {$postid}"));
  }
  else
  {
    $likersnew[] = $user_ID;
    $likersnew = implode(',',$likersnew);
    $wpdb->query($wpdb->prepare("INSERT INTO ".BoardLikeItGetDBName()." (`mngl_id`, `likers`) VALUES ('{$postid}', '{$likersnew}')"));
  }
}


function BoardLikeItUnLike($user_ID, $postid)
{
  global $wpdb;
  $likers = array();
  $result = $wpdb->get_var($wpdb->prepare("SELECT `likers` FROM ".BoardLikeItGetDBName()." WHERE `mngl_id` = {$postid}"));
  $results = explode(',', $result);

  if(in_array($user_ID, $results))
  {
    $results = BoardLikeItRemoveFromArray($results, $user_ID);
    if(!empty($results))
    {
      $likers = implode(',', $results);
      $wpdb->query($wpdb->prepare("UPDATE ".BoardLikeItGetDBName()." SET `likers` = '{$likers}' WHERE `mngl_id` = {$postid}"));
    }
    else
    {
      $wpdb->query($wpdb->prepare("DELETE FROM ".BoardLikeItGetDBName()." WHERE `mngl_id` = {$postid}"));
    }
  }
}


function BoardLikeItIsLiked($user_ID, $postid)
{
  global $wpdb;
  $result = $wpdb->get_var($wpdb->prepare("SELECT `likers` FROM ".BoardLikeItGetDBName()." WHERE `mngl_id` = {$postid}"));
  $results = explode(',', $result);

  if(in_array($user_ID, $results))
    return true;
  else
    return false;
}

function BoardLikeItActivate()
{
  global $wpdb;
  $charset_collate = '';

  if($wpdb->has_cap('collation'))
  {
    if(!empty($wpdb->charset))
      $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    if(!empty($wpdb->collate))
      $charset_collate .= " COLLATE $wpdb->collate";
  }

  $table_sql = "CREATE TABLE ".BoardLikeItGetDBName()."(
  `mngl_id` int(11) NOT NULL,
  `likers` longtext NOT NULL,
  PRIMARY KEY (`mngl_id`))
  {$charset_collate};";

  require_once(ABSPATH.'wp-admin/includes/upgrade.php');
  dbDelta($table_sql);
}

function BoardLikeItGetDBName()
{
  global $wpdb;
  return $wpdb->prefix."board_like_it";
}

function BoardLikeItRemoveFromArray($arr, $key)
{
  $new = array();
  foreach($arr as $j => $i)
  {
    if($i != $key)
      $new[] = $i;
  }
  return $new;
}

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery