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):
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:
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.
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: