Forums

how to implement these code in cfv4 ?

eddddd 16 Feb, 2012
hi i found a tutorial here http://greyhead.net/chronoforms/using-ajax-to-look-up-e-mail-addresses but the problem is that I dont where should i put the code in cfv4 .
where should i put this block ?



window.addEvent('domready', function() {
  // set the url to send the request to
  var url = 'index.php?option=com_chronocontact
    &chronoformname=form_name&task=extra&format=raw';
  var email = $('email');
  email.addEvent('blur', function() {
    // clear any background color from the input
    email.setStyle('background-color', 'white');
    // check that the email address is valid
    regex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
    var value = email.value.trim();
    if ( value.length > 6 && regex.test(value) ) {
      // if all is well send the JSON request
      var jSonRequest = new Json.Remote(url, {
        onComplete: function(r) {
          // check the result and set the background color
          if ( r.email_ok ) {
            email.setStyle('background-color', 'green');
          } else {
            email.setStyle('background-color', 'red');
          }
        }
      }).send({'email': email.value});
    } else {
      // if this isn't a valid email set background color red
      email.setStyle('background-color', 'red');
    }
  });
});


and this

<?php
// clean up the JSON message
$json = stripslashes($_POST['json']);
$json = json_decode($json);
$email = strtolower(trim($json->email));
// check that the email field isn't empty
$response = false;
if ( $email ) {
  // Check the database
  $db =& JFactory::getDBO();
  $query = "
    SELECT COUNT(*)
      FROM `#__users`
      WHERE LOWER(`email`) = ".$db->quote($email).";
  ";
  $db->setQuery($query);
  $response = (bool) !$db->loadResult();
}
$response = array('email_ok' => $response );
//send the reply
echo json_encode($response);
// stop the from running
$MyForm->stopRunning = true;
die;
?> 

---------------------------------------------------------
GreyHead 16 Feb, 2012
Hi eddddd,

What checking do you need to do and where?

If you use the Joomla! Registration action then there will be an error message shown if the Username or Email are already in use.

Bob
eddddd 16 Feb, 2012
Mr Greyhead , I added the "joomla registration" action but when click submit if username taken it will not save to the database and it wont display any error message .

on the action section i have these installed
show html
joomla user registration

i didnt' add the eventloop as targetevent will always show emtpy
GreyHead 16 Feb, 2012
Hi eddddd,

The Joomla! Registration action needs to go into the On Submit event (not the On Load event).

We're waiting for a new release from Max to fix the Event Loop problem :-(

Bob
eddddd 16 Feb, 2012
yes i placed it in the Onsubmit event and no eror show when username exist.

the onload action :
show html

the onsubmit action :
joomla registration

do i need to add anything at joomla regisraton action the onfail field ?
eddddd 16 Feb, 2012
How about you show me where to put those ajax and javascript code in CFV4 ?
GreyHead 16 Feb, 2012
Hi eddddd,

Yes you need the Event Loop there. It may work OK even though you can't make a selection as you want the default setting.

Bob
GreyHead 17 Mar, 2012
Hi gemlog,

The MooTools JSON syntax changes after MooTools 1.12 so this code no longer works.
// if all is well send the JSON request
      var jSonRequest = new Json.Remote(url, {
        onComplete: function(r) {
          // check the result and set the background color
          if ( r.email_ok ) {
            email.setStyle('background-color', 'green');
          } else {
            email.setStyle('background-color', 'red');
          }
        }
      }).send({'email': email.value});

The new documentation is here and I think it will need to be something like this (not tested!):
// if all is well send the JSON request
      var jSonRequest = new Request.JSON({
        url: url,
        onSuccess: function(r) {
          // check the result and set the background color
          if ( r.email_ok ) {
            email.setStyle('background-color', 'green');
          } else {
            email.setStyle('background-color', 'red');
          }
        }
      }).get({'email': email.value});

There may also need to be some changes to the PHP used to receive and respond to the JSON request.

Bob
gemlog 17 Mar, 2012
Thanks Bob. I did have even a more different syntax in use than most others show. I guess you can still see my post although I deleted it yesterday morning (I can't see it).

The name of the method changed from JSON.Remote to Request.JSON. The url could be positional, but is now moved into the {} with an identifier. onComplete -> onSuccess and, finally .send changed to .post. .Send still works though (it posts). Here's a clean example I came across:
new Request.JSON({
   onFailure: showError,
   onSuccess: showArticle,
   url: 'index.php'
}).post({
   content: 'article',
   id: 1
});


Another thing I stumbled on in migrating the ajax to cf4 was the json itself. Very happy that menchee pointed out in another thread here that Request.JSON wasn't actually encoding at all anymore -- that would have driven me nuts. Mootools has another explicit method for that now. Here's menchee's post on that:
http://www.chronoengine.com/forums/index.php?option=com_chronoforums&cont=posts&f=2&t=40810#p194765
Basically you now need to JRequest::getString('name','','post') for each pair you send instead of decoding to an array as before. Request.JSON does decode though, so send the answer back just the same with json_encode($data) where $data is an array.

Finally, when I did get my syntax sorted out and started getting back replies decode was choking on a linkback to chronoengine, so I had to move my tests onto a validated domain.

There! That's got everything I had to relearn in one place. Hope it helps someone -- probably a future version of myself :-) Thanks yet again Bob!
This topic is locked and no more replies can be posted.