As popular select with 2 json parameters

Asked

Viewed 1,469 times

2

I’m making a form, and I didn’t find anything like it in the research I did.

I have the code http://jsfiddle.net/rubensoul/xdmz35oy/3/ I’m getting popular them normally,

When selecting a date he populates the region, popular the region populates a professional, and popular the professional, he populates the time.

However, I need to select a 'complementary exam type' checkbox, it brings professional JSON information and also checkbox information to the time select. What I need is to create an array that has both professional and checkbox ids on time.

i need then, that the 2 parameters (professional and (type of exam) were sent to the same array, and return to select (schedules) I am using a switch case of json

I have to pass the checkbox parameter and it interact with the professional array, and update 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 to 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.

  • If I understand correctly you want when the user selects a checkbox to go a request to the server that returns a JSON and you want that information to go to the select "Available Times"... is that it? What is not working? Any errors? In AJAX you are sending data: {inlineCheckbox1: $("#inlineCheckbox1").val()}, but I don’t see in PHP where you’re using that inlineCheckbox1.

  • Hello, @Sergio can help me again?

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. Unable to use ajax to send to two pages at the same time.

I also recommend using only one $(Document). ready() and put all the contents 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 the comment:

Yes, you can intercept in javascript the selected values before sending to ajax in several ways. One trick you can use is to modify the name tag 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 send only the following to jquery:

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

Then you’ll need to split the type in php.

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

  • Okay, I made the change you suggested, http://jsfiddle.net/rubensoul/xdmz35oy/10/ but how do I get the json array to recognize the 2 parameters? I must take the id of professional and also checkbox, and how should I put the checkbox to update by json? " <input type="checkbox" id="inlineCheckbox1" value="option1" name="option1"> Clinical" something like that [] ?

  • I was able to solve this question, based on what I said, but now I have to leave this dynamic checkbox input, that is, each one that is sealed, has to have a different value, can you help me? http://jsfiddle.net/rubensoul/r92o6fb4/1/

  • Okay, I used an id outside the checkbox, and it worked, only I’m not getting past the value of each checkbox, I used: var data = new Array(); $("input[name='checkbox']:checked"). each(Function(i) { data.push($(this).val()); }); before $ajax, which I’m doing wrong?

  • I changed the answer, forgot to test if it is selected or not. [0] in front of jquery transforms a jquery object into pure javascript object

  • i used more or less what I said, from a look http://jsfiddle.net/rubensoul/r92o6fb4/2/ I just need now, that it sends the value of the checkbox to the json (php), the way I put it right?

  • check http://jsfiddle.net/r92o6fb4/3/, I think that’s what you need. Test on the Php side if $_REQUEST["Clin"] != '' and the rest. Switch to ifs in each of these requests and treat them one by one. Since you used checkbox instead of options, you can select more than one option, so you have to test the three options

  • I understood, is that I need this value to be dynamic, I put the following code: checkbox:$($("#checkbox input")[0]). prop("checked")? $($("#checkbox input")[0]). val():'', it brought the value, legal and worked, but since it has 3 and may have more fields depending on the client, it would need to meet all inputs checkbox, so I put the checkbox name []

Show 2 more comments

Browser other questions tagged

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