ugursemizoglu ugursemizoglu - 7 months ago 39
PHP Question

jQuery with Ajax, mysql, Data extraction from Logic

var dates = [new Date(2014, 02 - 1, 19),
new Date(2014, 02 - 1, 20),
new Date(2014, 02 - 1, 21),
new Date(2014, 01 - 1, 21)
];


With my data entry, but I have my data from the database, written on automatic.
I don't know jQuery much, I'm a beginner.

What I want to do: The data in the database as a variable to define

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="iso-8859-9">
<title>jQuery UI Datepicker - Türkçe Takvim Textbox'u...</title>
<link rel="stylesheet" href="css/jquery.ui.all.css">
<link rel="stylesheet" href="css/demos.css">
<script src="js/jquery-1.7.1.js"></script>
<script src="js/jquery.ui.core.js"></script>
<script src="js/jquery.ui.widget.js"></script>
<script src="js/jquery.ui.datepicker.js"></script>
<script src="js/jquery.ui.datepicker-tr.js"></script>
<style type="text/css">
#highlight, .highlight {
/*background-color: #000000;*/
}
.highlight > a.ui-state-default {
background: url("images/ui-bg_highlight-soft_25_ffef8f_1x100.png") repeat-x scroll 50% top #FFEF8F !important;
border: 1px solid #F9DD34;
color: #363636;
}
</style>

<script type="text/javascript">
var dates = [new Date(2014, 02 - 1, 19),
new Date(2014, 02 - 1, 20),
new Date(2014, 02 - 1, 21),
new Date(2014, 01 - 1, 21)
];

$(function() {

$('#datepicker').datepicker({
numberOfMonths: [1, 1],
beforeShowDay: highlightDays
}).click(function() {
$('.ui-datepicker-today a', $(this).next()).removeClass('ui-state-highlight ui-state-hover');
//$('.highlight a', $(this).next()).addClass('ui-state-highlight');
});


$('#datepicker').click(function() {
// put your selected date into the data object
var data = $('#datepicker').val();
/*
$.get('getdata.php?date=' + data, function(data) {
$('#events').html(data).show('slow');
});*/
});

function highlightDays(date) {
for (var i = 0; i < dates.length; i++) {
if (dates[i].getTime() == date.getTime()) {
return [true, 'highlight'];
}
}
return [true, ''];
}
});
</script>
</head>
<body>
<div id="datepicker"></div>
</body>
</html>

Answer

If I understand you correctly you want to use the data from your database as a variable in javascript that you can then manipulate.

Where you declare your dates, also declare a variable var dbData;. Then in your listener:

$('#datepicker').click(function() { // put your selected date into the data object var selectedDate = $('#datepicker').val(); $.get('getdata.php?date=' + selectedDate, function(data) { //assign the values returned from the php call to a variable dbData = data; //$('#events').html(data).show('slow'); }); });

The main problem (assuming your php file is doing what it should) was that in your listener, you defined the data variable twice. By changing it to use different variable names, it is possible to update a javascript variable to contain the data returned by your get request.

If you are looking for more advanced ways of manipulating the data returned in your get function in javascript in much the same way as you would database tables, have a look at http://www.taffydb.com/