PHP Update checkboxs dynamically

Asked

Viewed 90 times

0

I’m having difficulty performing a function on a scheduling system,

Follow the picture: Foto do problema

I need to do the following, when the user selects a date the system makes a query in the database and fills in the check box. The consultation in the bank I can do, my problem is in creating a changue Event in the data field calling the query and filling the checkbox.

//

In the previous version of the system I did using Ajax, however I had a date select field and a time select field, so I used the script below, however the client requested the exchange for this table model.

<script type="text/javascript">
      $(document).ready(function(){
         $("select[name=predata]").change(function(){
$("select[name=prehora]").html('<option value="">Carregando...</option>');
            $.post("ajax-prehora.php?predata=13/06/2016",
                  {predata:$(this).val()},
 function(valor){
                     $("select[name=prehora]").html(valor);
                  }
                  )
         })          
             })

  • It would not be better to use input with type datetime from Html5?

  • Good afternoon friend my first version is this way and the client asked to be made something more visual, he wants that when a date is scheduled the table turns red, with the input was already running.

1 answer

0

The following is a simple example to show how checkboxes can be changed dynamically according to the value of a text field. Schedules are chosen by a calculation that can be replaced by a query with Ajax. See this page of the jQuery documentation if you need further explanations on how to checkboxes are selected dynamically.

Placed here at Jsfiddle the same example using an Ajax "simulation".

<!DOCTYPE html>
<script
  src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>

<script type="text/javascript">
$(document).ready(function() {
    $("input[name='predata']").keyup(function() {
        var data = $(this).val();
        var horarios = [];
        if (parseInt(data) % 2 == 0 ) horarios.push("08");
        if (parseInt(data) % 3 == 0 ) horarios.push("09");
        if (parseInt(data) % 5 == 0 ) horarios.push("10");
        $( "input[type='checkbox']").val(horarios);
    });
})
</script>

<html>
<body>
<input type="text" name="predata">
<p>
<input type="checkbox" class="prehora" id="hora08" value="08">
<label for="hora08">08:00</label>
<input type="checkbox" class="prehora" id="hora09" value="09">
<label for="hora09">09:00</label>
<input type="checkbox" class="prehora" id="hora10" value="10">
<label for="hora10">10:00</label>
</p>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.