Skip to Content Skip to Menu

Responding To Ajax Username Generator

  • dpweberza
  • dpweberza
  • OFFLINE
  • Posts: 1
  • Thanks: 0
  • Karma: 0
14 years 4 months ago #137235 by dpweberza
Responding To Ajax Username Generator was created by dpweberza
Hi all,

My plug-in adds a username generator button under the username field on registration.

Basically my plug-in just outputs this jQuery
[code:1]
$('#username').after('<br><input type="button" id="generator" value="Generate Username"/>');
$('#generator').click(function(){
$.post("/home/components/com_comprofiler/plugin/user/plug_registration2/ajax.php",{ajax : "getUsername"}, function(data){
$('#username').val(data);
});
[/code:1]

Now that seems to work, but I always get "Direct Access to this location is not allowed" or errors like $mainframe doesnt exist or could not import some cb class.

Ajax.php
[code:1]
// Security
if (!isset($_POST) && isset($_POST) == 'getIDKey') {
die('Direct Access to this location is not allowed.');
}

// Set flag that this is a parent file
define('_VALID_MOS', 1);
/* ## Emulating Joomla environment */
require_once( '../../../../../configuration.php' );

/*
* Try Include CB Framework
*/
global $_CB_framework, $mainframe;

if (defined('JPATH_ADMINISTRATOR')) {
if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php')) {
echo 'CB not installed!';
return;
}

include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' );
} else {
if (!file_exists($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php')) {
echo 'CB not installed!';
return;
}

include_once( $mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php' );
}

cbimport('cb.plugins');


require_once 'cb.registration2.php';

/**
*
* AJAX Handling
*
*/
switch ($_POST) {
case "getUsername" : getUsername();
}

// Return unique random username
function getUsername() {
$class = new getRegistration2();
echo $class->randUsername();
}
[/code:1]

Have tried lots of ways to make this work, and have looked at many 3rd party cb ajax plugins but they use complicated ajax libraries.

Can anyone just suggest how I might return a value from a function in my plug-in class to the ajax request my generator button creates.

Thanks in advance,

David

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum