Jump to content
Larry Ullman's Book Forums

Help! Ajax Datepicker With Php


Recommended Posts

Hello -I'm trying to build a datepicker which only allows certain dates in my database to be selected..

Below are the codes...and the datepicker widget shows up but the "AvailableDate" (which is listed in the database as "2012-05-09") are not showing up in different color. Please can someone help to see where the error is?

Thank you!

 


[left]<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>


<link href="css/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<script type = "text/javascript">
$(function(){
$.ajax({
  url: "dates2.php",
  data: "action=showdates&id=1",
  dataType: "json",
  success: function(calendarEvents){
		   $(".calendarwidget").datepicker({

		   // [rows, columns] if you want to display multiple calendars.
		   numberOfMonths: [1, 1],
		   showCurrentAtPos: 0,
		   beforeShowDay: function (date){
						  for (i = 0; i < calendarEvents.length; i++) {
							  if (date.getMonth() == calendarEvents[i][0] - 1
							  && date.getDate() == calendarEvents[i][1]
							  && date.getFullYear() == calendarEvents[i][2]) {
							  //[disable/enable, class for styling appearance, tool tip]
							  return [false,"ui-state-active","Event Name"];
							  }
						   }
						   return [true, ""];//enable all other days
						}
		   });
		   }
 });
});
</script>
</head>

<body>
<div class="calendarwidget"><!--calender widget is loaded here-->

</div>
</body>
</html>[/left]


[left]

[/left]

 

 

<?php

[left]//DB CONFIG
$hostname_logon = '***';  //Database server LOCATION
$database_logon = '****';			//Database NAME
$username_logon = '***'; //Database USERNAME
$password_logon = '***';	//Database PASSWORD

//Table config
$tablename = "venue_room_availability"; //Name of the DB table
$event_id = "AvailableID";			//Primary Key field of the table

//Connect to the DB
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ('Unabale to connect to the database');
mysql_select_db($database_logon) or die ("Error in query: $qry. " . mysql_error());  

/****** Bread & Butter *************/
$action = $_REQUEST['action'];

switch ($action){
case 'showdates':
$qry = "SELECT * FROM ".$tablename."";
	$result = mysql_query($qry) or die ("Error in query: $qry. " . mysql_error());
	echo '[';

	while($row = mysql_fetch_assoc($result));
	//$row['AvailableDate'] =rtrim($row['AvailableDate'], ",");
	echo $row['AvailableDate'];
	echo ']';
break;
}

?>[/left]


[left]

[/left]

Link to comment
Share on other sites

I'm a little unclear on what you're trying to do, so if you could please answer my questions, I think we can better help you.

 

1) Is the intended value being correctly retrieved from the DB and stored in the $row['AvailableDate'] variable?

 

2) Why are you echoing echo [ and ] before and after the $row['AvailableDate'] variable?

 

3) Why are you sending the ID to the PHP script but not using it in the query?

 

4) Why are you grabbing all the records from the DB table, and then breaking the while loop after the first row that's returned, regardless of what's actually stored in the $row['AvailableDate'] variable after grabbing the first row of results from the DB?

 

Also, you can (and should) remove all your DB credentials from your post. They're not relevant to the post.

Thank you.

  • Upvote 1
Link to comment
Share on other sites

Hello, HartleySan - Thank you very much for your reply!

Firstly, let me start by saying that the codes combine a few codes i've found on Larry's Javascript book or on the web, particularly on http://www.emirplicanic.com/javascript/jquery-ui-highlight-multiple-dates-in-jquery-datepicker. Meaning, there's probably a v good reason why it's not working and/or I won't be able to answer all of the below in a coherent manner!

Here are my answers to your questions

 

1) Is the intended value being correctly retrieved from the DB and stored in the $row['AvailableDate'] variable?

Yes, I have double checked that the Available Date variable is being retrieved correctly

 

2) Why are you echoing echo [ and ] before and after the $row['AvailableDate'] variable?

If you are referencing the "echo '['" entries then this is where I can't give you an intelligent answer. I have seen these echo entries being used pre-post the $row when SQL SELECT is entered as part of "switch case." I've seen it as either "[" or "{", but they all seem to be used when SQL SELECT is being used. If I remove them and rewrite the "while mysql row fetch" argument in a way I'm more used to seeing, the page won't even display a datepicker. So I know they are needed, just not sure why...

 

3) Why are you sending the ID to the PHP script but not using it in the query?

Great question -so I plan to use this ID in a "GET" function and further fine tune what mysql SELECT retrieves. In the meantime, in a lazy way, I just wanted to make sure my jquery was working so I haven't instituted that GET function yet (nor is it needed yet) and just retrieved all records from this table to see if I could get AvailalbleDate to show

 

 

4) Why are you grabbing all the records from the DB table, and then breaking the while loop after the first row that's returned, regardless of what's actually stored in the $row['AvailableDate'] variable after grabbing the first row of results from the DB?

I'm grabbing all the records - -as mentioned above - for pure laziness reason for now...In terms of breaking the while loop, if you are referencing the fact that "{" and "}" is not shown after the while loop (and then including a ";" straight afterwards), then I must admit that, as mentioned on #2, I noticed this is how it was written on the URL mentioned above. I noticed too that this was breaking the while loop, so I rewrote it in the way I've seen and learned from Larry's PHP books before ..and then a blank page comes out

 

If you could help me again as you have already done so in my other posts, I'd greatly appreciate it! Thank you!

 

 

Hello -I'm trying to build a datepicker which only allows certain dates in my database to be selected..

Below are the codes...and the datepicker widget shows up but the "AvailableDate" (which is listed in the database as "2012-05-09") are not showing up in different color. Please can someone help to see where the error is?

Thank you!

 


[left]<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>


<link href="css/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<script type = "text/javascript">
$(function(){
$.ajax({
  url: "dates2.php",
  data: "action=showdates&id=1",
  dataType: "json",
  success: function(calendarEvents){
		   $(".calendarwidget").datepicker({

		   // [rows, columns] if you want to display multiple calendars.
		   numberOfMonths: [1, 1],
		   showCurrentAtPos: 0,
		   beforeShowDay: function (date){
						  for (i = 0; i < calendarEvents.length; i++) {
							  if (date.getMonth() == calendarEvents[i][0] - 1
							  && date.getDate() == calendarEvents[i][1]
							  && date.getFullYear() == calendarEvents[i][2]) {
							  //[disable/enable, class for styling appearance, tool tip]
							  return [false,"ui-state-active","Event Name"];
							  }
						   }
						   return [true, ""];//enable all other days
						}
		   });
		   }
 });
});
</script>
</head>

<body>
<div class="calendarwidget"><!--calender widget is loaded here-->

</div>
</body>
</html>[/left]


[left]

[/left]

 

 

<?php

[left]//DB CONFIG
$hostname_logon = '***';  //Database server LOCATION
$database_logon = '****';			//Database NAME
$username_logon = '***'; //Database USERNAME
$password_logon = '***';	//Database PASSWORD

//Table config
$tablename = "venue_room_availability"; //Name of the DB table
$event_id = "AvailableID";			//Primary Key field of the table

//Connect to the DB
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ('Unabale to connect to the database');
mysql_select_db($database_logon) or die ("Error in query: $qry. " . mysql_error());  

/****** Bread & Butter *************/
$action = $_REQUEST['action'];

switch ($action){
case 'showdates':
$qry = "SELECT * FROM ".$tablename."";
	$result = mysql_query($qry) or die ("Error in query: $qry. " . mysql_error());
	echo '[';

	while($row = mysql_fetch_assoc($result));
	//$row['AvailableDate'] =rtrim($row['AvailableDate'], ",");
	echo $row['AvailableDate'];
	echo ']';
break;
}

?>[/left]


[left]

[/left]

Link to comment
Share on other sites

Okay, I think I get it, and I suspect that your dates are not in the right format in the DB, which is why it's not working.

 

I think the author of the site you linked to echos the [ and ] before and after the while loop because he's returning a string that can be easily parsed via JSON or something (I sure hope it's not eval!).

Anyway, the author notes that the dates in the DB need to be in UNIX timestamp format. Are your dates like that? The following is an example of a UNIX timestamp:

January 1 1970 00:00:00 GMT

 

I'd confirm that your DB date format is correct, and after that, I'd copy the code exactly like it's used on the site you linked to, and make sure you can get it working like that. At the moment, your code looks radically different from the site author's code, and I think you code contains some errors that will cause it to not work properly.

 

Once you have it working like the site, then I think you're free to modify it as you see fit.

Lemme know how that works out.

  • Upvote 1
Link to comment
Share on other sites

Hello, Hartleysan

Apologies for not getting back. I ended up finding different set of codes which worked . I posted it under a different question I just posted under "Ajax & posting radio button". I've been trying to build this dynamic reservation system for ages now and seems to get stuck every time I think I have it sorted. I'd greatly appreciate your reply to the new topic if you have the chance as it's taken the question I've written above to an progressive stage.

Link to comment
Share on other sites

 Share

×
×
  • Create New...