简体   繁体   中英

Send article id to ajax file or get article id straight in ajax file ? In joomla! plugin

I want to pass current article id to ajax file. Url of ajax file is something like www.web.com/plugins/system/ajax.php so using JRequest::getInt(id) always parses 0 integer. In non ajax file I can get ID the same way. So I'd like to know how to pass integer value or maybe there's other way of getting article id in ajax file ?

<?php
defined( '_JEXEC' ) or die;
define( 'DS', DIRECTORY_SEPARATOR );
?>

<?php
class plgSystemRatingx extends JPlugin

{
    public function onContentBeforeDisplay()
    {
?>
<?php echo JRequest::getInt('id'); ?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".like").click(function()
{
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
$("#votebox").slideDown("slow");

$("#flash").fadeIn("slow");

$.ajax
({
type: "POST",
url: "/joomla/plugins/system/ratingx/conf.php",
data: dataString,
cache: false,
success: function(html)
{
$("#flash").fadeOut("slow");
$("#content").html(html);
} 
});
});

$(".close").click(function()
{
$("#votebox").slideUp("slow");
});

});
</script>
<?php echo JURI::current(); ?>

<div style="margin:50px">
<a href="#" class="like" id="1" name="up">Like</a> -- <a href="#" class="like" id="1" name="down">Dislike</a>
<div id="votebox">
<span id='close'><a href="#" class="close" title="Close This">X</a></span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="content">

</div>

</div>
</div>
<?php


        return true;
    }
}

AJAX FILE:

<?php
// Set flag that this is a parent file
define('_JEXEC', 1);

// No direct access.
defined('_JEXEC') or die;

define( 'DS', DIRECTORY_SEPARATOR );

define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..'.DS.'..' );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );



$db = &JFactory::getDbo();

if(JRequest::getInt('id'))
{

$id = JRequest::getInt('id');
$name = JRequest::getVar('name');

$queryx = "SELECT id from messages";
$db->setQuery($queryx);
$db->query($queryx);
$idx = $db->loadObjectList();




$query = "update messages set $name=$name+1 where id='$id'";
$db->setQuery( $query );
$db->query( $query ) or die('blo5gai');

$query2 = "select up,down from messages where id='$id'";
$db->setQuery( $query2 );
$db->query( $query2 ) or die('blo5gai');

$vote = $db->loadObject();

$up_value= $vote->up;
$down_value = $vote->down;

$total=$up_value+$down_value;

$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;

?>
<div style="margin-bottom:10px">
<b>Ratings for this blog</b> ( <?php echo $total; ?> total)
</div>
<table width="700px">
<?php echo JURI::current(); ?>
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>

<tr>
<td width="30px"></td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>

</table>

<?php

}

You have to add one attribute as vid and pass the value of the id through the vid attribute

example:

<a href="#" class="like" id="1" name="up" vid="<?php echo JRequest::getInt('id'); ?>">Like</a> 

In the script file, you have to call the attribute like this.

<script type="text/javascript">
    $(document).ready(function()
    {
        $(".like").click(function()
        {
            var vid=$(this).attr("vid");
            alert(vid);
        }
    });
</script>
<?php
// Set flag that this is a parent file
define('_JEXEC', 1);

// No direct access.
defined('_JEXEC') or die;

define( 'DS', DIRECTORY_SEPARATOR );

define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..'.DS.'..' );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$id = JRequest::getInt('id');
$db = &JFactory::getDbo();

if($id)
{       
    $name   = JRequest::getVar('name');

    $query->select('id');
    $query->from('messages');   
    $db->setQuery($query);
    $db->query($query);
    $idx = $db->loadResult();


    $query->update('messages');
    $query->set('name = '.$name+1);
    $query->where('id='.$id);
    $db->setQuery($query);
    $db->query();

    $query->select('up,down');
    $query->from('messages');
    $query->where('id='.$id);
    $db->setQuery($query);
    $db->query($query);
    $vote = $db->loadObject();

    $up_value= $vote->up;
    $down_value = $vote->down;

    $total=$up_value+$down_value;

    $up_per=($up_value*100)/$total;
    $down_per=($down_value*100)/$total;

?>
    <div style="margin-bottom:10px">
        <b>Ratings for this blog</b> ( <?php echo $total; ?> total)
    </div>

    <table width="700px">
        <?php echo JURI::current(); ?>
        <tr>
            <td width="30px"></td>
            <td width="60px"><?php echo $up_value; ?></td>
            <td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
        </tr>

        <tr>
            <td width="30px"></td>
            <td width="60px"><?php echo $down_value; ?></td>
            <td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
        </tr>
    </table>
<?php

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM