Web form generator PHP class - empty property on foreach
I am currently building PHP class that generates web form for flexibility and localization issues. I am having difficulty assigning a key and value for dropdown input; for some reason the foreach seems does not get array variable ($country_list). Here is my code that I am having difficulty.
require_once('_include/country_list.php');
//drop down form class
class DropDown
{
function __construct ($form, $field_label, $field_name, $field_desc, $dropdown_data, $locale){
$this->form = $form;
$this->field_label = $field_label;
$this->field_name = $field_name;
$this->filed_desc = $filed_desc;
$this->dropdown_data = $dropdown_data;
$this->locale = $locale;
}
function getNotRequiredData(){
global $notReqArry; //http://stackoverflow.com/questions/1415577/accessing-variables-and-methods-outside-of-class-definitions
return $notReqArry[$this->locale];
}
function getValue(){
return $_POST[$this->field_name];
}
function dropdown(){
$selecedVal = $this->getValue();
$select_start = "<select name=\"$this->field_name\"><option value=\"\">$this->filed_desc</option>";
foreach ($this->dropdown_data as $key=>$value){
$selected = ($key == $selecedVal ? 'selected' : '');
$options = sprintf('<option value="%s" %s >%s</option>',$key,$selected,$value);
print $options;
}
$select_end = "</select>";
return $select_start . $options . $select_end;
}
function getLabel(){
$non_req = $this->getNotRequiredData();
$req = in_array($this->field_name, $non_req) ? '' : '*';
return $this->field_label ? $req . $this->field_label : '';
}
function __toString(){
$label = $this->getLabel();
$field = $this->dropdown();
return $label.$field;
}
}
function generateForm ($lang,$country_list){
switch($lang)
{
case 'en-US':
//create EN web form
echo $textField = new TextField($form, 'Note', 'form_note', '2', '20', '250', 'en-US');
//echo $textField_js = new JsTextField($textField, 'onkeyup', 'return checklength(this,contact_max_warning)', 'Characters typed:');
echo $dropDown = new DropDown ($form, 'Country', 'form_country', '--Select Country--', $country_list, 'en-US');
break;
case 'fr-FR':
//create FR web form
break;
case 'de-DE':
//create DE web form
break;
case 'ja-JP':
//create JA web form
break;
default:
//create default web form
开发者_如何学Pythonprint('foooo');
};
}
<form id="frm_verification" action="<?=$_SERVER['SCRIPT_NAME']?>" method="POST">
<?
$lang='en-US';
echo generateForm ($lang,$country_list);
?>
<p>
<input type="button" name="reset" value="Reset">
<input type="submit" name="submit" value="Submit">
</p>
</form>
and the array from country_list.php is like:
$country_main = array (
//Main 5 countries at first
//Those 5 countries are direct market of my previous company. you can modify whatever you want.
"US" => "United States",
"UK" => "United Kingdom",
"DE" => "Germany",
"FR" => "France",
"JP" => "Japan",
);
It does not work currently and I have check the error log, and it says:
"PHP Fatal error: Cannot access empty property in..."
I am sure that this is from my misunderstanding of class and its var or foreach statement. Please help me to fix this; I really need help. (Yeah, I am really new on PHP)
foreach ($this->dropdown_data as $key->$value){
should read
foreach ($this->dropdown_data as $key=>$value){
Notice the change from ->
to =>
in $key=>$value
. Otherwise your code is trying to access property value
in object key
which is obviously invalid since key
is not an object.
The variable you're referencing for your list of countries is misnamed:
echo generateForm ($lang,$country_list);
should be
echo generateForm ($lang,$country_main);
as that's how it's defined in country_list.php.
精彩评论