-1

Im very inexperienced with php and myadmin and have been trying to utilise some tutorials to use ajax to query a database. I firstly want to loop through the database to give me a drop down list of options to choose from ie: Food Petrol Shopping Entertainment Then I want the user to be able to select one of the dropdown options and this then will query the database and produce a table with data on that selection ie if they choose petrol it will produce a table Payee Amount Date Tesco 23.00 27/10/13 Sainsbury 20.00 20/10/13 etc

Here is my code for the ajax

    <html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("output").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("output").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","catagory.php?catagory="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>


<?php

$server = 'localhost';
$user='root';
$pass='';
$db = 'finance_checker';

$mysqli = mysqli_connect($server, $user, $pass, $db);

$query = $mysqli->query("SELECT distinct `catagory` FROM `transactions`");

while($array[]= $query->fetch_object());

array_pop($array);

?>


        <h3>Transactions</h3>
        <select name="the_name" onchange="showUser(this.value)">
            <?php foreach ($array as $option): ?>
            <option value="<?php echo $option->Transaction; ?>"><?php echo $option -> catagory;?></option>
            <?php endforeach; ?>
        </select>
        <div id="ouput"<b>Transactions:</b></div>
        <?php
        $query-> close();
        ?>


</body>
</html> 

And here is my code to query the database:

    <?php
$q = $_GET['catagory'];

$con = mysqli_connect('localhost','root','','finance_checker');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,"finance_checker");
$sql="SELECT `ThirdParty`, `Credit`,`Date` FROM `transactions` WHERE `catagory` = '".$q."'";

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

echo "<table border='1'>
<tr>
<th>Payee</th>
<th>Amount</th>
<th>Date</th>

</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ThirdParty'] . "</td>";
  echo "<td>" . $row['Credit'] . "</td>";
  echo "<td>" . $row['Date'] . "</td>";

  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

The initial code is producing the correct dropdown options, however when I select one of the options I do not get anything reproduced. I thought I had understood what was going on but clearly somewhere I have missed something would anyone be able to offer further guidance?

Steve73NI
  • 17
  • 2
  • 9

2 Answers2

0

Since you only...

SELECT distinct `catagory` FROM `transactions`

...$option->Transaction would always be undefined.

geomagas
  • 3,230
  • 1
  • 17
  • 27
0

When you load data in select box please check the select query. Try below one

$query = $mysqli->query("SELECT distinct `catagory`,`Transaction` FROM `transactions`");
Paresh Gami
  • 4,777
  • 5
  • 23
  • 41