Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Oi, os 2 combos funcionam perfeitamente, carregando os seus dados, lista a categoria depois a subcategoria.
Mas ocorre um errinho que não consegui reescrever, ele não passa o value da subcategoria.
Por isso ao cadastra no banco de dados ele não salva a subcategoria.
Me ajudem com isso?
Obrigada!
cadastro.php
<head>
<script language="javascript">
function list_dados(valor)
{
// Passando os dados para a página result.php através do método GET.
http.open("GET", "result.php?id=" + valor, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function handleHttpResponse()
{
campo_select = document.forms[0].subcategoria;
if (http.readyState == 4) {
campo_select.options.length = 0;
results = http.responseText.split(",");
for (var i = 0; i < results.length; i++) {
string = results[i].split("|");
campo_select.options[i] = new Option(string[0], string[1]);
}
}
}
// Essa função é somente para identificar o Navegador e suporte ao XMLHttpRequest.
function getHTTPObject()
{
var req;
try {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
if (req.readyState == null) {
req.readyState = 1;
req.addEventListener("load", function() {
req.readyState = 4;
if (typeof req.onReadyStateChange == "function") {
req.onReadyStateChange();
}
}, false);
}
return req;
}
if (window.ActiveXObject) {
var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
for (var i = 0; i < prefixes.length; i++) {
try {
req = new ActiveXObject(prefixes[i] + ".XmlHttp");
return req;
} catch (ex) {};
}
}
} catch (ex) {}
alert("XmlHttp Objects not supported by client browser");
}
var http = getHTTPObject();</script>
</head>
<body>
<form name="form" action="cadastrook.php" >
....campos.....
CATEGORIAS:
<select name="categoria" onChange="javascript:list_dados(this.value);">
<option>Selecione a categoria</option>
<?php
$consulta = mysql_query("SELECT * FROM cad_categoria ORDER BY categoria ASC");
while ($row = mysql_fetch_assoc($consulta)) {
echo "<option value=\"{$row['categoria']}\">{$row['categoria']}</option>\n";
}
?>
</select>
<select name="subcategoria">
<option>Selecione a subcategoria</option>
</select>
</form>
cadastrook.php
<?php
include "configsistema.php";
//Faz o controle de cache.
$gmtDate = gmdate("D, d M Y H:i:s");
header("Expires: {$gmtDate} GMT");
header("Last-Modified: {$gmtDate} GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Cache: no-cache");
?>
<body>
....posts + posts ....
$subcategoria = $_POST['subcategoria'];
result.php
<?php
include "configsistema.php";
//Faz o controle de cache.
$gmtDate = gmdate("D, d M Y H:i:s");
header("Expires: {$gmtDate} GMT");
header("Last-Modified: {$gmtDate} GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Cache: no-cache");
$categoria = addslashes(trim($_GET["id"]));
$consulta = mysql_query("SELECT * FROM cad_subcategoria WHERE categoria = '$categoria' ORDER BY subcategoria ASC" );
while( $row = mysql_fetch_assoc($consulta) ){
$linha .= $row["subcategoria"] . ",";
}
$linha = substr($linha,0,(strlen($linha)-1));
if ($linha == "") {
echo "Vazio!";
} else {
echo $linha;
};
?>Carregando comentários...