php - Populate the Phone Number field with the dialing code from the selected country -
hi i'd please.
i'm having contact form build on bootstrap , part of code i'd focus on
<div class="form-group"> <label for="country">* country in ?</label> <select name="country" class="form-control"> // list of countries here - comes database <option value="uk">united kingdom</option> <option value="gre">greece</option> <option value="ger">gemany</option> <option value="fra">france</option> <option value="ita">italy</option> </select> </div> <div class="form-group"> <label for="phone">* what’s phone number ?</label> <input type="text" name="phone" value="" class="form-control" /> </div>
what i'd when select country dropdown populate phone field dialing code of selected country. example if pick united kingdom
select menu phone field should have +44 code @ beginning , fill out phone, on server side should have this
$_post['phone'] => +44 123456
if understand , can add data-attributes
options
inside select
, this:
html
<div class="form-group"> <label for="country">* country in ?</label> <select name="country" class="form-control">// list of countries here - comes database <option data-code="+44" value="uk">united kingdom</option> <option data-code="+30" value="gre">greece</option> <option data-code="+49" value="ger">gemany</option> <option data-code="+33" value="fra">france</option> <option data-code="+39" value="ita">italy</option> </select> </div> <div class="form-group"> <label for="phone">* what’s phone number ?</label> <input type="text" name="phone" value="" class="form-control" /> </div>
javascript
$(document).ready(function () { $("[name='country']").on("change", function () { $("[name='phone']").val($(this).find("option:selected").data("code")); }); });
Comments
Post a Comment