Reputation: 5
Let me start off by saying I am (clearly) new to web development. What I am trying to do is query a mySQL database for the range of time of today's date, I have that part down if I hardcode it. What I would like is for my website to grab the current date each time it is loaded and display that data, then also have a set of drop boxes that a user can select a specific date for displaying the data. Here is what I have so far:
on my index.php page I have:
$result_date = mysqli_query($link, "SELECT date FROM MyData WHERE
YEAR(date)='$year'AND MONTH(date)='$month' AND DAY(date)='$day'");
If I use:
$year=date('Y');
$month=date('n');
$today = getdate();
$day=$today['mday'];
On that same index.php it worked fine, until I woke up this morning and it was still displaying yesterdays data and not updating to today (unless I hardcoded 2015, 7, 9) etc.
I have another page data.html.php that has all the front-end HTML stuff where I have written the code for a drop down menu and submit button, and other graphics for the website. I think what I need to do is place the year, month and day variables on that page and then pass it to the index.php page, but I'm not sure how. Here is my form... the ellipses are all the data choices:
<form action=" ">
<select name="Month">
<option value="01">Jan</option>
...
</select>
<select name="Day">
<option value="1">1</option>
...
</select>
<select name="Year">
<option value="2015">2015</option>
...
</select>
<input id="viewBtn" type="button" value="VIEW" onclick=" " />
</form>
So my goal is to have the default date be used unless a user selects a different date from the drop down menu and clicks VIEW. I imagine I need some kind of onClick method, the proper form actions and some way to pass the variables, I'm just stuck after googling it for a while now. Any help would be much appreciated!
Upvotes: 0
Views: 96
Reputation: 67505
Replace your form with :
<form action="" method="POST">
And your input with :
<input id="viewBtn" type="submit" name="submit" value="VIEW" />
Try to add this condition :
if(isset($_POST['submit'])){
$year=$_POST['Year'];
$month=$_POST['Month'];
$today = $_POST['Day'];
}else{
$year=date('Y');
$month=date('n');
$today = getdate();
}
$day=$today['mday']-1;
Now the default is Today date and if the user submit, the new values will be taken.
Hope this answers your question
Upvotes: 1