0
Hello friends I’m studying Ajax I’m trying to make this code execution work, but I still can’t.
I’m trying to do after formprodutos
be selector is sent to consultarcor.php
via Ajax the information to query the colors of each product selected consultarcor.php
check and return the result tocores
as <option>
for selection in another form.
Someone can check what I did wrong because Ajax doesn’t work.
function showHint(str) {
if (str.length == 0) {
document.getElementById("produto").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
var vasr="cor=";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("produto").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST", "consultacor.php"+str, true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(vasr);
Formulario Html
<tr>
<td><input type="text" size="2" maxlength="2"name="quant" placeholder="Quant." onkeypress="return maskKeyPress(event)"/></td>
<td><input list='produtos' name='produto' onchange="showHint()"/>
<datalist id='produtos'><?php
$sql= mysqli_query($conn,"select DISTINCT descricao from produtos order by descricao");
while ($resp = mysqli_fetch_array($sql)) {
$group=$resp['descricao'];
echo "
"; }
?> </datalist></td>
<td>
<input list='cor' name='cor'/>
</td>
</tr>
Code inside.php color query
<?php
include "conecta.inc";
$q[]= $_POST['produto'];
$q = array_filter($q);
$query.= "select * from produtos where descricao='$q'";
$x = mysqli_query($conn,$query );
while($prt=mysqli_fetch_array($x)){
$cor[]=$prt['cores'];
echo "
<datalist id='cores'>
<option value='$cor'>
</datalist>";
}
?>
This is the error shown "console"
plug.php:110 Uncaught Typeerror: Cannot read Property 'length' of Undefined
Look in the browser console to see what error it is
– rray
does not display the error just does not execute the query.
– Danilo Araujo
mute
$query.= "select * from produtos where descricao='$q'";
for$query = "select * from produtos where descricao='$q'";
– rray
you came to see if there is any value in
$_POST
, of the oneprint_r()
in it.– rray