Jump to content

Auto detect Coutry + states drop-down menu


joe123

Recommended Posts

Script to Auto Detect Your website visitors Country and automatically select it and auto select the selected country's states/regions.

 

1. download the ip to country database From

http://ip-to-country.webhosting.info/node/view/6

 

2. Create a table in your whmcs database called "tbliptocountry"

with 5 Fields

 

ip_from

ip_to

country_code2

country_code3

country

 

3.import the database(ip-to-country.csv) into tbliptocountry.

 

I included a file called test.php ,upload it to your whmcs directory and call it in your browser if your country code is showing correct then the database is ready to be used the file that should be used with the script is iptoc.php.

 

Included are 2 versions of the script

 

1. the following files are to auto detect the visitors country and populates the states.

(included are 4 files , clientregister.tpl , countrystate.js , iptoc.php , test.php )

http://www.hosting777.com/countrystate1.zip

 

2.the following is just the countries and states without country detect.( countrystate.js , clientregister.tpl )

http://www.hosting777.com/countrystate2.zip

 

Look at the changes made in clientregister.tpl and change it accordingly in your order-signup.tpl file.

 

currently only the states for the following countries are included if you add more , don't forget to share.

 

United States

Canada

Australia

Austria

Brazil

Spain

Netherlands

United Kingdom

Ireland

Belgium

Germany

Denmark

France

 

Try the demo here ->> http://www.hosting777.com/clients/register.php

 

Enjoy it !

Link to comment
Share on other sites

Very nice joe123!

 

Can you add any type of error handling in case you are not able to get a proper ip to country match? Just for kicks, I tried your demo with proxify.com (remove all scripts unchecked) and both the state/region and country fields were blank. It works if you don't use an anonymous proxy though.

 

I think this is just a result of there not being an ip to country match for whichever IP address proxify.com was using at the time. Looks and works great though.

Link to comment
Share on other sites

Hey Joe,

 

For registration page there is no problems. Dropdown countries and states flieds run ok (img1), but if a customer place an order is different (img2). It doesn't appears dropdown fields.

I've made some changes in order-signup.tpl file taking clientregister.tpl file like model without any success.

I use "portal" templates and "boxes" orderforms.

May you explain me what I must to do?

 

One more thing;

I've no root access on my server.

I can create a new table called "tbliptocountry" with 5 fields, but I don't know the values to insert in varchar, primary key, null, etc.etc.

 

I'm waiting for your reply Joe.

 

Thank you

img1.jpg

img2.jpg

Link to comment
Share on other sites

I forgot :) , Put the following hidden Fields after the Opening <form> tag

 

<input type="hidden" value="countrySelect" name="clientcountry" id="clientcountry">
<input type="hidden" value="stateSelect"   name="clientstate"   id="clientstate" />
<input type="hidden" value="countrydefault" name="clientcountry_default" id="clientcountry_default" />
<input type="hidden" value="statedefault"   name="clientstate_default"   id="clientstate_default" />
<input type="hidden" value="US" name="countrydefault" id="countrydefault">
<input type="hidden" value=""   name="statedefault"   id="statedefault">

 

I will make a Video soon how to create the table and the fields exactly . will post it in about 30 Minutes .

Link to comment
Share on other sites

This is /portal/clientregister.tpl code I use

{if $noregistration}
Not Allowed
{else}

<p>{$LANG.clientregisterheadertext}</p>

{if $errormessage}<div class="errorbox">{$errormessage|replace:'<li>':'  #  '}  #  </div><br />{/if}

<form method="post" action="{$SERVER.PHP_SELF}">
<input type="hidden" name="register" value="true" />
<input type="hidden" value="countrySelect" name="clientcountry" id="clientcountry">
<input type="hidden" value="stateSelect"   name="clientstate"   id="clientstate" />
<!-- The id of the the fields holding the default values.  If more than one, seperate with spaces -->
<input type="hidden" value="countrydefault" name="clientcountry_default" id="clientcountry_default" />
<input type="hidden" value="statedefault"   name="clientstate_default"   id="clientstate_default" />
<!-- The actual default values -->
<input type="hidden" value="ES" name="countrydefault" id="countrydefault">
<input type="hidden" value=""   name="statedefault"   id="statedefault">

