broserdooder
broserdooder

Reputation: 19

how do i connect my php to mysql for an ajax query

I am trying to make this page sortable with a simple drop down. Right now there are only two different car makes in there and its not sorting those. I would like to be able to sort through make, model, and year, but need to figure this first part out first (right?).

Can somebody please help me out with this? Is there something wrong with my PHP code or my script or both?

<script>
function showCars(str)
{
if (str=="")
{
document.getElementById("showcars").innerHTML="";
return;
} 
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcars.php?q="+str,true);
 xmlhttp.send();
}
</script>

<form>
<select name="make" onchange="showCars(this.value)">
<option value="">Select a person:</option>
<option value="Ford">Ford</option>
<option value="Subaru">Subaru</option>
</select>
</form>
<br>
<div id="showcars">
<?php
$q = intval($_POST['q']);$con=mysqli_connect("mysql.database.com","user","password","table");
// Check connection
if (!$con)
 {
 die('Could not connect: ' . mysqli_error($con));
 }

 mysqli_select_db($con,"ford_man");
 $sql="SELECT * FROM make WHERE make = '".$q."'";

 $result = mysqli_query($con,$sql);

 while($row = mysqli_fetch_array($result))
 {

  echo '<span style="float: left; wdith="300px;">' . $row['pics']  . '</span>';
  echo '&nbsp; &nbsp; &nbsp; &nbsp;' . '<span style="font-size:28px;">' . $row['year'] .    '</span>';  echo "&nbsp;"; echo '<span style="font-size:28px;">' . $row['make'] . '</span>'; echo "&nbsp;"; echo '<span style="font-size:28px;">' . $row['model'] . '</span>';
  echo "<br>"; 
  echo '&nbsp; &nbsp; &nbsp; &nbsp;' . '<span style="font-size:32px;">' . $row['price'] . '</span>'; echo "&nbsp; &nbsp;"; echo '<span style="font-size: 26px;">' . $row['miles'] . '</span>'; echo "&nbsp;";  echo 'miles' ;
  echo "<br>";
  echo '<span style="width:800px; float:left;">' . $row['description'] . '</span>';
  echo "<br>";
  echo '<i> &nbsp; &nbsp;'; echo '<span style="font-size:12px;">' .  'Stock#' . '</span>';   echo '&nbsp;'; echo '<span style="font-size:12px;">' . $row['stock'] . '</span>'; echo'</i>';
  echo '<br>'; 
  echo '<br>';
  echo '<br>';
  echo '<a style="text-decoration: none; color: rgba(254,094,008,1.00);"   href="mailto:[email protected]">' . 'Email Me About This Car' . '</a>';
  echo "<hr>";
 }
  mysqli_close($con);
 ?>
 </div>

Upvotes: 0

Views: 67

Answers (1)

nstein
nstein

Reputation: 349

In your AJAX you're using GET whereas your php is looking in the POST superglobal

Upvotes: 1

Related Questions