我在OpenCart的管理部分创建了一个注册页面,我们在其中询问详细信息,例如姓名、电子邮件、电话、国家/地区、州/地区、密码和许多其他信息。除国家和地区表单字段外,一切正常。每当我打开表格时,我发现总是选择一个国家,并且区域字段中没有显示任何内容。这是第一个问题。当我选择另一个国家时,它会加载state/zone,如果一切顺利,那么用户就会注册。但是如果表单有任何错误,那么当它在页面上显示错误时,状态/区域值会再次丢失。我必须重新选择国家,然后显示区域。我已经通过“回声”检查该区域的值已转移到此页面,但未显示在下拉列表中。这是我的主要问题。
这是第一张图片的链接——
现在这是显示错误消息的第二张图片——
现在有人请告诉我我该怎么办?我不是opencart的专家。我刚刚第一次创建了这个。我也没有 jquery 或 javascript 的知识。请告诉我应该将哪一部分代码放在这里以获得解决方案。
谢谢 !!
这是customer.tpl模板文件
<tr>
<td><span class="required">*</span> <?php echo $entry_country; ?></td>
<td><select name="country_id" onchange="country(this);">
<option value=""><?php echo $text_select; ?></option>
<?php foreach ($countries as $country) { ?>
<?php if ($country['country_id'] == $country_id) { ?>
<option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>
<?php } ?>
<?php } ?>
</select><?php if ($error_country) { ?>
<span class="error"><?php echo $error_country; ?></span>
<?php } ?></td>
</tr>
<tr>
<td><span class="required">*</span> <?php echo $entry_zone; ?></td>
<td><select name="zone_id">
</select><?php if ($error_zone) { ?>
<span class="error"><?php echo $error_zone; ?></span>
<?php } ?></td>
</tr>
<!-- ... -->
<script type="text/javascript">
function country(element) {
if (element.value != '') {
$.ajax({
url: 'index.php?route=seller/customer/country&token=<?php echo $token; ?>&country_id=' + element.value,
dataType: 'json',
beforeSend: function() {
$('select[name=\'country_id\']').after('<span class="wait"> <img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
},
complete: function() {
$('.wait').remove();
},
success: function(json) {
html = '<option value=""><?php echo $text_select; ?></option>';
if (json['zone'] != '') {
for (i = 0; i < json['zone'].length; i++) {
html += '<option value="' + json['zone'][i]['zone_id'] + '"';
if (json['zone'][i]['zone_id'] == '<?php echo $zone_id; ?>') {
html += ' selected="selected"';
}
html += '>' + json['zone'][i]['name'] + '</option>';
}
} else {
html += '<option value="0" selected="selected"><?php echo $text_none; ?></option>';
}
$('select[name=\'zone_id\']').html(html);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
}
$('select[name$=\'[country_id]\']').trigger('change');
</script>
这是customer.php控制器文件
<?php
class ControllerSellerCustomer extends Controller {
private $error = array();
public function index() {
$this->language->load('seller/customer');
$this->document->setTitle($this->language->get('heading_title'));
//$this->load->model('sale/customer');
$this->getForm();
}
public function insert() {
$this->language->load('seller/customer');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('seller/customer');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
$this->model_seller_customer->addCustomer($this->request->post);
$this->session->data['success'] = $this->language->get('text_success');
$this->redirect($this->url->link('seller/customer', 'token=' . $this->session->data['token'] . $url, 'SSL'));
}
$this->getForm();
}
protected function getForm() {
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['text_enabled'] = $this->language->get('text_enabled');
$this->data['text_disabled'] = $this->language->get('text_disabled');
$this->data['text_select'] = $this->language->get('text_select');
$this->data['text_none'] = $this->language->get('text_none');
$this->data['text_wait'] = $this->language->get('text_wait');
$this->data['text_no_results'] = $this->language->get('text_no_results');
$this->data['entry_description'] = $this->language->get('entry_description');
$this->data['entry_firstname'] = $this->language->get('entry_firstname');
$this->data['entry_email'] = $this->language->get('entry_email');
$this->data['entry_telephone'] = $this->language->get('entry_telephone');
$this->data['entry_business'] = $this->language->get('entry_business');
$this->data['entry_password'] = $this->language->get('entry_password');
$this->data['entry_confirm'] = $this->language->get('entry_confirm');
$this->data['entry_status'] = $this->language->get('entry_status');
$this->data['entry_company'] = $this->language->get('entry_company');
$this->data['entry_company_id'] = $this->language->get('entry_company_id');
$this->data['entry_address'] = $this->language->get('entry_address');
$this->data['entry_city'] = $this->language->get('entry_city');
$this->data['entry_zone'] = $this->language->get('entry_zone');
$this->data['entry_country'] = $this->language->get('entry_country');
$this->data['button_save'] = $this->language->get('button_save');
$this->data['button_cancel'] = $this->language->get('button_cancel');
$this->data['tab_general'] = $this->language->get('tab_general');
$this->data['token'] = $this->session->data['token'];
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
if (isset($this->session->data['success'])) {
$this->data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$this->data['success'] = '';
}
if (isset($this->error['firstname'])) {
$this->data['error_firstname'] = $this->error['firstname'];
} else {
$this->data['error_firstname'] = '';
}
if (isset($this->error['email'])) {
$this->data['error_email'] = $this->error['email'];
} else {
$this->data['error_email'] = '';
}
if (isset($this->error['telephone'])) {
$this->data['error_telephone'] = $this->error['telephone'];
} else {
$this->data['error_telephone'] = '';
}
if (isset($this->error['company'])) {
$this->data['error_company'] = $this->error['company'];
} else {
$this->data['error_company'] = '';
}
if (isset($this->error['business'])) {
$this->data['error_business'] = $this->error['business'];
} else {
$this->data['error_business'] = '';
}
if (isset($this->error['description'])) {
$this->data['error_description'] = $this->error['description'];
} else {
$this->data['error_description'] = '';
}
if (isset($this->error['password'])) {
$this->data['error_password'] = $this->error['password'];
} else {
$this->data['error_password'] = '';
}
if (isset($this->error['confirm'])) {
$this->data['error_confirm'] = $this->error['confirm'];
} else {
$this->data['error_confirm'] = '';
}
if (isset($this->error['address'])) {
$this->data['error_address'] = $this->error['address'];
} else {
$this->data['error_address'] = '';
}
if (isset($this->error['city'])) {
$this->data['error_city'] = $this->error['city'];
} else {
$this->data['error_city'] = '';
}
if (isset($this->error['country'])) {
$this->data['error_country'] = $this->error['country'];
} else {
$this->data['error_country'] = '';
}
if (isset($this->error['zone'])) {
$this->data['error_zone'] = $this->error['zone'];
} else {
$this->data['error_zone'] = '';
}
$url = '';
$this->data['breadcrumbs'] = array();
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
'separator' => false
);
$this->data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('seller/customer', 'token=' . $this->session->data['token'] . $url, 'SSL'),
'separator' => ' :: '
);
//SAVE and CANCEL Button
$this->data['action'] = $this->url->link('seller/customer/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
$this->data['cancel'] = $this->url->link('seller/customer', 'token=' . $this->session->data['token'] . $url, 'SSL');
//SAVE and CANCEL Button
//BUSINESS TYPE
//Calling the model for displaying business type
$this->load->model('seller/customer');
$this->data['businesses'] = $this->model_seller_customer->getBusiness();
//BUSINESS TYPE
//print_r($this->data['businesses']);die;
// For displaying values in form fields if an error occurs
if (isset($this->request->post['firstname'])) {
$this->data['firstname'] = $this->request->post['firstname'];
} else {
$this->data['firstname'] = '';
}
if (isset($this->request->post['email'])) {
$this->data['email'] = $this->request->post['email'];
} else {
$this->data['email'] = '';
}
if (isset($this->request->post['telephone'])) {
$this->data['telephone'] = $this->request->post['telephone'];
} else {
$this->data['telephone'] = '';
}
if (isset($this->request->post['company'])) {
$this->data['company'] = $this->request->post['company'];
} else {
$this->data['company'] = '';
}
if (isset($this->request->post['business_id'])) {
$this->data['business_id'] = $this->request->post['business_id'];
} elseif (isset($this->session->data['business_id'])) {
$this->data['business_id'] = $this->session->data['business_id'];
} else {
$this->data['business_id'] = $this->config->get('business_id');
}
if (isset($this->request->post['description'])) {
$this->data['description'] = $this->request->post['description'];
} else {
$this->data['description'] = '';
}
if (isset($this->request->post['address'])) {
$this->data['address'] = $this->request->post['address'];
} else {
$this->data['address'] = '';
}
if (isset($this->request->post['city'])) {
$this->data['city'] = $this->request->post['city'];
} else {
$this->data['city'] = '';
}
if (isset($this->request->post['country_id'])) {
$this->data['country_id'] = $this->request->post['country_id'];
} elseif (isset($this->session->data['country_id'])) {
$this->data['country_id'] = $this->session->data['country_id'];
} else {
$this->data['country_id'] = '';
}
if (isset($this->request->post['zone_id'])) {
$this->data['zone_id'] = $this->request->post['zone_id'];
} elseif (isset($this->session->data['zone_id'])) {
$this->data['zone_id'] = $this->session->data['zone_id'];
} else {
$this->data['zone_id'] = '';
}
if (isset($this->request->post['password'])) {
$this->data['password'] = $this->request->post['password'];
} else {
$this->data['password'] = '';
}
if (isset($this->request->post['confirm'])) {
$this->data['confirm'] = $this->request->post['confirm'];
} else {
$this->data['confirm'] = '';
}
if (isset($this->request->post['status'])) {
$this->data['status'] = $this->request->post['status'];
} else {
$this->data['status'] = 1;
}
$this->load->model('localisation/country');
$this->data['countries'] = $this->model_localisation_country->getCountries();
$this->template = 'seller/customer_form.tpl';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render());
}
protected function validateForm() { //echo "validating"; die;
if (!$this->user->hasPermission('modify', 'seller/customer')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32) || (!preg_match('/^[A-Za-z ]+$/', $this->request->post['firstname']))) {
$this->error['firstname'] = $this->language->get('error_firstname');
}
if ((utf8_strlen($this->request->post['description']) < 100) || (utf8_strlen($this->request->post['description']) > 1000)) {
$this->error['description'] = $this->language->get('error_description');
}
if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
$this->error['email'] = $this->language->get('error_email');
}
//For displaying error message if same email-id exist in database.
$rv = mysql_query("SELECT email FROM " . DB_PREFIX . "seller_details WHERE email = '" . $this->request->post['email']."'");
$row = mysql_fetch_array($rv);
if ( !empty($row['email']) )
{
$this->error['email'] = $this->language->get('error_email_exist');
}
//For displaying error message if same telephone number exist in database.
$rv = mysql_query("SELECT phone FROM " . DB_PREFIX . "seller_details WHERE phone = '" . $this->request->post['telephone']."'");
$row = mysql_fetch_array($rv);
if ( !empty($row['phone']) )
{
$this->error['telephone'] = $this->language->get('error_telephone_exist');
}
//For displaying error message if same store name exist in database.
$rv = mysql_query("SELECT store_name FROM " . DB_PREFIX . "seller_details WHERE store_name = '" . $this->request->post['company']."'");
$row = mysql_fetch_array($rv);
if ( !empty($row['store_name']) )
{
$this->error['company'] = $this->language->get('error_store_exist');
}
//$customer_info = $this->model_sale_customer->getCustomerByEmail($this->request->post['email']);
if ((utf8_strlen($this->request->post['telephone']) < 10) || !preg_match('/[0-9]+/', $this->request->post['telephone'])) {
$this->error['telephone'] = $this->language->get('error_telephone');
}
if ((utf8_strlen($this->request->post['company']) < 3) || (utf8_strlen($this->request->post['company']) > 32)) {
$this->error['company'] = $this->language->get('error_company');
}
//If no business_type is selected then error message is displayed.
if ($this->request->post['business_id'] == '') {
$this->error['business'] = $this->language->get('error_business');
}
if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
$this->error['zone'] = $this->language->get('error_zone');
}
if ($this->request->post['password'] || (!isset($this->request->get['customer_id']))) {
if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
$this->error['password'] = $this->language->get('error_password');
}
if ($this->request->post['password'] != $this->request->post['confirm']) {
$this->error['confirm'] = $this->language->get('error_confirm');
}
}
if ($this->error && !isset($this->error['warning'])) {
$this->error['warning'] = $this->language->get('error_warning');
}
if (!$this->error) {
return true;
} else {
return false;
}
}
public function country() {//echo "country"; die;
$json = array();
$this->load->model('localisation/country');
$country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);
if ($country_info) {
$this->load->model('localisation/zone');
$json = array(
'country_id' => $country_info['country_id'],
'name' => $country_info['name'],
'iso_code_2' => $country_info['iso_code_2'],
'iso_code_3' => $country_info['iso_code_3'],
'address_format' => $country_info['address_format'],
'zone' => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),
'status' => $country_info['status']
);
}
//print_r($json['zone'][0]['name']);die;
$this->response->setOutput(json_encode($json));
}
}
?>