Send HTML by JSON with PHP

0

I have this HTML that I generate with the function of php selectHorarios ()

<option>13:15</option>
<option>13:30</option>
<option>13:45</option> 
<option>14:00</option>

That I send it with this PHP file

require "config/config.php";
require "listadoTurnosFunc.php";
$dia = 

    $datos = array(
    'fecha' =>selectHorarios($_POST['medico'],$_POST['fecha'],$_POST['horadesde'],$_POST['horahasta']);
    );

    echo json_encode($datos, JSON_FORCE_OBJECT);

And I receive it with this javascript function to update a schedule selection

 $("#fecha").on("change",function(){
selectHorarios = {
      fecha: $('#fecha').val(),
      medico: $('#medico').val(),
      horadesde: $('#horadesde').val(),
      horahasta: $('#horahasta').val()
  }; 
$.post('buscadorSelectHorarios.php', selectHorarios, function(data, 
textStatus) {
  if(textStatus=="success"){
    $("#fecha").html(data.fecha);
  }
}, 
"json");

But the Json gives me back

{"fecha":"13:15<\/option>13:30<\/option>13:45<\/option>14:00<\/option>"}

There is a way to pass the html by json in php so that it takes the html characters as "/ < >" ?

    
asked by Joaquinlio 12.10.2018 в 10:25
source

1 answer

0

Try placing your json_encode like this:

echo json_encode($datos, JSON_HEX_QUOT | JSON_HEX_TAG);

With this your answer would be something like:

"\u003Coption\u003E13:15\u003C\/option\u003E\n \u003Coption\u003E13:30\u003C\/option\u003E\n \u003Coption\u003E13:45\u003C\/option\u003E \n \u003Coption\u003E14:00\u003C\/option\u003E"

You can see it here .

And then in your ajax you simply do this:

$.post('buscadorSelectHorarios.php', selectHorarios, function(data, 
textStatus) {
  if(textStatus=="success"){
    $("#fecha").html(data);
  }
}, 
"json");

var fechaJson = '\u003Coption\u003E13:15\u003C\/option\u003E\n \u003Coption\u003E13:30\u003C\/option\u003E\n \u003Coption\u003E13:45\u003C\/option\u003E \n \u003Coption\u003E14:00\u003C\/option\u003E';

$("#fecha").html(fechaJson);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Horario:</label> <select id="fecha"></select>
    
answered by 12.10.2018 / 15:23
source