[英]PHP: Calling a variable outside of function and echo on selected option
I am new to PHP and have created a project for my learning experience. 我是PHP的新手,为我的学习经验创建了一个项目。 I am hoping someone can assist me with the following script, please: 我希望有人可以通过以下脚本帮助我:
When a user selects or on the selection of a country (from an array) I need the dial code of that country to be echoed outside of the script as text before the mobile number input field. 当用户选择一个国家(或从一个数组中选择一个国家)时,我需要在手机号码输入字段之前以文本形式在脚本之外回显该国家的拨号代码。 Please find the code below: 请在下面找到代码:
<form action="" method="post" class="start_form">
<fieldset>
<legend><b>Are you a</b></legend>
<div" align="center">
<input type="radio" name="gender" value="male">Male
</div>
<div>
<input type="radio" name="gender" value="female">Female<br>
</div>
</fieldset>
<select id="country" name="country">
<option value="AD">AD - Andorra (+376)</option>
<option value="AE">AE - United Arab Emirates (+971)</option>
<option value="AF">AF - Afghanistan (+93)</option>
<option value="AG">AG - Antigua And Barbuda (+1268)</option>
<option value="AI">AI - Anguilla (+1264)</option>
</select>
<?php $country["code"]?>
<input type="text" name="mobilenumber" value="" autocomplete="off" placeholder="What is your Mobile Number?" mssg="" maxlength="12">
<input" type="submit" name="s_submit" value="Next"><br/>
<a href="">Skip</a>
</form>
This is the php code: 这是PHP代码:
<?php
$countryArray = array(
'AD'=>array('name'=>'ANDORRA','code'=>'376'),
'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'),
'AF'=>array('name'=>'AFGHANISTAN','code'=>'93'),
'AG'=>array('name'=>'ANTIGUA AND BARBUDA','code'=>'1268'),
'AI'=>array('name'=>'ANGUILLA','code'=>'1264'),
);
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes = ""){
global $countryArray; // Assuming the array is placed above this function
$output = "<select id='".$id."' name='".$name."' class='".$classes."'>";
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"])); // Making it look good
$output .= "<option value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
$output .= "</select>";
return $output; // or echo $output; to print directly
}
?>
This is a script I pulled from github here: https://gist.github.com/josephilipraja/8341837 这是我从github提取的脚本: https : //gist.github.com/josephilipraja/8341837
So, what I need is when the user selects a country from the options then that country's dial code is automatically echoed as text before the mobile number input field. 因此,我需要的是,当用户从选项中选择一个国家/地区时,该国家/地区的拨号代码会作为文本自动回显到手机号码输入字段之前。
Any help will be greatly appreciated. 任何帮助将不胜感激。
Thank you in advance 先感谢您
I will try to help you. 我会尽力帮助您。 I hate vanilla JS so I will show you how to do it in jQuery. 我讨厌香草JS,所以我将向您展示如何在jQuery中进行操作。
First off we need to add Jquery, so in the <head>
section of your page add the line from here. 首先,我们需要添加Jquery,因此,在页面的<head>
部分中,从此处添加行。
https://code.jquery.com/ https://code.jquery.com/
For the google CDN 对于Google CDN
<head>
<!-- other stuff like the <title> -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="crossorigin="anonymous"></script>
</head>
Then once that is installed we can add our script also in the head but below that line. 然后,一旦安装完毕,我们也可以将脚本添加到头部但在该行之下。
<head>
<!-- after including the jQuery library -->
<script type="text/javascript" >
(function($){
$(document).ready( function(){
$('#country').change( function(e){
//on change event
$('input[name="mobilenumber"]').val($(this).find('option:selected').data('co'));
});
}); //make sure all html is loaded before running
})(jQuery); //assign jQuery to the $
</script>
</head>
Then in your php file where you create the options. 然后在您的php文件中创建选项。 Add the custom data attribute. 添加自定义数据属性。
foreach($countryArray as $code => $country){
$countryName = ucwords(strtolower($country["name"])); // Making it look good
$output .= "<option data-co="'.$country["code"].'" value='".$code."' ".(($code==strtoupper($defaultCountry))?"selected":"").">".$code." - ".$countryName." (+".$country["code"].")</option>";
}
By setting the country code to a custom attribute we can avoid having to parse it out of the text of the input. 通过将国家/地区代码设置为自定义属性,我们可以避免不得不将其从输入文本中解析出来。
you can try it here 你可以在这里尝试
https://jsfiddle.net/a83u2m7j/ https://jsfiddle.net/a83u2m7j/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.