DarthCaniac Posted December 16, 2011 Share Posted December 16, 2011 Hello, I run a small game hosting service, and during the checkout process, the user has to specify a username to use for the control panel. The only problem is popular usernames (like Rob, Justin, Chris, etc) are already registered, and when someone else tries to register using that username, the autosetup fails. We use the Custom Input field under the Configurable Options for the product as the username input. So my question is, I know there is a way using "Regular Expression Validation String" to only allow the field to contain certain characters, but is there a way that I can have it ping the username against say a PHP page that will return if the username is taken or not, or better yet, just have WHMCS check the existing client database for that username? Thanks so much! -Josh 0 Quote Link to comment Share on other sites More sharing options...
wilinsky Posted December 31, 2011 Share Posted December 31, 2011 Hi I'm looking for the same thing and havnt been able to find it anywhere! Almost at the point of getting some complicated jquery added to the template to keep checking it agianst the server whenever the field changes and keep the update button disabled unless the field is valid. If that sounds interesting let me know and we might be able to build something reusable. Thanks Steve 0 Quote Link to comment Share on other sites More sharing options...
laszlof Posted December 31, 2011 Share Posted December 31, 2011 You'd have to do it with jquery/ajax. Should be fairly simple to write a PHP page that connects to WHMCS, checks for the username, and returns a "0" or "1" for the results.. Then use an ajax call on form submit to first check the usernames to see if its valid, if it is, let the form submission go through, if not, update a div tag with something like "This username is not valid" in an errorbox. 0 Quote Link to comment Share on other sites More sharing options...
DarthCaniac Posted December 31, 2011 Author Share Posted December 31, 2011 Hey guys, thanks for the replies! In my site, I actually store all the username in a separate database outside WHMCS, so I can just do a quick php file that returns if the name exists or not. How would one go about implementing Ajax into the checkout field? 0 Quote Link to comment Share on other sites More sharing options...
laszlof Posted December 31, 2011 Share Posted December 31, 2011 Maybe something like this (completely untested). $(document).ready(function() { $('#resultsdiv').hide(); $('#formid').submit(function() { username = $('#username').val(); $.ajax({ type: "GET", url: "http://www.domain.tld/myscript.php", data: "username="+username, success: function(ret) { if (ret == "0") { $('#resultsdiv').hide(); return true; } else { $('#resultsdiv').show(); $('#resultsdiv').html("This username is already in use."); return false; } }); }); }); }); This would assume your form looks something like this. <form action="blah.php" method="POST" id="formid"> <input type="text" name="username" id="username" /><br /> <div class="errorbox" id="resultsdiv"></div> <input type="submit" value="Submit" /> </form> Hope this helps. 0 Quote Link to comment Share on other sites More sharing options...
wilinsky Posted January 2, 2012 Share Posted January 2, 2012 I got this working on my setup. at the top of the configureproduct.tpl put this {if $productinfo.groupname == "whatever group name your using"} {literal} <script type="text/javascript" /> function validate() { $('#usernameVal').attr("class",""); $.ajax({ url: "{/literal}{$systemurl}{literal}name_of_validation_php_script.php", async: false, cache: false, data: {"username":$('input[validate="true"]').val() }, dataType:'xml', type:'POST', success: function(data) { $ERROR = false; $(data).find("ERROR").each( function() { $ERROR = true; $('#usernameVal').html($(this).text()); $('#usernameVal').attr("class","errorbox"); } ); if ($ERROR == false) { $('#usernameVal').html("User name OK"); document.forms["mainForm"].submit(); } }, error : function(XMLHttpRequest, textStatus, errorThrown) { $('#usernameVal').html('error : ' + errorThrown); $('#usernameVal').attr("class","errorbox"); } }); } </script> {/literal} {/if} Then further down in the template find the loop that outputs the custom fields it will look something like this : {foreach key=num item=customfield from=$customfields} and change the output to this: <tr><td class="fieldlabel">{$customfield.name}:</td><td> {if $customfield.name == 'The lable of the field you want to validate' && $productinfo.groupname == "whatever group name your using"} <input id="customfield{$customfield.id}" type="text" size="30" value="{$customfields.value}" name="customfield[{$customfield.id}]" validate="true"> {else} {$customfield.input} {/if} {if $customfield.description}<br />{$customfield.description}{/if}</td></tr> you can put this code anywhere you want the error messgage to show up.. <div id='usernameVal' name='usernameVal' ></div> finally change the submit button code to this... {if $productinfo.groupname == "syncserver"} <p align="center">{if $firstconfig}<input type="button" onclick="validate();" value="{$LANG.addtocart}" />{else}<input type="button" onclick="validate();" value="{$LANG.updatecart}" />{/if}</p> {else} <p align="center">{if $firstconfig}<input type="submit" value="{$LANG.addtocart}" />{else}<input type="submit" value="{$LANG.updatecart}" />{/if}</p> {/if} The validation routine should return an XML string similar to this if it was invalid: <XML><ERROR>User name invalid</ERROR></XML> anything in the ERROR node will be displayed on screen for the user if it's ok just return any valid XML string with no ERROR nodes. If you have any porblems let me know. Thanks Steve 0 Quote Link to comment Share on other sites More sharing options...
wilinsky Posted January 2, 2012 Share Posted January 2, 2012 I had a word with support and you can do this through hooks as well. This will do pretty much the same as above... <?php function hook_UNIQUENAME_ShoppingCartValidateProductUpdate($vars) { global $errormessage; // ob_start(); // print_r($_SESSION['cart']); // $errormessage .= "<li>Error herei ".ob_get_contents() ; // ob_end_clean(); foreach ($_SESSION['cart']['products'] as $product) { // make sure were the correct product group $sql = "select p.id from tblproductgroups pg join tblproducts p on pg.id = p.gid and pg.name = 'PRODUCT GROUP NAME' and p.id = ".mysql_real_escape_string($product['pid']); $result = mysql_query($sql); if (mysql_error() != "" ) { $errormessage .= "<li>database error: ".mysql_error()."</li>"; return; } if ( $row = mysql_fetch_assoc($result) ) { // its in the right group so check if we have the field to validate foreach ( $product['customfields'] as $key => $value ) { $sql = "select * from tblcustomfields where relid = ".$row['id']." and type='product' and fieldname = 'FIELD NAME TO VALIDATE' and id = ".mysql_real_escape_string($key); $result = mysql_query($sql); if (mysql_error() != "" ) { $errormessage .= "<li>database error: ".mysql_error()."</li>"; return; } if ( $row2 = mysql_fetch_assoc($result) ) { // this is the correct field so run your validation // put validation code here! if (!$valid ) { $errormessage .= "<li>Duplicat user name : ".$value."</li>"; // bomb out on first error - if you want it to find all errors comment out the return return; } } } } } } add_hook("ShoppingCartValidateProductUpdate",1,"hook_UNIQUENAME_ShoppingCartValidateProductUpdate"); ?> stick this in /include/hooks/something.php or if your using a module stick it in the same directory as your module code and call it hook.php Hope this makes your life simpler! Steve 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.