How popular select with 2 json parameters

2

I'm making a form, and I have not found anything similar in the searches I've done.

I have the code link I am getting popular they usually,

When selecting a date, it populates the region, when the popular region populate a professional, and when popular the professional, it populates the schedule.

However, I need to select a checkbox for 'type of supplementary exam', it will bring information from the JSON of professional and also from checkbox to the time select. What I need is to create an array that has both ids, both professional and checkbox, in time.

I need to have the 2 parameters (professional and (type of exam) be sent to the same array, and return to the select (schedules) I'm using a json switch case

I have to pass the checkbox parameter and it interacts with the array of professional, and refresh the schedule, that's basically it

 <?php 

 header('Content-type: text/json'); $retorno = array();

 switch($_POST['profissional']) {    case '21': // 
       $retorno = array(         0 => "Selecione um horário",  
         41 => "18h30",

       );
       break;    case '24': // 
       $retorno = array(         0 => "Selecione um horário",  
          42 => "18h50",

       );
       break;

    case '27': // 
       $retorno = array(         0 => "Selecione um horário",   
          43 => "20h30",

       );
       break;    case '30': // 
       $retorno = array(         0 => "Selecione um horário",   
          44 => "20h00",

       );
       break;
         case '33': // 
       $retorno = array(         0 => "Selecione um horário",     
          45 => "22h45",

       );
       break;

             case '35': // 
       $retorno = array(         0 => "Selecione um horário",   
          46 => "22h00",

       );
       break; }


 echo json_encode($retorno);

 ?>

// call checkbox

<?php 

header('Content-type: text/json');
$retorno = array();

switch($_POST['inlineCheckbox1'])
{
   case 'option1': // regiao 1
      $retorno = array(
         0 => "Selecione um horário (checkbox)",  
         91 => "18h30",
         95 => "19h30",

      );
      break;
   case 'option2': // regiao 2
      $retorno = array(
         0 => "Selecione um horário (checkbox)",  
         92 => "18h50",
         99 => "22h50",


      );
      break;

   case 'option3': // regiao 2
      $retorno = array(
         0 => "Selecione um horário (checkbox)",   
         93 => "20h30",
         97 => "22h30",

      );
      break;

}


echo json_encode($retorno);

?>

How can I make this change in the array to receive the 2 parameters?

Can anyone help me? Thanks in advance.

    
asked by anonymous 18.06.2015 / 06:04

1 answer

2

To send to javascript using more than one value use:

data: {regiao: $("#regiao").val(),
profissional: $("#profissional").val()}

javascript will then send the two values at the same time to the requested page. You can not use ajax to send to two pages at a time.

I also recommend using only a $ (document) .ready () and putting all the content together, so code would be more readable too:

$(document).ready(function(){
$('#regiao').bind('change', function(){
$.ajax({
 type: "POST",
     url: "teste2.php",
     data: {regiao: $("#regiao").val()},
     dataType: "json",
     success: function(json){
        var options = "";
        $.each(json, function(key, value){
           options += '<option value="' + key + '">' + value + '</option>';
        });
        $("#profissional").html(options);
     }
   });
 });
// AQUI É O CHECKBOX

$('#inlineCheckbox1').on('click', function() {
  $.ajax({
     type: "POST",
     url: "teste4.php",
     data: {inlineCheckbox1: $("#inlineCheckbox1").val()},
     dataType: "json",
     success: function(json){
        var options = "";
        $.each(json, function(key, value){
           options += '<option value="' + key + '">' + value + '</option>';
        });
        $("#horarios").html(options);
     }
  });
});

// AQUI É O PROFISSIONAL

 $('#profissional').bind('change', function(){
  $.ajax({
     type: "POST",
     url: "teste3.php",
     data: {profissional: $("#profissional").val()},
     dataType: "json",
     success: function(json){
        var options = "";
        $.each(json, function(key, value){
           options += '<option value="' + key + '">' + value + '</option>';
        });
        $("#horarios").html(options);
     }
     });
   });
});

Reply to comment:

Yes, you can intercept in javascript the selected values before sending to ajax in several ways. One trick that you can use is to modify the tag name of all checkboxes to option and their values to 1 and 2 and 3. Modify their id to clin, derm and odont. So you can just send the following to jquery:

data: {regiao: $("#regiao").val(),
    profissional: $("#profissional").val(),
    tipo: [ $("#clin").val(), $("#derm ").val(), $("#odont").val()]
}

Then you will need to break the type into the php through split.

or

data: {regiao: $("#regiao").val(),
    profissional: $("#profissional").val(),
    clin: $("#clin")[0].checked?$("#clin").val():'',
    derm: $("#derm")[0].checked?$("#derm").val():'',
    odont: $("#odont")[0].checked?$("#odont").val():''
 }

Then you will have a variable for each checkbox

    
19.06.2015 / 16:32