user4949052
user4949052

Reputation:

Undefined variable error when using jQuery datepicker and PHP

How do I copy the datepicker date into the PHP variable, $adateStringValue?

When I use " $adateStringValue = $_POST['datepicker']; " I get Undefined variable: adateStringValue error. How do I rectify this problem?

The following is my code.

To select a date:

<script>
$(document).ready(function () {
   $("#datepicker").datepicker({
     dateFormat: "dd-mm-yy"   
     });
$('form#dateform').submit(function(){   
  var aselectedDate = $('#datepicker').val();
  localStorage.setItem('adate', aselectedDate);
  });
});
</script>

abc.php :

<?php
 echo "<form id=\"dateform\" name=\"dateform\" action=\"associate.php\" method=\"POST\"><br><br>
 <table>
 <tr><td>
 <b>Select date<b></td><td>
 <input id=\"datepicker\" name=\"datepicker\"value=\"$adateStringValue\"/>
 </td></tr>
 </table>
?>

I need to check this selected date with a column in the database( this column has dates in the string format).

if(isset($_POST['submit']))
{
$sql1="SELECT event_date from events_schedule";
$getDates_query= mysql_query($sql1,$con);
$adateStringValue = $_GET['datepicker']; 
while($fetchdates = mysql_fetch_array($getDates_query)) {
   if ($fetchdates['event_date'] == $adateStringValue) {
     $message = "You have selected this date";
     echo "<script>alert('$message');</script>";
    }
      else {
      $message1 = "Select another date";
      echo "<script>alert('$message1');</script>";
       }
     }
  }

The complete code is as follows:

<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function () {
 $("#datepicker").datepicker({
   dateFormat: "dd-mm-yy"   
 });
$('form#dateform').submit(function(){   
 var aselectedDate = $('#datepicker').val();
 localStorage.setItem('adate', aselectedDate);
  });
});
</script>

<?php
$con = mysql_connect("localhost","root","" );
if(!$con){
die("Cannot connect:" .mysql_error());
}
mysql_select_db("trainingschedule",$con);
echo "<form id=\"dateform\" name=\"dateform\" action=\"associate.php\"method=\"POST\"><br><br>
<table>

<tr><td>
<b>Select a date &nbsp;&nbsp;<b></td><td><input id=\"datepicker\" name=\"datepicker\" size=\"15\"    value=\"$adateStringValue \"  /></td></tr>
</table>
<br>
<input type=\"submit\" name=\"submit\" value=\"Submit\"/>;
</form>";

if(isset($_POST['submit']))
{
$sql1="SELECT event_date from events_schedule";
$getDates_query= mysql_query($sql1,$con);
$adateStringValue = $_POST['datepicker']; 
while($fetchdates = mysql_fetch_array($getDates_query)) {
 if ($fetchdates['event_date'] == $adateStringValue) {
 $message = "You have selected this date";
 echo "<script>alert('$message');</script>";
}
else {
 $message1 = "Select another date";
 echo "<script>alert('$message1');</script>";
  }
 }
}
mysql_close($con);
?>

Upvotes: 2

Views: 877

Answers (2)

Manno
Manno

Reputation: 399

Change your PHP to the following so that you are declaring $adateStringValue before it is used. Adjust accordingly to add your MySQL logic back in:

<?php

// Ternary IF statement to set either the $_POST value or a blank string
$adateStringValue = (isset($_POST['datepicker'])) ? $_POST['datepicker'] : '';

// Just use a blank action attribute to POST to the same file.
// Also added a space between the action/method attributes to output valid (X)HTML
echo '
      <form id="dateform" name="dateform" action="" method="POST"><br><br>
          <table>
              <tr>
                  <td><b>Select a date &nbsp;&nbsp;<b></td>
                  <td><input id="datepicker" name="datepicker" size="15" value="' . $adateStringValue . '"/></td>
              </tr>
          </table>
          <br>
          <input type="submit" name="submit" value="Submit"/>
      </form>
     ';

// Output the value if we have POST data
if (isset($_POST['submit'])) {
    $message = "You have selected this date: $adateStringValue";
    echo "<script>alert('$message');</script>";
}

?>

Upvotes: 1

B.Nadesh kumar
B.Nadesh kumar

Reputation: 226

<div id='datetimepicker1' >
<input type='text' class="form-control" name="datatime" data-date-format="MM/DD/YYYY" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>

<script type="text/javascript">
        $(function () {
            $('#datetimepicker1').datetimepicker();
        });
    </script>

Upvotes: 0

Related Questions