Refresh Ajax Button in Joomla module
I have a component and a module.
This module has:
<form action="#" method="post" name="signup">
<input type="text" name="address" id="address" value="Enter email
address" size="30" />
<input type="submit" name="submit" value="Signup" />
<div id="msg"></div>
</form>
<script type="text/javascript">
window.addEvent('domready', function(){
$('signup').addEvent('submit', function(e) {
//Prevent the submit event
new Event(e).stop();
var address = $('address').value;
// check email using regex
var address_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if(!address_regex.test(address)){ $('msg').innerHTML = 'Please enter a
valid email address.'; return; }
new
Ajax('index.php?option=com_traincomponent&task=adduser&template=raw',
{
method: 'get',
data: { 'address' : address },
onRequest: function() { $('msg').innerHTML = 'Please wait...'; },
onComplete: function(response) {
if (response != '') $('msg').innerHTML = response;
else msg.html('Please enter your email address.');
}
}).request();
});
});
</script>
Component com_traincomponent has a /controller/ajax.raw.php
function adduser() {
$app = JFactory::getApplication();
$model = $this->getModel('signup');
$data = $model->addUser();
echo $data;
$app->close();
}
function signup() {
$address = JRequest::getVar('address', '');
$msg = 'Thank you for registering!';
if ($address != '') {
$db = &JFactory::getDBO();
$address = $db->getEscaped($address); // escape the email string to
prevent sql injection
$db->setQuery("SELECT * FROM `#__users` WHERE `email`='$address'");
$db->Query();
if ($db->getNumRows() != 0) $msg = 'You are already registered, thank
you.';
else {
$db->setQuery("INSERT INTO `#__users` (`name`, `username`, `email`,
`usertype`, `block`, `gid`, `registerDate`) VALUES ('default',
'$address', '$address', 'Registered', 0, 18, '".date('Y-m-d
H:i:s')."')");
$db->query();
$user_id = $db->insertid();
$db->setQuery("INSERT INTO `#__core_acl_aro` (`section_value`,
`value`, `name`) VALUES ('users', $user_id, '$address')");
$db->query();
$aro_id = $db->insertid();
$db->setQuery("INSERT INTO `#__core_acl_groups_aro_map` (`group_id`,
`aro_id`) VALUES (18, $aro_id)");
$db->query();
}
} else {
$msg = 'Please enter an email address.';
}
return $msg;
}
But the problem is that when I click submit button, it still refreshes the
page and doesn't do the ajax function.
I'm actually getting the error Uncaught TypeError: Object [object Object]
has no method 'addEvent'
Any idea?
No comments:
Post a Comment