Pass a variable that is in a javascript file to a $_SESSION and pick up a php page

Asked

Viewed 53 times

2

Good evening friends.

Next I need to pass a variable from a . js file to a Session and to pick up another page with php. The javascript function returns the first available time for marking a query, and I need to leave this set initially on another page.

var hr = now.getHours();
var min = now.getMinutes();
var time_format = hr + ":" + min;
//document.write ("<h3> Hoje é " + hr + ":" + min + ". </h3>");
//$('#next_time').html('28 de dezembro (sexta-feira) - 14:00 horas');
var seconds = now.getTime() / 1000;
var doctor_id = $('#doctor_id').val();
var clinic_id = $('#clinic_id').val();
$.ajax({
  type: "GET",
  url: base_url+"Searchdoctor/doctor_availability?"+"date="+seconds+"&doctor_id="+$('#doctor_id').val()+"&offset=0&clinic_id="+$('#clinic_id').val(),
  success: function(result){
        if(result.data.time_interval.length>0)
        {
          $('#schedule-consult-timeslot').html('<option disabled selected>'+'Horarios disponiveis'+'</option>');
          $.each(result.data.time_interval, function (i, item) {
            if(item.length != 0){
                var split = item.time.split('-');
                if(compararHora(time_format,split[0])){
                   $('#next_time').html(now.getDate ()+ ' de '+ monName[now.getMonth()]+ ' ('+dayName[now.getDay()]+') '+ '- '+ split[0]+ ' horas');
                   return false;
                }
            }

          })
        }else{
           recursiveDate(1);
           //console.log( adicionarDiasData(3) );
        }
    },
    error: function(xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    }

  });

  function compararHora(hora1, hora2)
{
    hora1 = hora1.split(":");
    hora2 = hora2.split(":");

    var d = new Date();
    var data1 = new Date(d.getFullYear(), d.getMonth(), d.getDate(), hora1[0], hora1[1]);
    var data2 = new Date(d.getFullYear(), d.getMonth(), d.getDate(), hora2[0], hora2[1]);

    return data1 < data2;
}

function recursiveDate(cont){
    /*if(cont == 5){
        console.log('parou');
    }else{
        console.log(cont);
        cont = cont + 1;
        recursiveDate(cont);
    }*/
    var seconds = adicionarDiasData(cont);
    $.ajax({
      type: "GET",
      url: base_url+"Searchdoctor/doctor_availability?"+"date="+seconds+"&doctor_id="+doctor_id+"&offset=0&clinic_id="+clinic_id,
      success: function(result){
            if(result.data.time_interval.length>0)
            {
              $('#schedule-consult-timeslot').html('<option disabled selected>'+'Horarios disponiveis'+'</option>');
              $.each(result.data.time_interval, function (i, item) {
                if(item.length != 0){
                    var split = item.time.split('-');
                       date = getDateFormat(cont);
                       $('#next_time').html(date.getDate ()+ ' de '+ monName[date.getMonth()]+ ' ('+dayName[date.getDay()]+') '+ '- '+ split[0]+ ' horas');
                       //colocar a session com todos os dados acima;
                       return false;
                }
              })
            }else{
              cont = cont + 1;
              recursiveDate(cont);
            }
        },
        error: function(xhr, status, error) {

            console.log(error);
        }

      });
}

function adicionarDiasData(dias){
  var hoje        = new Date();
  var dataVenc    = new Date(hoje.getTime() + (dias * 24 * 60 * 60 * 1000));
  return dataVenc.getTime() / 1000;
  //return dataVenc.getDate() + "/" + (dataVenc.getMonth() + 1) + "/" + dataVenc.getFullYear();
}

function getDateFormat(dias){
  var hoje = new Date();
  return new Date(hoje.getTime() + (dias * 24 * 60 * 60 * 1000));
}

what I need to pass to a SESSION is exactly what’s inside the id #next_time. Because I need to take another php page.

THAT -> date.getDate()+ ' from '+ monName[date.getMonth()]+ ('+dayName[date.getDay()]+') '+ '- '+ split[0]+ ' hours');

Thank you :)

  • I believe this post can help you: https://stackoverflow.com/questions/35091757/parse-javascript-fetch-in-php basically is to fetch a js by passing the variable on the body and retrieving it in php.

  • Maybe a sessionStorage from the JS itself. See in this answer how simple it is to create one and recover the value: https://answall.com/a/357226/8063

1 answer

0

What I would do is this...

if(compararHora(time_format,split[0])){
    var ndate = now.getDate ();
    var nmonth = monName[now.getMonth()];
    var nday = dayName[now.getDay()];
    var nhour = split[0];       

    $.ajax({
        type: "POST", 
        data: {ndate:ndate, nmonth:nmonth, nday:nday, nhour:nhour},
        url: BASE_URL + "Searchdoctor/set_date",
        success: function(data) {
            ('#next_time').html(now.getDate ()+ ' de '+ monName[now.getMonth()]+ ' ('+dayName[now.getDay()]+') '+ '- '+ split[0]+ ' horas');
        }
    }); 

    return false;
}

The function inside the controller Searchdoctor -> set_date:

function set_date(){
    $set_date['ndate'] = ($this->input->post('ndate')) ? $this->input->post('ndate') : '';
    $set_date['nmonth'] = ($this->input->post('nmonth')) ? $this->input->post('nmonth') : '';
    $set_date['nday'] = ($this->input->post('nday')) ? $this->input->post('nday') : '';
    $set_date['nhour'] = ($this->input->post('ndate')) ? $this->input->post('nhour') : '';

    return $this->session->set_userdata('search_return', $set_date);
}

And to display, in your view:

echo $this->session->userdata('search_return')->ndate . 'de' . $this->session->userdata('search_return')->nmonth . ' ' . $this->session->userdata('search_return')->nday . ' ' . $this->session->userdata('search_return')->nhour . 'horas';
  • 1

    Thanks buddy, I’ll try to do it that way and put the result.

Browser other questions tagged

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