繁体   English   中英

将文章ID发送到ajax文件或直接在ajax文件中获取文章ID? 在joomla! 插入

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

我想将当前文章ID传递给Ajax文件。 Ajax文件的网址类似于www.web.com/plugins/system/ajax.php,因此使用JRequest :: getInt(id)总是解析0整数。 在非ajax文件中,我可以以相同的方式获取ID。 所以我想知道如何传递整数值,或者还有其他方法可以在ajax文件中获取商品ID?

<?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文件:

<?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

}

您必须添加一个属性作为vid并将id的值通过vid属性传递

例:

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

在脚本文件中,您必须像这样调用属性。

<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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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