HTML, JS - Creating Form with Subform

Asked

Viewed 172 times

1

I created this java script code to Select State and City with FORM SELECT.

What it has is that.. I would like to get the state data, through MYSQL, instead of writing them... How can I do this in JS?

dgCidadesEstados.prototype = {
estado: document.createElement('select'),
cidade: document.createElement('select'),
set: function(estado, cidade) { //define os elementos DOM a serem preenchidos
this.estado=estado;
this.estado.dgCidadesEstados=this;
this.cidade=cidade;
this.estado.onchange=function(){this.dgCidadesEstados.run()};
},
start: function () { //preenche os estados
var estado = this.estado;
while (estado.childNodes.length) estado.removeChild(estado.firstChild);
for (var i=0;i<this.estados.length;i++) this.addOption(estado, 
this.estados[i][0], this.estados[i][1]);
},
run: function () { //preenche as cidades de acordo com o estado escolhido
var sel = this.estado.selectedIndex; // estado escolhido
var itens = this.cidades[sel]; // pega as cidades correspondentes
var itens_total = itens.length;

var opts = this.cidade;
while (opts.childNodes.length) opts.removeChild(opts.firstChild); // limpa a lista atual

this.addOption(opts, '', '-- Secção --');
for (var i=0;i<itens_total;i++) this.addOption(opts, itens[i], itens[i]); // vai adicionando as cidades correspondentes
},
addOption: function (elm, val, text) {
var opt = document.createElement('option');
opt.appendChild(document.createTextNode(text));
opt.value = val;
elm.appendChild(opt);
},
estados : [
['','-- Categoria --'],
['Livros','Livros'],
['Informática','Informática'],      
['Consolas','Consolas'],
['Telemóveis','Telemóveis'],
['Televisões','Televisões'],
['Imóveis','Imóveis'],
['Empresas','Empresas'],
],
cidades : [
[''],
['Livros Terror', 'Livros Ação', 'Livros Escolares', 'Livros História', 'Livros Romance', 'Livros Ficção'],
['Cabos HDMI',
'Cabos Rede',
'Cabos Sata',
'Caixas Desktop',
'Caixas de Disco',
'Cooling',
'Discos Externos',
'Discos Rígidos',
'Drives',
'Fontes',
'Microphones',
'Monitores',
'Motherboards',
'PEN-USB',
'Placas Gráficas',
'Portáteis',
'Ratos',
'SSD',
'Tapetes',
'Teclados',
'Webcam',
'Desktop'],
['Playstation 1', 'Playstation 2', 'Playstation 3', 'Playstation 4', 'PSP', 'PSP Go', 'Nintendo', 'Nintendo DS Lite', 'Gameboy'],
['Acer', 'Alcatel', 'Apple', 'Asus', 'BlackBerry', 'Google', 'HTC', 'Huawei', 'Nokia', 'Samsung', 'Sony'],
['Ansonic', 'Apple', 'Benq', 'Denver', 'Hitachi', 'Philips', 'Samsung', 'Sanyo', 'Sony', 'Toshiba'],
['Apartamento T1', 'Apartamento T2', 'Apartamento T3', 'Casa de Férias', 'Casa 50-100m2', 'Casa 100-500m2', 'Casa 500-1000m2', 'Armazém 50-100m2', 'Armazém 100-500m2', 'Armazém 500-1000m2', 'Escritório 10-20m2', 'Escritório 20-40m2', 'Escritório 40-100m2', 'Quarto Single', 'Quarto Dobble', 'Garagem Single', 'Garagem Dobble', 'Garagem ++'],
['Procuro Emprego', 'Oferta Emprego', 'Serviços', 'Mudanças'],
]
};

On this line of the code

estados : [
['','-- Categoria --'],
['Livros','Livros'],
['Informática','Informática'],      
['Consolas','Consolas'],
['Telemóveis','Telemóveis'],
['Televisões','Televisões'],
['Imóveis','Imóveis'],
['Empresas','Empresas'],
],

I would like to get via MYSQL and not written, how can I do it? I know I need a FETCH, and I created it in a separate file, now like... : S

<?php
include 'db.php';

$con = new mysqli($dbServername, $dbUsername, $dbPassword, $dbName);

if ($con->connect_error) {
  die("Connection failed: " . $con->connect_error);
}

$select_parent = "SELECT parent_title, parent FROM public_parents";
$parent_query = $con->query($select_parent);

echo "<select name='genre' class='form-control' required>";

if($parent_query->num_rows > 0) {

 while($parents = $parent_query->fetch_assoc()) {

    echo '<optgroup label="'.$parents['parent_title'].'">';

    $select_categories = "SELECT category_id, category_title FROM public_categories WHERE parent = '". $parents['parent'] ."' ORDER BY category_title";
    $category_query = $con->query($select_categories);

    while($options = $category_query->fetch_assoc()) {

         echo "<option value='". $options['category_id'] ."'>". $options['category_title'] ."</option>";
    }                                
   echo '</optgroup>';
   }                                
   }

  echo "</select>";

  $con->close();

  ?>
  • Each thing is in a separate file?

  • Yes this in separate file.

  • I intended an answer is not...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.