Contributions > Plugins, tips, and tricks

User input validation for pre-chat survey

(1/2) > >>

bobscan:
#######################################################
Fix survey blank/bad input                                                                                              #
#######################################################
There's pre-existing functionality of leavemessage.php's validation methods, which requires   input, and requiring a properly formed email address.

thankfully, we can easily integrate this into our pre-chat survey.


insert this @ line 80 of a distro-default client.php. (append it as part of the 'if survey is on' function, just before the following }else{ clause)


--- Code: ---$errors = NULL;
if( !$email ) {
$errors[] = no_field("form.field.email");
} else if( !$newname ) {
$errors[] = no_field("form.field.name");
} else if( !$firstmessage ) {
$errors[] = no_field("form.field.message");
} else {
if( !is_valid_email($email)) {
$errors[] = wrong_field("form.field.email");
}
}
--- End code ---

         
then insert at line 115, now 128 if you added the above statement. (following the 'IP connections exceeded' clause, and before $thread string definition)

--- Code: --- if( count($errors) > 0 ){
$page = array();
setup_logo();
setup_survey($visitor['name'], $email, $groupid, $info, $referrer);
expand("styles", getchatstyle(), "survey.tpl");
exit;
}
--- End code ---

dremhmrk2:
Just wanted to update this for the current version of Mibew (1.6.9)

The provided code does still work perfectly, BUT the line numbers are different, and I also added my own touch.

In the root Mibew folder, open client.php.

The following code now gets added right after line 86. (after the userchangename function.)

--- Code: ---$errors = NULL;
if( !$email ) {
$errors[] = no_field("form.field.email");
} else if( !$newname ) {
$errors[] = no_field("form.field.name");
} else if( !$firstmessage ) {
$errors[] = no_field("form.field.message");
} else {
if( !is_valid_email($email)) {
$errors[] = wrong_field("form.field.email");
}
}
--- End code ---

After adding that, this second block of code goes right after line 141. (after the check multiple IP function.)

--- Code: --- if( count($errors) > 0 ){
$page = array();
setup_logo();
setup_survey($visitor['name'], $email, $groupid, $info, $referrer);
expand("styles", getchatstyle(), "survey.tpl");
exit;
}
--- End code ---

Not so different so far, BUT what the original author didn't consider is that the form auto-fills, at least in my case, the word 'Guest' as a placeholder. The problem there is that 'Guest' passes the validation! D'oh! But no worries, there is a super simple solution. Bby the way, this applies specifically to the English localization, but probably also applies to other languages as well.

Open mibew/locales/en/properties (note the lack of an extension, make sure your editor doesn't add an extension when saving!)

On line 44, find:

--- Code: ---chat.default.username=Guest
--- End code ---

and change it to:

--- Code: ---chat.default.username=
--- End code ---

This keeps it from auto-filling the word 'guest', and forces the user to enter SOMETHING.

Otherwise the original poster had it all perfect for his version of Mibew, and I am just adapting it to my needs. I was getting driven crazy by people entering guest and talking to us as if we knew who they were, such as: "Did my order ship?" and we would have to always ask them to identify themselves.

ibrahim52:
I don't know why people don't bother to say THANK YOU for some one putting so much of efforts for others. Seriously man, thanks a lot for this solution you have posted. Just one doubt I have, on the chat window at NAME field it keep saying     Please fill "Your email". Is there any way to force just a normal name

ibrahim52:
 :o not a programmer but figured out.

Instead of

$errors = NULL;
         if( !$email ) {
            $errors[] = no_field("form.field.email");
         } else if( !$newname ) {
            $errors[] = no_field("form.field.name");
         } else if( !$firstmessage ) {
            $errors[] = no_field("form.field.message");
         } else {
            if( !is_valid_email($email)) {
               $errors[] = wrong_field("form.field.email");
            }
         }


Just use

      $errors = NULL;
          if( !$newname ) {
            $errors[] = no_field("form.field.name");

this is only if you do not want visitors to enter their email on first window of chat.

Dmitriy Simushev:
I want to insert my two cents.

It would be better to use

--- Code: ---$errors = array();
--- End code ---
instead of
--- Code: ---$errors = NULL;
--- End code ---
to avoid PHP notices.

Navigation

[0] Message Index

[#] Next page

Go to full version