Forums

check if the code is already stored into the db

surferbloggy 20 Jul, 2012
Hi, i've made a joomla registration form with chronoforms it stores data into a db table

now i'd want to check if the code inputfield value is already stored into the db

if there is it shoud stop the registration and give a message error

"the code is already stored into our database"

if there isn't it should go on with the registration and store data into the db

i don't know how i can do this with chronoforms

could you help me?? thank you
GreyHead 21 Jul, 2012
Hi surferbloggy,

You can add a Custom Serverside Validation action to check the database after the form is submitted.

If you want to be more user-friendly you can also add a check on the page using Ajax, there's a tutorial that you can buy here that included the code to check for an email like this that can be adopted to check for other values.

Bob
surferbloggy 21 Jul, 2012
Thank you i've bought the script but i didn't understood where to put the javascript code that add the blur event to the email field in chronoform
(i've added the id as email in the wizard)



the php goes into the custom code for a new event but i dont understand about the javascript
it's told the form javascript i don't know the right place where to put it

thank you for your help
surferbloggy 23 Jul, 2012
Thank you i've done, now the email field become always red but i can register a new user even if i 've added a duplicate email shouldn't it avoid to register a new user if the email is stored in the db?? or I ve done any mistakes?? could you help me thank you
surferbloggy 24 Jul, 2012
i've added the right name of the form in javascript event and the db table in the custom code on ajax event but the email field become always red even if I insert an email that isn't stored in the db what's wrong??? please help me! thank you
GreyHead 24 Jul, 2012
Hi surferbloggy,

I have no idea why you are only getting a 'red' result there isn't any information here to help diagnose that bug.

I would probably change this part of the code to disable the submit button if there was an error - but it really does depend on how you want your form to behave.
if ( r == 'ok' ) {
  email.setStyle('background-color', 'green');
  $('submit_btn').disabled = false;
} else {
  email.setStyle('background-color', 'red');
  $('submit_btn').disabled = true;
}

Bob
surferbloggy 24 Jul, 2012
Thank i've added the code the problem is the query isn't never "ok" it isn't never $count == 0
i think the problem could be in this line:

var url ='index.php?option=com_chronoforms&chronoform=Iscrizione&event=ajax&format=raw';

the form is called Iscrizione and the event is On ajax so it seems right
or the error is in the query

FROM dtyi5_chronoforms_data_iscrizione
WHERE email = ".$db->quote($email).";
";

but the table is this
dtyi5_chronoforms_data_iscrizione
it seems right too isn't it???

the textfield become red even if i set
if ( $count != 0 ) {
$response = 'ok';

i don't understand what's wrong
thank you for your help
GreyHead 25 Jul, 2012
Hi surferbloggy,

If you use your browser web developer tools you can see what data is being sent to the Ajax event and being returned. That will give you clues about what is happening and where the problem is.

Your code snippets look OK provided that $email has a value.

Bob
surferbloggy 25 Jul, 2012
thank you, the problem is in the javascript not in the query becouse even if it is
echo "ok";
the email textfield become always red and it isn't the url wrong i've tested the problem is in the javascript but it isn't jquery and i know only jquery i've substituted the js code with this jquery code


$(document).ready(function() {
  $('#email').blur(function() {
$('#email').css("background-color", "green");
});
});


but it doesn't work i've also added jQuery.noConflict(); and i've added a custom code
<?php $document = &JFactory::getDocument();
$document->addScript( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' ); ?>

but nothing
how could i add jquery code to chronoforms???
i want to try solve it with jquery
thank you for your help
GreyHead 26 Jul, 2012
Hi surferbloggy,

Of course you can use JQuery if you are familiar with it. But I don't recommend using jQuery in Joomla! and I don't support it; it's not good practice to load two JavaScript libraries when the MooTools library is more than good enough to do the job.

Bob
surferbloggy 26 Jul, 2012
the problem is the query becouse with jquery the email field become always red too
but when i set as output the query the output is nothing
$query is empty i don't know why

$query = "
.....
FROM '#__users'
WHERE LOWER('email') = ".$db->quote($email).";
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count == 0 ) {
$response = 'ok';
}
}
//send the reply
echo $query;


even with the right table


$query = "
.....
FROM dtyi5_chronoforms_data_iscrizione
WHERE LOWER('email') = ".$db->quote($email).";
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count == 0 ) {
$response = 'ok';
}
}
//send the reply
echo $query;


what's wrong??? i'm going crezy!! please help me i need the check in the db working
thank you for your help
surferbloggy 26 Jul, 2012
why this code doesn't work???i've bought the script i've cut and pasted i've remaked it i'm going crazy!!!

<?php
// get the query info
$email = JRequest::getString('email', '', 'post');
$email = strtolower(trim($email));
// check that the email field isn't empty
$response = 'in use';
if ( $email ) {
// Check the database
$db =& JFactory::getDBO();
$query = "
SELECT COUNT(*)
FROM '#__chronoforms_data_iscrizionefree'
WHERE LOWER('email') = ".$db->quote($email).";
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count == 0 ) {
$response = 'ok';
}
}
//send the reply
echo $response;
?>


i verified it dosn't read this
$email = JRequest::getString('email', '', 'post');


becouse if i've set it as output it is empty

but even if i set $email with an email stored or not stored it always give me the same response is there anotheer way to retrieve the email value from form?? and why the query dosn't work??
thank you for your help
GreyHead 04 Aug, 2012
Hi surferbloggy,

The section in the tutorial titled The Form JavaScript gets the value of the 'email' input from the form and passes it on to the Event code. It looks as though you may have something wrong there.

Bob

