pass javascript variable to a php variable

Asked

Viewed 399 times

2

Good morning, everyone,

I’m having a question:

i have a select that takes the one array from the database, and every time the user changes the select it has a change in javascript that update it in an input of type Hidden >

<input id="id_clinic_hidden" hidden name="clinic_id">

the javascript function is this:

$(document).ready(function () {
    $(document).on("change", "#doc_sel_clinic", function(){
        var id = $(this).val();
        $("#id_clinic_hidden").val(id);
        alert(id);
    });
});

What I want to do is take this javascript id variable for php, it can be via session. This is inside javascript because I need to change the value when the user changes the select.

thank you

3 answers

2

Follow an example:

var myval = 'test';

$.ajax({
  type: 'POST',
  url: 'seuphpfile.php',
  data: {'variable': myval},
});

Receiving variable in PHP (seuphpfile.php)

$minhaVariavel = $_POST['variable'];
  • Hello, I’m not in the notebook yet to test, but this updates the variable every time the onchange event happens?

2

For this you will need to work with ajax, who makes requisitions http asynchronous form;

$.ajax({
   url : 'caminho_do_php_que_ira_recebe_o_input.php',
   dataType : 'html',
   type : 'POST',
   data : {'id' : id},
   beforeSend : function () {
         console.log('Carregando...');
   },
   success : function(retorno){
       console.log(retorno);
   },
   error : function(a,b,c){
       console.log('Erro: '+a[status]+' '+c);
   }
});

Basically it is to call in a function (at the time you want to send) and ready, on the server side, just receive as follows:

$valor_recebido = $_POST['id'];

If you wanted to receive some feedback after sending with the return of PHP, just receive and work with this variable call retorno (if you think it best to name as you prefer).

  • 1

    good, what is this dataType for?

  • 1

    dataType is formed server response, I particularly only use html or json, but it is not a mandatory field.

0

did so in javascript as vcs recommended:

$(document).ready(function() {
  $(document).on("change", "#doc_sel_clinic", function(){
    var id = $(this).val();
    $("#id_clinic_hidden").val(id);

    $.ajax({
      type: 'POST',
      url: base_url + 'Doctor/config_schedule',
      data: {'id_selected': id},
    });

    console.log(id);
  });

and tried searching for in php>

<?php $minhaVariavel = $_POST['id_selected']; ?>

However I did not succeed, returns error:

Message: Undefined index: id_selected
  • you have to test to see if it exists example:<?php if(isset($_POST['id_selected'0) $minhaVariavel = $_POST['id_selected']; ?>

  • yes, isset does not solve my problem, because I wanted it to be updated at the onchange event as I showed. The page shows the data according to what is chosen in this select, then every time the user chooses a select he has to update. I can’t do this.

  • i do not know if I am passing the url path of ajax correctly, because here we use url amigavél and codeigniter, do not know if this is it.

Browser other questions tagged

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