<table cellspacing="1" cellpadding="0" class="frame"><tr><td>
<table width="100%" cellpadding="2">
<tr><td width="150" class="fieldarea">{$LANG.clientareafirstname}</td><td><input type="text" name="firstname" size=30 value="{$clientfirstname}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientarealastname}</td><td><input type="text" name="lastname" size=30 value="{$clientlastname}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareacompanyname}</td><td><input type="text" name="companyname" size=30 value="{$clientcompanyname}"></td></tr>
<tr><td class="fieldarea">{$LANG.clientareaemail}</td><td><input type="text" name="email" size=50 value="{$clientemail}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareaaddress1}</td><td><input type="text" name="address1" size=40 value="{$clientaddress1}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareaaddress2}</td><td><input type="text" name="address2" size=30 value="{$clientaddress2}"></td></tr>
<tr><td class="fieldarea">{$LANG.clientareacity}</td><td><input type="text" name="city" size=30 value="{$clientcity}"> *</td></tr>
{* CHANGE START *}
<SCRIPT type="text/javascript" SRC="http://www.mydomain.com/clients/templates/portal/countrystate.js"></SCRIPT>
<tr><td class="fieldarea">{$LANG.clientareastate}</td><td><span><select id='stateSelect' name='state'></select></span> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareapostcode}</td><td><input type="text" name="postcode" size=10 value="{$clientpostcode}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareacountry}</td><td><div>
     <select id='countrySelect' name='country' onchange='updateState(this.id)'>
     </select>
*</div>
<SCRIPT  type="text/javascript">initCountry(); </SCRIPT> </td></tr>
{* CHANGE END  *}	
<tr><td class="fieldarea">{$LANG.clientareaphonenumber}</td><td><input type="text" name="phonenumber" size="20" value="{$clientphonenumber}"> *</td></tr>
{if $customfields}<tr><td class="fieldarea"><br /></td></tr>{/if}
{foreach key=num item=customfield from=$customfields}
<tr><td class="fieldarea">{$customfield.name}</td><td>{$customfield.input} {$customfield.required}</td></tr>
{/foreach}
</table>
</td></tr></table>

<p><strong> » {$LANG.orderlogininfo}</strong></p>
<p>{$LANG.orderlogininfopart1} {$companyname} {$LANG.orderlogininfopart2}</p>

<table cellspacing="1" cellpadding="0" class="frame"><tr><td>
<table width="100%" cellpadding="2">
<tr><td width="150" class="fieldarea">{$LANG.clientareapassword}</td><td><input type="password" name="password" size=30> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareaconfirmpassword}</td><td><input type="password" name="password2" size=30> *</td></tr>
</table>
</td></tr></table>

{if $capatacha}
<p><strong> » {$LANG.clientregisterverify}</strong></p>
<p>{$LANG.clientregisterverifydescription}</p>
<p align="center"><img src="includes/verifyimage.php" align="middle"> <input type="text" name="code" size="10" maxlength="5"> </p>
{/if}

{if $accepttos}
<p><input type="checkbox" name="accepttos" id="accepttos"> <label for=accepttos>{$LANG.ordertosagreement} <a href="{$tosurl}" target="_blank">{$LANG.ordertos}</a></label>.<p>
{/if}

<p align="center"><input type="submit" value="{$LANG.ordercontinuebutton}"></p>

</form>
{/if}

 

And this is for templates/orderforms/boxes/order-signup.tpl

 

<p>{$LANG.orderprogress}: {$LANG.orderchooseapackage} > {$LANG.orderdomainoptions} > {$LANG.orderconfigure} > {$LANG.orderconfirmorder} > <strong>{$LANG.ordercheckout}</strong></p>

{if $errormessage}<div class="errorbox">{$errormessage|replace:'<li>':'  #  '}  #  </div><br />{/if}

<form method="post" action="{$SERVER.PHP_SELF}">
<input type="hidden" name="register" value="true" />
<input type="hidden" value="countrySelect" name="clientcountry" id="clientcountry">
<input type="hidden" value="stateSelect"   name="clientstate"   id="clientstate" />
<!-- The id of the the fields holding the default values.  If more than one, seperate with spaces -->
<input type="hidden" value="countrydefault" name="clientcountry_default" id="clientcountry_default" />
<input type="hidden" value="statedefault"   name="clientstate_default"   id="clientstate_default" />
<!-- The actual default values -->
<input type="hidden" value="ES" name="countrydefault" id="countrydefault">
<input type="hidden" value=""   name="statedefault"   id="statedefault">