window.addEvent('domready', function() {
  // set the url to send the request to
  var url =
  'index.php?option=com_chronoforms&chronoform=my_form_name&event=ajax&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 request = new Request({ 
        method: 'post',
        url: url,
        data: { 'email' : value },
        onComplete: function(r) {
          // check the result and set the background color
          if ( r == 'ok' ) {
            email.setStyle('background-color', 'green');
          } else {
            email.setStyle('background-color', 'red');
          }
        }
      }).send();
    } else {
      // if this isn't a valid email set background color red
      email.setStyle('background-color', 'red');
    }
  });
});

Bob
Riceman 08 Aug, 2012
Hi Greyhead,

Got a question. I bought your "ChronoForms v4 using Ajax.pdf" and am trying to implement this on a form. I am not using it to check email but rather to check another field "unique_id". All instances of "email" in the code provided have been changed to "unique_id" and I have also removed this line from the Load JS code: (it was to check email structure)
// check that the email address is valid
             regex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;


One other quick related question, I am under the impression that the HTML in the pdf is auto generated by Chonoforms. I did add "unique_id" for field name and field id of course. But I want to make sure that I do not need to add/change any code in this section.

The Problem:

My form is always displaying red and never green, even though the value is not in the DB.

Assuming the rest of the code is OK, I am wondering if it is this line causing the issue. I am trying to accomplish this with SEF on.

// set the url to send the request to
           var url =
             'clients/create-a-mobile-property-website/dealer-packages&event=ajax&format=raw';
GreyHead 11 Aug, 2012
Hi Riceman,

Don't use the SEF URL in the Ajax call, that probably won't work; use the non-SEF Form URL instead, you can get it from the FrontEnd View link in the Forms Manager.

Bob
Riceman 21 Aug, 2012
Thanks Greyhead,

But I am still having trouble getting this to work.
Relevant field is Unique ID in the second section of the form.

Here is the non SEF url:
http://autodealermobileservices.com/index.php?option=com_chronoforms&chronoform=VehicleSubmissionMobile

Relevant code in the Load JS Event
var url =
             'index.php?option=com_chronoforms&chronoform=VehicleSubmissionMobile&event=ajax&format=raw';

It seems whatever data I put in the field it always comes up red. There is only one row in the DB right now, and the field value of "unique_id" in that row is "820031", so that is the only value that should give us a return of red.

Could my trouble be in the Check DB lines (in the on Ajax Event)...I don't see where it specifies which DB and Row:
 // get the query info
     $unique_id = JRequest::getString('unique_id', '', 'post');
     $unique_id = strtolower(trim($unique_id));
     // check that the unique_id field isn't empty
     $response = 'in use';
     if ( $unique_id ) {
            // Check the database
            $db =& JFactory::getDBO();
            $query = "
            SELECT COUNT(*)
  FROM `#__users`
  WHERE LOWER(`unique_id`) = ".$db->quote($unique_id).";
  ";
  $db->setQuery($query);
  $count = $db->loadResult();
  if ( $count == 0 ) {
    $response = 'ok';
  }
}
//send the reply
echo $response;
// stop any further processing
$mainframe->close();
?>
GreyHead 22 Aug, 2012
Hi Riceman,

Please turn off the Dynamic load at least until you have finished debugging.

You have left in the regex.test() but removed the definition of regex so I suspect that the AJAX call never submits.

Bob
Riceman 23 Aug, 2012
I believe I am getting closer, muddling my way through an unfamiliar scripting language....but no luck yet

original code that checked for valid email:
 window.addEvent('domready', function() {
           // set the url to send the request to
           var url =
             'index.php?option=com_chronoforms&chronoform=VehicleSubmissionMobile&event=ajax&format=raw';
           var unique_id = $('unique_id');
           unique_id.addEvent('blur', function() {
             // clear any background color from the input
             unique_id.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 request = new Request({
              method: 'post',
             url: url,
             data: { 'unique_id' : value },
             onComplete: function(r) {
               // check the result and set the background color
               if ( r == 'ok' ) {
                 unique_id.setStyle('background-color', 'green');
               } else {
                 unique_id.setStyle('background-color', 'red');
               }
} }).send();

         } else {
           // if this isn't a valid unique_id set background color red
           unique_id.setStyle('background-color', 'red');
         }
}); });


current code:
 window.addEvent('domready', function() {
           // set the url to send the request to
           var url =
             'index.php?option=com_chronoforms&chronoform=VehicleSubmissionMobile&event=ajax&format=raw';
           var unique_id = $('unique_id');
           unique_id.addEvent('blur', function() {
             // clear any background color from the input
             unique_id.setStyle('background-color', 'white');
             
               // if all is well send the JSON request
               var request = new Request({
              method: 'post',
             url: url,
             data: { 'unique_id' : value },
             onComplete: function(r) {
               // check the result and set the background color
               if ( r == 'ok' ) {
                 unique_id.setStyle('background-color', 'green');
               } else {
                 unique_id.setStyle('background-color', 'red');
               }
} }).send();


}); });
GreyHead 24 Aug, 2012
Hi Riceman,

As far as I can see 'value' isn't defined anywhere; try this version:
window.addEvent('domready', function () {
    // set the url to send the request to
    var url = 'index.php?option=com_chronoforms&chronoform=VehicleSubmissionMobile&event=ajax&format=raw';
    var unique_id = $('unique_id');
    unique_id.addEvent('blur', function () {
        // clear any background color from the input
        unique_id.setStyle('background-color', 'white');

        // if all is well send the JSON request
        var request = new Request({
            method: 'post',
            url: url,
            data: {
                'unique_id': unique_id.value
            },
            onComplete: function (r) {
                // check the result and set the background color
                if (r == 'ok') {
                    unique_id.setStyle('background-color', 'green');
                } else {
                    unique_id.setStyle('background-color', 'red');
                }
            }
        }).send();
    });
});

Bob
This topic is locked and no more replies can be posted.