2

I'm trying to populate a select based on ajax data.

    <input id="datepicker" name="date" width="270" style = "display: block; float: center;"  required>

            <script> 
        $('#datepicker').datepicker({
                    format: 'd/m/yyyy',
            onSelect: function (date) {
                $.ajax({
                    type: "POST",
                    url: '../../Models/appointment/timesquery.php',
                    data: {
                        'date': date,
                    },
                }).done(function (data) {
                    $('select[name=times]').html(data);
                }).fail(function () {
                    alert('An error has occured.')
                });
            }
                });
        </script>

        <select  class="select-css" name = "times">
             <!-- Options will be added here dynamically using jQuery -->
</select>

timesquery.php

<?php
$date = $_POST['date']; // <====== Here is our date variable, you need to escape this!!!
$mysqli = NEW MySQLi('localhost','root','','cappeli');
$result = $mysqli->query("SELECT t.`hour` FROM `times` t LEFT JOIN ( SELECT `time` FROM `reservations` WHERE `date` = '$date' GROUP BY `time` HAVING COUNT(*) > 2) r on r.time = t.`hour` WHERE r.time IS NULL ");

while($rows = $result->fetch_assoc())
{
    $hd = $rows['hour'];
    echo "<option value = 'hairdressing'>$hd</option>";
}

?>

The select isnt populating, it staying null, and I replaced $date=$_POST['$date']; with $date='1/2/2020' just to make sure the query is correct, and when i opened the php file I got my list of values, but why isnt it populating in the select?

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Feb 2, 2020 at 19:05

2 Answers 2

1

Here is a working example of the front-end code, using a back-end script on my server, but the point is that the JS code works properly.

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

<input id="datepicker" name="date" width="270" style="display: block; float: center;" required>

<script>
  $('#datepicker').datepicker({
    format: 'd/m/yyyy',
    onSelect: function(date) {

      $.ajax({
        type: "POST",
        url: 'https://www.blackwicked.com/testajax.php',
        data: {
          'date': date,
        },
      }).done(function(data) {
        $('select[name=times]').html(data);
      }).fail(function() {
        alert('An error has occured.')
      });
    }
  });
</script>

<select class="select-css" name="times">
  <!-- Options will be added here dynamically using jQuery -->
</select>

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

3 Comments

@ElioRahy probably because you have not imported all the required Javascript libraries. To have the datepicker widget you need to include it in your project.
yeah probably, im so dumb
Btw, why did you accept the same solution from somebody else that was posted 10 minutes after mine?
0

Consider the following example.

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input id="datepicker" name="date" width="270" style="display: block; float: center;" required>

<script>
  $('#datepicker').datepicker({
    format: 'd/m/yyyy',
    onSelect: function(dt) {
      $.ajax({
        type: "POST",
        url: '../../Models/appointment/timesquery.php',
        data: {
          'date': dt,
        },
        dataType: "HTML",
        success: function(data) {
          $("select[name='times']").html(data).fadeIn();
        },
        error: function(x, e, t) {
          console.log(dt, e, t);
          alert("There was a connection issue, please review console for more details.");
        }
      });
    }
  });
</script>

<select class="select-css" name="times" style="display: none;">
  <!-- Options will be added here dynamically using jQuery -->
</select>

Unable to test in Snippet.

Comments

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.