External PHP file to access row id on details page

Mustafa_s

Member
I have the following Javascript code that upon button click (onClick) a) creates a nice notification box and b) calls a PHP file. These two combined work 99.9%, I'm missing the .01% of the equation to make this work. The Javascript code is this for reference (this code is OK):

JavaScript:
<script type="text/javascript" src="/lobibox/lib/jquery.1.11.min.js"></script>
<script type="text/javascript" src="/lobibox/dist/js/notifications.min.js"></script>
<link href="/lobibox/dist/css/lobibox.min.css" rel="stylesheet"/>
<link href="/lobibox/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>

<button id="buttondanger" class="btn btn-danger" onClick="FunctionDanger()">remove</button>
<button id="buttonsuccess" class="btn btn-success" onClick="FunctionSuccess()">add</button>

<script>
function FunctionSuccess(){

$.post("/copy/favorites_add.php");

Lobibox.notify('success', {
iconSource: 'fontAwesome',
size: 'mini',
delay: 8000,
sound: false,
title: 'Success!',
msg: 'More Success message, yay!'
});
}
</script>

Note the Ajax call to the PHP file "favorites_add.php" located under the /copy folder. The PHP catures variables and inserts data into the DB. The code for that PHP is as follows:

PHP:
<?php
error_reporting(1);

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );

require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');

$user = JFactory::getUser();
$username = $user->username;
$pre = user_;
$user_xxx = $pre .$username;
$rowid = $_REQUEST['rowid'];
$rowid_only = strtok($rowid, ':');


$db =& JFactory::getDBO();
$db->setQuery("select id from yi5vu_modules WHERE title = '$user_xxx'");
$db->query();
$moduleid = $db->loadResult();

//echo "$moduleid";
//echo "$user_xxx";
//echo "$rowid_only";

// Get a db connection.
$db1 = JFactory::getDbo();
// Create a new query object.
$query = $db1->getQuery(true);
// Insert columns.
$columns = array('type_alias', 'core_content_id', 'content_item_id', 'tag_id', 'tag_date', 'type_id');
// Insert values.
$values = array('com_content.article', '1', "$rowid_only", "$moduleid", "0000-00-00 00:00:00", '1');
// Prepare the insert query.
$query
    ->insert($db1->quoteName('yi5vu_contentitem_tag_map'))
    ->columns($db1->quoteName($columns))
    ->values(implode(',', $db1->quote($values)));
$db1->setQuery($query);
$db1->execute();
?>

So as mentioned above, this all works very well for the most part. The part that does not work has me scratching my head. When I echo "$rowid_only" onto the page I'm viewing (a record details page) I actually get the row_id number (ie: 875). But when I actually click the button (on a record details page) to execute the PHP file the "$rowid_only" variable inserted as 0 inside the database, the other variables work fine.

Here's a twist, if I run the PHP code outside of the onClick Javascript I'm able to insert into the DB without any problems or code changes to the PHP file.

Why would this be? How come I can see the row_id when I echo but not when I insert the variable into the db? I ensured the "content_item_id" field, where the "$rowid_only" variable is supposed to be entered in the DB accepts both integers and words.
 
Last edited:
var_dump($userid); is giving me: 875string(3) "875" but the echo $userid is giving me just 875.

I didn't know this was going on, should have done a dump hours ago lol. I feel like I'm getting close, clearly the strktok is screwing things up.
 
I think you need to pass the rowid, from the front-end back. Since your back-end script has now idea what row your are currently working on.
Code:
var v = jQuery('#your row id element name').val();
$.post("/copy/favorites_add.php", { rowid: v});

You should not need your strtok anymore

Paul
 
That's exactly what it was, and your suggestion worked like a charm with some minor tweaks.

Thank you Paul.
 
Also, why not use the baked in user_ajax.php method, rather than rolling your own framework includes?

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top