so I am having an issue when creating a search function which prints results from a selected year. The user basically selects the year in which they would like to see all the tasks that took place in that year.
I have tried many functions and I am still learning myslqi so I do apologize for lack of this *currently on in mysql.
Here's what I have so far, I am getting no errors, I am not getting any errors, I am just not getting any results.
Here's the html where the user selects the year they want results from;
I have cut the code down to show the important parts;
First off here is the html year selection:
<table>
<div class="container-fluid">
<form>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" onclick="showYear(this.value)" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2015">
<button type="button" class="btn btn-default" value="2015">2015</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2014">
<button type="button" class="btn btn-default" value="2014">2014</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2013">
<button type="button" class="btn btn-default" value="2013">2013</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2012">
<button type="button" class="btn btn-default" value="2012">2012</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" onclick="showYear(this.value)" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2011">
<button type="button" class="btn btn-default" value="2011">2011</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2010">
<button type="button" class="btn btn-default" value="2010">2010</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2009">
<button type="button" class="btn btn-default" value="2009">2009</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2008">
<button type="button" class="btn btn-default" value="2008">2008</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" onclick="showYear(this.value)" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2007">
<button type="button" class="btn btn-default" value="2007">2007</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2006">
<button type="button" class="btn btn-default" value="2006">2006</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2005">
<button type="button" class="btn btn-default" value="2005">2005</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2004">
<button type="button" class="btn btn-default" value="2004">2004</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year"onclick="showYear(this.value)" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2003">
<button type="button" class="btn btn-default" value="2003">2003</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2002">
<button type="button" class="btn btn-default" value="2002">2002</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2001">
<button type="button" class="btn btn-default" value="2001">2001</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2000">
<button type="button" class="btn btn-default" value="2000">2000</button>
</div>
</div>
</form>
</table>
<div class="container-fluid" id="txtHint"><b></b></div>
There is a script at the top of this page to handle and pass this:
<script>
function showYear(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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","getYear.php?year="+str,true);
xmlhttp.send();
}
}
</script>
and here's the php:
<html>
<body>
<div class="container-fluid"> <!-- Table Headers -->
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Series:</th><th>Title:</th><th>Date:</th><th>Type:</th><th>House Location:</th><th>Housemates:</th>
<br>
</tr>
</thead>
</div>
<?php
$date = intval($_GET['year']);
include('dbconnect.php');
$sql ="SELECT * FROM tasks WHERE YEAR(date) = '$date' ";
$result = mysql_query($sql);
if (!$result)
echo "An error occurred: ".mysql_error();
else
while ($row=mysql_fetch_assoc($result))
{
$taskid = $row['taskid'];
echo '<tr>';
echo '<td>';
echo '<button type="button" name="delete_row" id="delete_row" class="close">';
echo '<a href="taskResult.php?id='. $taskid .'">';
echo '<span title="View" aria-hidden="true" class="glyphicon glyphicon-search">';
echo '</span>';
echo '<span class="sr-only">';
echo 'View';
echo '</span>';
echo '</button>';
echo '</a>';
echo '</td>';
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['houseLocation'] . "</td>";
echo "<td>" . $row['housemates'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
At the moment I am only getting the table headers printing on any selection?
Any help would be much appreciated. This is for very small project, I am a very new coder.
In you JS:
xmlhttp
is undefined. try adding var
before xmlhttp
like:
var xmlhttp = new XMLHttpRequest();
and
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
OR
add var xmlhttp
in the first line of your else
block.
And your onclick
method is added to wrong div in your html where as it should be added to the buttons:
Relace you HTML with this:
<table>
<div class="container-fluid">
<form>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2015">
<button type="button" class="btn btn-default" value="2015" onclick="showYear(this.value)">2015</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2014">
<button type="button" class="btn btn-default" value="2014" onclick="showYear(this.value)">2014</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2013">
<button type="button" class="btn btn-default" value="2013" onclick="showYear(this.value)">2013</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2012">
<button type="button" class="btn btn-default" value="2012" onclick="showYear(this.value)">2012</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2011">
<button type="button" class="btn btn-default" value="2011" onclick="showYear(this.value)">2011</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2010">
<button type="button" class="btn btn-default" value="2010" onclick="showYear(this.value)">2010</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2009">
<button type="button" class="btn btn-default" value="2009" onclick="showYear(this.value)">2009</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2008">
<button type="button" class="btn btn-default" value="2008" onclick="showYear(this.value)">2008</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2007">
<button type="button" class="btn btn-default" value="2007" onclick="showYear(this.value)">2007</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2006">
<button type="button" class="btn btn-default" value="2006" onclick="showYear(this.value)">2006</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2005">
<button type="button" class="btn btn-default" value="2005" onclick="showYear(this.value)">2005</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2004">
<button type="button" class="btn btn-default" value="2004" onclick="showYear(this.value)">2004</button>
</div>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." name="year" id="year" class="form-control">
<div class="btn-group btn-group-lg" role="group" value="2003">
<button type="button" class="btn btn-default" value="2003" onclick="showYear(this.value)">2003</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2002">
<button type="button" class="btn btn-default" value="2002" onclick="showYear(this.value)">2002</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2001" >
<button type="button" class="btn btn-default" value="2001" onclick="showYear(this.value)">2001</button>
</div>
<div class="btn-group btn-group-lg" role="group" value="2000">
<button type="button" class="btn btn-default" value="2000" onclick="showYear(this.value)">2000</button>
</div>
</div>
</form>
</table>
<div class="container-fluid" id="txtHint"><b></b></div>
First thing that jumps out at me is that in your sql you are comparing a number to a string.
$sql ="SELECT * FROM tasks WHERE YEAR(date) = '$date' ;
By wrapping the $date inside quotes it becomes a string value. The numeric year value returned by YEAR(date) will never match against a string value.
i.e. 2015 != '2015'