I have a question about drilldown combo boxes using php and javascript.
When I choose parent data, child data become selectable; and it is controlled in javascript.
I tried to keep the two values after post, but the following code remain only the parent data. How do I remain the two values in drilldown combo boxes after submission?
HTML&php
<select id="SEL1" name="SEL1">
<option value="">---</option>
<?php
foreach ($parent_list as $parent => $children_list) {
echo '<option value="'.$parent.'"';
if ($parent == $_GET['SEL1']){
echo 'selected';
}
echo '>'.$parent.'</option>';
}
?>
</select>
<select id="SEL2" name="SEL2">
<option value="">---</option>
<?php
foreach ($parent_list as $parent => $children_list) {
echo '<optgroup label="'.$parent.'">';
foreach ($children_list as $children => $grandson_list) {
$view_children = str_replace($parent,"",$children);
echo '<option value="'.$children.'"';
if ($children == $_GET['SEL2']){
echo 'selected';
}
echo '>'.$view_children.'</option>';
}
echo '</optgroup>';
}
?>
</select>
javascript
function ConnectedSelect(selIdList){
for(var i=0;selIdList[i];i++) {
var CS = new Object();
var obj = document.getElementById(selIdList[i]);
if(i){
CS.node=document.createElement('select');
var GR = obj.getElementsByTagName('optgroup');
while(GR[0]) {
CS.node.appendChild(GR[0].cloneNode(true));
obj.removeChild(GR[0]);
}
obj.disabled = true;
}
if(selIdList[i+1]) {
CS.nextSelect = document.getElementById(selIdList[i+1]);
obj.onchange = function(){ConnectedSelectEnabledSelect(this)};
} else {
CS.nextSelect = false;
}
obj.ConnectedSelect = CS;
}
}
function ConnectedSelectEnabledSelect(oSel){
var oVal = oSel.options[oSel.selectedIndex].value;
if(oVal) {
while(oSel.ConnectedSelect.nextSelect.options[1])oSel.ConnectedSelect.nextSelect.remove(1);
var eF = false;
for(var OG=oSel.ConnectedSelect.nextSelect.ConnectedSelect.node.firstChild;OG;OG=OG.nextSibling) {
if(OG.label == oVal) {
eF = true;
for(var OP=OG.firstChild;OP;OP=OP.nextSibling)
oSel.ConnectedSelect.nextSelect.appendChild(OP.cloneNode(true));
break;
}
}
oSel.ConnectedSelect.nextSelect.disabled = !eF;
} else {
oSel.ConnectedSelect.nextSelect.selectedIndex = 0;
oSel.ConnectedSelect.nextSelect.disabled = true;
}
if(oSel.ConnectedSelect.nextSelect.onchange)oSel.ConnectedSelect.nextSelect.onchange();
}
ConnectedSelect(['SEL1','SEL2']);
//-->
</script>
Related
I have three drop down list and few text input,the first drop down is using to hide some input there are no need to show and the second drop down is using to reload the page and make the third drop down take the info from database base on the option in second drop down.Now I face a problem with when I click the second drop down,the page reload,and the input hide in drop down 1 show again....
code to get value after reload:
<?php
#$utm=$_GET['utm']; // Use this line or below line if register_global is off
if(strlen($utm) > 0 and !is_numeric($utm))
{ // to check if $utm is numeric data or not.
echo "Data Error";
exit;
}
#$gpotp=$_GET['gpotp'];
if(strlen($gpotp) > 0 and !is_numeric($gpotp))
{
echo "Data Error";
exit;
}
?>
javascript to hide content(call by drop down 1):
<script>
function jsFunction(value)
{
var p = document.getElementById('ps');
var r = document.getElementById('rps');
var u = document.getElementById('upoint');
var m = document.getElementById('umng');
var t = document.getElementById('tmpass');
if ((value) == '1')
{
p.style.display = '';
r.style.display = '';
u.style.display = 'none';
m.style.display = 'none';
t.style.display = 'none';
}
else if ((value) == '2')
{
p.style.display = 'none';
r.style.display = 'none';
u.style.display = '';
m.style.display = 'none';
t.style.display = '';
}
if ((value) == '3')
{
p.style.display = 'none';
r.style.display = 'none';
u.style.display = '';
m.style.display = '';
t.style.display = '';
}
}
</script>
code to reload page(call when drop down 2):
function reload(form)
{
var val=form.utm.options[form.utm.options.selectedIndex].value;
var va2=form.gpotp.options[form.gpotp.options.selectedIndex].value;
self.location='CrtGroup.php?utm=' + val +'&gpotp=' + va2 ;
}
drop down 1:
<select name='gpotp' class='form-control' onmousedown=\"this.value='';\" onchange=\"jsFunction(this.value);\">
<option disabled selected value> -- select an option -- </option>";
if($stmt = $conn->query("$query3"))
{
while ($row2 = $stmt->fetch_assoc())
{
if($row2['Group_ID']==#$gpotp){echo "<option selected value='$row2[Group_ID]'>$row2[Group_Cat]</option>";}
else{echo "<option value='$row2[Group_ID]'>$row2[Group_Cat]</option>";}
}
}else
{
echo $conn->error;
}
echo"</select>
drop down 2:
echo"<select class='form-control' onchange=\"reload(this.form)\" name='utm' onmousedown=\"this.value='';\">";
echo"<option disabled selected value> -- select an option -- </option>";
if($stmt = $conn->query("$query2"))
{
while ($row2 = $stmt->fetch_assoc())
{
if($row2['Group_ID']==#$utm){echo "<option selected value='$row2[Group_ID]'>$row2[Tm_GroupID]</option>";}
else{echo "<option value='$row2[Group_ID]'>$row2[Tm_GroupID]</option>";}
}
}else
{
echo $conn->error;
}
echo"</select>";
drop down 3:
echo"<select class='form-control' name='umn' >";
echo"<option disabled selected value> -- select an option -- </option>";
if(isset($utm) and strlen($utm) > 0){
if($stmt = $conn->prepare("SELECT DISTINCT Mng_GroupID,Group_ID,Tm_GroupID FROM mnggroup where Tm_GroupID=? order by Mng_GroupID"))
{
$stmt->bind_param('i',$utm);
$stmt->execute();
$result = $stmt->get_result();
while ($row1 = $result->fetch_assoc()) {
echo "<option value='$row1[Group_ID]'>$row1[Mng_GroupID]</option>";
}
}else{
echo $conn->error;
}
/////////
}else{
///////
$query="SELECT DISTINCT Mng_GroupID,Group_ID,Tm_GroupID FROM mnggroup order by Mng_GroupID";
if($stmt = $conn->query("$query")){
while ($row1 = $stmt->fetch_assoc()) {
echo "<option value='$row1[Group_ID]'>$row1[Mng_GroupID]</option>";
}
}else{
echo $conn->error;
}
}
echo"</select>";
You could use window.name to store a flag that persists after the reload.
No links, excuse me, but you could use it instead of local-storage or cookies for simply implementing a logic to hide what you want after the page reloading. Es.:
if (window.name === "hide") {
element.style.display = "none";
}
Hi Guys I have this search from that takes a search term matches it with a table.field and in php it searches all matching data.
I want to add autocomplete to it, can anyone PLEASE assist?
Here is the HTML FORM
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
<input type="text" id="searchThis" name="searchThis" placeholder="search" value="" size="14" />
<select name="searchItems" id="searchItems">
<optgroup value="Vehicles" label="Vehicles">Vehicles
<option value="vehicles.Make">Make</option>
<option value="vehicles.model">Model</option>
<option value="vehicles.RegNumber">Registration Number</option>
<option value="vehicles.licenseExpireDate">License Expire Date</option>
</optgroup>
<optgroup value="Owners" label="Owners">Clients
<option value="owners.OwnerName" label="" >Name</option>
<option value="owners.mobile">Mobile Number</option>
</optgroup>
</select>
<input type="submit" id="doSearch" name="Search" value="Search" />
</form>
<ul id="result">
</ul>
There is the JS
<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
var $j = jQuery.noConflict();
(function($j){
$j(document).ready(function (){
$j("#searchThis").keyup(function()
{
var searchThis = $j('#searchThis').val();
var searchItems = $j('#searchItems').val();
var dataString = {'searchThis': searchThis,'searchItems':searchItems};
if(searchThis!='')
{
$j.ajax({
type: "POST",
url: "doAutocomplete_search.php",
data: dataString,
dataType: "html",
cache: false,
success: function(data)
{
$j("#result").html(data).show();
}
});
}return false;
});
$j("#result").live("click",function(e){
var clicked = $j(e.target);
var name = clicked.find('.name').html();
var decoded = $j("<div/>").html(name).text();
$j('#searchThis').val(decoded);
});
$j(document).live("click", function(e) {
var clicked = $j(e.target);
if (! clicked.hasClass("search")){
$j("#result").fadeOut();
}
});
$j('#searchid').click(function(){
$j("#result").fadeIn();
});
});
})($j);
And the PHP
function implement($cxn,$searchThis, $field) {
$show = "";
//Item to be searched
$srchThis = strip_tags(trim($searchThis));
//[0]= table , [1]=field to search
$srchStack = explode('.',$field);
$gtData = "SELECT * FROM ".$srchStack[0]." WHERE ".$srchStack[1]." like '%|{$srchThis}|%'";
//or die(mysqli_error($cxn))
if($selectc = mysqli_query($cxn,"SELECT * FROM {$srchStack[0]} WHERE {$srchStack[1]} LIKE '{$srchThis}%' OR {$srchStack[1]} LIKE '%{$srchThis}%'")) {
$srchData = array();
$rows = mysqli_fetch_row($selectc);
echo $rows;
//if() {, MYSQL_ASSOC
//echo var_dump($srchData);
$show .= '
<table style="border:2px solid #0000">';
//foreach($srchData as $fields=>$data) {
for($s=0; $s < $rows && $srchData = mysqli_fetch_assoc($selectc);$s++) {
if($srchStack[0] == 'vehicles' && $fields == 'RegNumber') {
$dataItem = $data;
$editTbl = 'Vehicles';
$link = 'href="index.php?list=vehicles&&tbl='.$srchStack[0].'&&item='.$dataItem.'"';
} elseif($srchStack[0] == 'vehicles' && $fields == 'Make') {
$dataItem = $data;
$editTbl = 'vehicles';
$link = 'href="index.php?list=vehicles&&tbl='.$srchStack[0].'&&item='.$dataItem.'"';
}
$show .= '<tr><td><a '.$link.'>'.$data.'</a></td></tr>
';
}
$show .= '</table>';
return $show;
} else {
$show .= "There are no entries in the database...<br>".mysqli_error($cxn);
return $show;
}
//}
}
echo implement($cxn, $_POST['searchThis'], $_POST['searchItems']);
$cxn->close();
Hi Guys so i had to do some refactoring, realized it was more the PHP MySQL code.
Here is the refactored PHP code
//Item to be searched
$srchThis = strip_tags(trim($searchThis));
//[0]= table , [1]=field to search
$srchStack = explode('.',$field);
$gtData = "SELECT * FROM ".$srchStack[0]." WHERE ".$srchStack[1]." like '%|{$srchThis}|%'";
//or die(mysqli_error($cxn))
if($selectc = mysqli_query($cxn,"SELECT * FROM {$srchStack[0]} WHERE {$srchStack[1]} LIKE '{$srchThis}%' OR {$srchStack[1]} LIKE '%{$srchThis}%'")) {
//$srchData = array();
$rows = mysqli_num_rows(#$selectc) or die(mysqli_error($cxn).'<br>No Rows returned...');
if($rows > 0) {//, MYSQL_ASSOC
//$link = ''; $l_c = 0;
$show .= '<table style="border:2px solid #0000">';
for($c = NULL;$c != $rows && $srchData = mysqli_fetch_assoc($selectc); $c++) {
foreach($srchData as $fields=>$data) {
if($fields == $srchStack[1]) {
$dataItem = $data;
$editTbl = $srchStack[0];
$show .= '<tr><td>'.$dataItem.'</td></tr>';//$a_json_row($dataItem);
//$show .= $link[$c];$link[$c]
}
}
}
$show .= '</table>';
return $show;
} else {
$show .= "There are no entries in the database...<br>".mysqli_error($cxn);
return $show;
}
Of-course the JS code still needs some more work but this greatly improved the results...
Hope this help someone...
Here's my view :
$(".qty").keyup(function(){
var qty = $(this).val();
var bt = (this.id);
var bts = $('#bts'+bt).val();
var edge = $('#edge'+bt).val();
for (var i = 1; i<edge; ++i) {
var batas = $('#bts'+i).val();console.log(batas);
}
});
<input type="hidden" id="<?php echo 'edge'.$items['id']?>" value="<?php echo $items['options']['jum']?>"/>
<?php
foreach ($items['options']['stok'] as $stok) {
$er = $stok['stokbagus'];
$length = $items['options']['jum'];
for ($i=1; $i< $length; $i++) {
echo '<input type="text" rel="'.$items['rowid'].'" id="bts'.$i.'" value="'.$er.'"/>';
}
}
?>
$items['options']['jum'] contains = 2.
$stok['stokbagus'] contains = 30 and 21.
It'll display the first one (30). How to display all $stok['stokbagus'] in javascript?
Because i want to compare $(".qty").val() with all of $stok['stokbagus']
Okay. Here is what you should do.
If your $stok['stokbagus'] variable is array, then you should select the first and the second variable like this:
$first = $stok['stokbagus'][0];
$second = $stok['stokbagus'][1];
Else if, your $stok['stokbagus'] variable is a string and has 30,21 like this;
$vars = explode(",", $stok['stokbagus']);
$first = $vars[0];
$second = $vars[0];
You are saying that $stok['stokbagus'] variable has 30 and 21 values then it must be an array.
To show all values in your $stok['stokbagus'];
implode(', ', $stok['stokbagus']; // or just use $er.
Full:
echo '<input type="text" rel="'.$items['rowid'].'" id="bts'.$i.'" value="'.implode(', ', $er).'"/>';
Update:
<?php
foreach ($items['options']['stok'] as $stok) {
$er = $stok['stokbagus'];
$length = $items['options']['jum'];
for ($i=1; $i < $length; $i++) {
if(is_array($er)) {
foreach($er as $key => $value) {
echo '<input type="text" rel="'.$items['rowid'].'" class="bts" data-id="'.$i.'" value="'.$value.'"/>';
}
} else {
echo '<input type="text" rel="'.$items['rowid'].'" id="bts'.$i.'" value="'.$er.'"/>';
}
}
}
Js:
$(".qty").keyup(function(){
var qty = $(this).val();
var bt = (this.id);
var bts = $('#bts'+bt).val();
var edge = $('#edge'+bt).val();
for (var i = 1; i<edge; ++i) {
// I don't know what do you want to do with batas variable...
if($('.bts').length > 1) {
$('.bts').each(function(){
console.log($(this).val());
});
} else {
var batas = $('#bts'+i).val();console.log(batas);
}
}
});
I have two selected menu the 1st one we chose the type so the next will filter the mysql database to show the depertments numbers, and i need to show the depertment price in a textfiled after i select the depertment number from the second selectedmenu.
1st selected menu
<select name="gender" id="gender" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>"> <?php echo $row['name']; ?>
<?php } ?>
</option>
<?php } ?>
</select>
2nd selected menu
<select name="category"
disabled="disabled" class="update" id="category" onChange="precio()" onClick="show()" >
<option value="">----</option>
</select>
this is how i get the value for the 2nd selected value
update.php
<?php
if (!empty($_GET['id']) && !empty($_GET['value'])) {
$id = $_GET['id'];
$value = $_GET['value'];
try {
$objDb = new PDO('mysql:host=localhost;dbname=name', 'root', '1234');
$objDb->exec('SET CHARACTER SET utf8');
$sql = "SELECT *
FROM `depertamientos`
WHERE `master` = ?";
$statement = $objDb->prepare($sql);
$statement->execute(array($value));
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
if (!empty($list)) {
$out = array('<option value="">Select one</option>');
foreach($list as $row) {
if ($row['visible'] == 0) {
$out[] = '<option value="'.$row['name'].'" id="'.$row['precio'].'">'.$row['name'].'</option>';
}
}
echo json_encode(array('error' => false, 'list' => implode('', $out)));
} else {
echo json_encode(array('error' => true));
}
} catch(PDOException $e) {
echo json_encode(array('error' => true));
}
} else {
echo json_encode(array('error' => true));
}
core.js
var formObject = {
run : function(obj) {
if (obj.val() === '') {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
} else {
var id = obj.attr('id');
var v = obj.val();
jQuery.getJSON('mod/update.php', { id : id, value : v}, function(data) {
if (!data.error) {
obj.next('.update').html(data.list).removeAttr('disabled');
} else {
obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
}
});
}
}
};
$(function() {
$('.update').live('change', function() {
formObject.run($(this));
});
});
js function
> <script src="javascripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function show() {
var select = document.getElementById('category');
var input = document.getElementById('ds');
var change = document.getElementById('dpto');
var deptprecio = document.getElementById('11');
select.onchange = function() {
input.value = select.value;
deptprecio.value = "I don't know what to do here ???? ";
change.value = select.value;
}
}
</script>
my data base :
id master name visible precio
-------------------------------------------------
1 0 Type a 0 0
2 0 type b 0 0
3 1 101 1 20000
4 1 201 1 10000
5 2 103 1 30000
why putting the price as the id of your options tag ? Why not putting it in the value propertie as in #WebDevRon example?
$out[] = '<option value="'.$row['name'].'">'.$row['name'].'</option>';
remove javascript event in your your HTML tag:
<select name="category"
disabled="disabled" class="update" id="category" >
<option value="">----</option>
</select>
and if i understand your request you could just replace your javascript function "show" by something like this:
$("#category").change(function () {
var price = $(this).val();
$('#price-input').val(price); // where "price-input" is the id of your input.
});
Edit:
use data-attibute to store the price:
$out[] = '<option value="'.$row['name'].'" data-price="'.$row['precio'].'">'.$row['name'].'</option>';
JS:
$(function() {
$('.update').live('change', function() {
formObject.run($(this));
});
$("#category").change(function () {
var dept_number = $(this).val();
var price = $(this).find(':selected').data('price');
$('#dept-input').val(dept_number);
$('#price-input').val(price);
});
});
FIDDLE DEMO
Here is your complete solution - Demo
var $select2 = $('#select2');
var $text = $('#price');
$("#select1").change(function () {
var id = $(this).val();
if ($select2.data('options') == undefined) {
$select2.data('options', $select2.find('option').clone());
}
var options = $select2.data('options').filter('[value=' + id + ']');
$select2.html(options);
$text.val(id);
});
I have created a registration page in admin section in OpenCart, in which we are asking details such as name, email, telephone, country, state/zone, password and many other things. Everything is working fine except country and zone form field. whenever I open the form I find one country always selected and nothing is displayed in zone field. This is first problem. When i choose another country then it loads state/zone, and if everything goes well, then the user gets registered. But if there is any error in form, then when it shows error on the page, state/zone value gets lost again. I have to reselect country and then zone is displayed. I have checked through "echo" that value of zone is transferred to this page but not shown selected in the drop down.this is my main problem.
this is the link to first image--
now this is the second image in which error messages are displayed---
Now someone please tell me what should i do?
I'm not an expert of opencart. i have just created this for the first time. I also don't have knowledge of jquery or javascript.
please tell me which part of code should I put here for getting solution.
thanks !!
this is customer.tpl template file
<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>
and this is customer.php controller file
<?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));
}
}
?>
I just made changes two changes in the template file --
1. HTML part
2. Ajax Script
HTML part is --
<tr>
<td><span class="required">*</span> <?php echo $entry_country; ?></td>
<td><select name="country_id">
<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">
<option value=""><?php echo $text_select; ?></option>
</select>
<?php if ($error_zone) { ?>
<span class="error"><?php echo $error_zone; ?></span>
<?php } ?></td>
</tr>
Ajax part is --
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=seller/customer/country&token=<?php echo $token; ?>&country_id=' + this.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) {
if (json['postcode_required'] == '1') {
$('#postcode-required').show();
} else {
$('#postcode-required').hide();
}
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>
In Your getForm() function find these lines:
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');
}
// few lines later
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'] = '';
}
and change them to
if (isset($this->request->post['business_id'])) {
$this->data['business_id'] = $this->request->post['business_id'];
} else {
$this->data['business_id'] = $this->config->get('business_id');
}
if (isset($this->request->post['country_id'])) {
$this->data['country_id'] = $this->request->post['country_id'];
} else {
$this->data['country_id'] = '';
}
if (isset($this->request->post['zone_id'])) {
$this->data['zone_id'] = $this->request->post['zone_id'];
} else {
$this->data['zone_id'] = '';
}
You are not working with session here but it may happen that the concrete values could be in session from some other form and this could mess things up.