<table cellspacing="1" cellpadding="0" class="frame"><tr><td>
<table width="100%" cellpadding="2">
<tr><td width="150" class="fieldarea">{$LANG.clientareafirstname}</td><td><input type="text" name="firstname" size=30 value="{$clientfirstname}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientarealastname}</td><td><input type="text" name="lastname" size=30 value="{$clientlastname}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareacompanyname}</td><td><input type="text" name="companyname" size=30 value="{$clientcompanyname}"></td></tr>
<tr><td class="fieldarea">{$LANG.clientareaemail}</td><td><input type="text" name="email" size=50 value="{$clientemail}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareaaddress1}</td><td><input type="text" name="address1" size=40 value="{$clientaddress1}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareaaddress2}</td><td><input type="text" name="address2" size=30 value="{$clientaddress2}"></td></tr>
<tr><td class="fieldarea">{$LANG.clientareacity}</td><td><input type="text" name="city" size=30 value="{$clientcity}"> *</td></tr>
{* CHANGE START *}
<SCRIPT type="text/javascript" SRC="http://www.mydomain.com/clients/templates/portal/countrystate.js"></SCRIPT>
<tr><td class="fieldarea">{$LANG.clientareastate}</td><td><span><select id='stateSelect' name='state'></select></span> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareapostcode}</td><td><input type="text" name="postcode" size=10 value="{$clientpostcode}"> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareacountry}</td><td><div>
     <select id='countrySelect' name='country' onchange='updateState(this.id)'>
     </select>
*</div>
<SCRIPT  type="text/javascript">initCountry(); </SCRIPT> </td></tr>
{* CHANGE END  *}	
<tr><td class="fieldarea">{$LANG.clientareaphonenumber}</td><td><input type="text" name="phonenumber" size="20" value="{$clientphonenumber}"> *</td></tr>
{if $customfields}<tr><td class="fieldarea"><br /></td></tr>{/if}
{foreach key=num item=customfield from=$customfields}
<tr><td class="fieldarea">{$customfield.name}</td><td>{$customfield.input} {$customfield.required}</td></tr>
{/foreach}
</table>
</td></tr></table>

<p><b> » {$LANG.orderlogininfo}</b></p>
<p>{$LANG.orderlogininfopart1} {$companyname} {$LANG.orderlogininfopart2}</p>

<table cellspacing="1" cellpadding="0" class="frame"><tr><td>

<table width="100%" cellpadding="2">
<tr><td width="150" class="fieldarea">{$LANG.clientareapassword}</td><td><input type="password" name="password" size="30" /> *</td></tr>
<tr><td class="fieldarea">{$LANG.clientareaconfirmpassword}</td><td><input type="password" name="password2" size="30" /> *</td></tr>
</table>

</td></tr></table>

{if $accepttos}
<p><input type="checkbox" name="accepttos" id="accepttos" /> <label for="accepttos">{$LANG.ordertosagreement} <a href="{$tosurl}" target="_blank">{$LANG.ordertos}</a></label>.<p>
{/if}

<p align="center"><input type="submit" value="{$LANG.ordercontinuebutton}" /></p>

</form>

Link to comment
Share on other sites

From the image above i see you are using the cart so you have to edit viewcart.tpl I guess ( i dont use it)

and the other form is working fine http://www.sudomyhost.com/clients/register.php

 

and dont forget that sometimes you have to delete the files in templates_c to see the changes and refreshing the page as well.

 

 

Hey Joe,

Something I've made wrong on viewcart.tpl file code. It dosn't calculate tax an goes directly to create the account.

 

May you do it for me, please?

In the attach is the viewcart.tpl code

 

Thank you

viewcart.txt

Link to comment
Share on other sites

  • 6 months later...
i checked joe123's viewcart. its not working.

 

The states will not show. Only country is being selected.

 

even in this host, its not working http://www.sudomyhost.com/clients/cart.php?a=checkout

 

something si really wrong with viewcart.tpl. maybe joe123 can give it a check.

 

Sorry , but i have been kidnapped by aliens and they promised they will release me at the coming weekend , i will look at it then !

Link to comment
Share on other sites

  • 8 months later...
  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated