PLEASE NOTE: I'm not getting any errors, just not functioning the way my logic says it should.
I'm attempting to create a calendar application, essentially my goal at this stage, is on the $document.load of the calendar page, I wish for it to loop through each day of the calendar (1 month) and for each day, send a request to PHP (AJAX, $.post()), query database, and then append those results onto the element it is currently accessing with the.each() function.
What I want/expect to happen:
I expect the results that are passed back from PHP to be appended onto the current day element it is posting off for.
What does happen:
Unfortunately what seems to be happening, is that the post is done for each element (day in the month) but the append, is only happening once and appending to the last element in the set, although as you can see below, my.append is inside the.each()
Please help guys, I'm not sure how I can make it pause after each post and append to the current element, instead of the last!
CODE LISTING:
HTML:
<ul class="days">
<li><span>1</span></li>
<li><span>2</span></li>
<li><span>3</span></li>
<li><span>4</span></li>
<li><span>5</span></li>
<li><span>6</span></li>
<li><span>7</span></li>
<li><span>8</span></li>
<li><span>9</span></li>
<li><span class="active">10</span></li>
<li><span>11</span></li>
<li><span>12</span></li>
<li><span>13</span></li>
<li><span>14</span></li>
<li><span>15</span></li>
<li><span>16</span></li>
<li><span>17</span></li>
<li><span>18</span></li>
<li><span>19</span></li>
<li><span>20</span></li>
<li><span>21</span></li>
<li><span>22</span></li>
<li><span>23</span></li>
<li><span>24</span></li>
<li><span>25</span></li>
<li><span>26</span></li>
<li><span>27</span></li>
<li><span>28</span></li>
<li><span>29</span></li>
<li><span>30</span></li>
<li><span>31</span></li>
</ul>
jQuery:
$(".days li").each(function()
{
$dayNumber = $(this).text();
$this = $(this);
$.post("getShiftsMV.php", {dayNumber:$dayNumber}, function(results)
{
$this.append(results);
});
});
PHP:
$shifts = 0;
if($_POST["dayNumber"] != "" )
{
$day = $_POST['dayNumber'];
$sql = "SELECT * FROM shifts WHERE shift_date = '$day'";
$run = $con->query($sql);
if($run->num_rows > 0)
{
while($row = $run->fetch_assoc())
{
$shifts++;
}
}
if($shifts > 0)
{
echo'<span class="label label-success">' . $shifts .'</span>';
}
else
{
echo'';
}
}
=========================================================================
EDIT:
Using $var = "test";
for example, does work, as it will run in the console just fine.