0

so iv been trying to make a search bar that uses ajax to GET the results to a php page that then runs an SQL query and displays the results, I'v been looking at it for a while now and from what iv managed to debug.. the ajax function works,I believe? and runs the corresponding page in the original. but my text input isn't loaded into the query.. so my question: what have I missed and how do you tell the php page what to do with the input sent through ajax. is there a simple way to turn it into a php variable for easy handling before being sent back?

<script>
//-----------------ajax code for search by text section----(booking.php)---------
function showHint(str) {
  var xhttp;
  if (str.length == 0) { 
    document.getElementById("membersContainer").innerHTML = "";
    return;
  }
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("membersContainer").innerHTML = xhttp.responseText;
    }
  };
  xhttp.open("GET", "booking/searchActivity.php?q="+str, true);
  xhttp.send();   
}
</script> 

--------------------HTML Code on booking.php-----------------------------

<form action="">
    <td> <input type="text" name="activity" id="activity" onkeyup="showHint(this.value)"</td>
    <td></td>                      
</form>

-------------------searchActivity.php------------------------------

<?php
include '../Global/DBConnect.php';


$activity=$_GET['activity'];

$query= "select * from scotiaExcursions where description like '%$activity%'";
$result=mysqli_query($db,$query);

echo "<div align='center'>";
echo "<table width='600' border='1'>";
echo "<tr>";
echo "<td>Excursion ID</td>";
echo "<td>Description</td>";
echo "<td>Price</td>";
echo "<td>Places Free</td>";
echo "<td>Order Now</td>";
echo "</tr>";

while ($row = mysqli_fetch_assoc($result))
    {
    echo "<tr>";
    echo "<td>".$row['excursionId']."</td>";
    echo "<td>".$row['description']."</td>";
    echo "<td>".$row['price']."</td>";
    echo "<td>".$row['availablePlaces']."</td>";
?>  
    <td>
        <form name="addCart" id="addCart" action="../cheakout.php" method="POST">
             <input type="hidden" name="activityID" id="activityID" value="<?php echo $row['excursionId']; ?>"/>
             <input type="hidden" name="price" id="price" value="<?php echo $row['price']; ?>"/>
             <input type="hidden" name="title" id="title" value="<?php echo $row['description']; ?>" />
             <select name="qty" id="qty">
                 <option value="1">1</option>
                 <option value="2">2</option>
                 <option value="3">3</option>
            </select>
            <input type="submit" name="submit" id="blueButton" value="Order"/>
         </form>
    </td>
    <?php
    echo "</tr>";

    }

echo "</table>";
echo "</div>";

?>
bodovix
  • 299
  • 2
  • 4
  • 11
  • That's the old way of doing ajax, why not consider using a framework: jquery or angular – Demodave Mar 17 '16 at 19:56
  • 1
    One thing I noticed is that you are looking with "q" from "searchActivity.php?q" but your page is looking for activity $_GET['activity'] – Demodave Mar 17 '16 at 19:59
  • ^ Hit The nail on the head there!! seems annoyingly obvious now!^ Thanks Demodave!!! – bodovix Mar 17 '16 at 20:11

0 Answers0