0

so I have this page that shows today's matches by default, it has this datepicker form

<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" /> </p>
<input type="submit" value="Submit" name="usub" />
</form>
<?php
    if(isset($_POST["usub"])){ $date = $_POST["datepicker"]; }
else{ $date = date('Y-m-d'); } 
$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$date'") or die(mysql_error());

echo $_POST["usub"];
echo "<h4>$today Matches</h4> </br>";

//table
if (mysql_num_rows($data)==0){
     echo " No Matches";
     echo "</br>";
     echo "<h4> Sorry About That Check For other Days Or you can Check the Library</h4>";
 }
else{

echo "<table border='1'>
<tr>
<th>Match</th>
<th>Tourmanet</th>
<th>Date</th>
</tr>";

while($info = mysql_fetch_array( $data ))
  {
  echo "<tr>";
  echo "<td>" . $info['curmatch'] . "</td>";
  echo "<td>" . $info['tournamentname'] . "</td>";
  echo "<td>" . $info['matchdate'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
}
?>

what I want is if the user choose a date in the form it would go in the query and bring the data associated with that date while keeping today as the default one when they first load the page

1 Answer 1

4

You need to quote the datevalue in the query as

SELECT * FROM todaysmatches where matchdate= '$today'


$data = mysql_query("SELECT * FROM todaysmatches where matchdate='$today'") 

Now to get the datepicker value you need to change a bit in the form and use name attributes as

<form method="post">
<p> Select Date:<input id="datepicker" type="text" size="8" name="datepicker"/> </p>
<input type="submit" value="Submit" name="usub"/>
</form>

Then in PHP you have do as

if(isset($_POST["usub"])){
 $date = $_POST["datepicker"];
}

And use it in the query and in the else part you can have the query to get the data from today's date

NOTE : Make sure that the date you are passing from date picker to query is in proper format.

Sign up to request clarification or add additional context in comments.

10 Comments

thank you but that doesn't change the $today = date('Y-m-d'); in the query to the new date selected
First check if something is posted as above and if yes use the var $date in the query and in the else part i.e. when nothing is posted use the today in the query.
is this what you mean if ($date==0){ $data = mysql_query("SELECT * FROM todaysmatches where matchdate='$today'") or die(mysql_error()); } else{ $data = mysql_query("SELECT * FROM todaysmatches where matchdate='$date'") or die(mysql_error()); }
Well somewhat similar but your should use something as if(isset($_POST["usub"])){ $date = $_POST["datepicker"]; }else{ $date = date('Y-m-d'); } $data = mysql_query("SELECT * FROM todaysmatches where matchdate='$date'") or die(mysql_error()); And make sure that the form elements has name like I shown in my answer !!
Give a name as name="datepicker" in the datepicker and see ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.