call a variable in another php without get or post

2

Thank you very much to the community in advance.
good to the matter, I have created a simple dynamic select option that when selecting a client in the next select option your pets come out.

Here I charge the clients in the first select option.

<label for="inputEmail1" required class="col-lg-2 control-label">Paciente:</label>
<select id="id_paciente" name="id_paciente" class="form-control" onchange="MostrarMascota()">
<option value="" selected disabled>---SELECCIONE---</option>
<?php
$salx=mysql_query("SELECT * FROM pacientes ");  

while($col=mysql_fetch_array($salx)){
echo '<option value="'.$col['id'].'">'.$col['nombre'].'</option>';
                         }
 ?>   

and the one for pets is the following code:

   </select>
  <br>
   <label for="inputEmail1" required class="col-lg-2 control-label">Mascotas:</label>
   <div  id="div_mascot">
   <select name="mascotas" type=text  id="mascotas" autocomplete="off"     class="form-control"  required>
<option value=""></option> 
</select>
   </div>
   <br>

To get the client's pets out I had to make another php that performs the respective query with the id already obtained by a get.
This would be the code that I mention:

<select name="mascotas" id="mascotas" class="form-control">
<option value="">- Seleccione Mascota -</option>
<?php
     include '/conexion.php';
     $IdEspe = $_POST['id'];
   $sql = "select mascotas.id, mascotas.nombre from mascotas where     idpaciente= $IdEspe";
      $result = $mysqli->query($sql);
  while( $row = $result->fetch_assoc()) 
      {
        $idcitass=$row['id'];
     ?>
    <option  value="<?php echo $row['id']."~".$row['nombre'] ?>" ><?php echo $row['nombre']; ?></option>
    <?php
       $valor=explode("~",$_POST["mascotas"]);
        $clase=$valor[0];
        $clase2=$valor[1];
     }
     ?>
 </select>  

a select option was made that has two variables per value separated by the sign ~. The problem I have is that when wanting to manipulate the variables $ class and $ class2 to the other php by means of an include "combomascota.php"; I get an error on the page and I have no idea how to get those two variables in the other php.

    
asked by jota 21.10.2016 в 07:11
source

2 answers

3

I do not think it's a valid option to use variables of SESSION for your example,

  

The sessions follow a simple workflow. When a session is started, PHP will retrieve an existing session using the last ID (usually from a session cookie) or, if a session is not passed, a new session will be created. PHP will fill the superglobal variable $ _SESSION with any data of the session started. When PHP closes, it automatically takes the contents of the superglobal variable $ _SESSION, serializes it, and sends it to store it using the session storage manager.

EDIT

Your pet selector has an id id="mascotas" , the only way you can read and manipulate the value of the selection that the user gave is AFTER the request GET or POST .

Your HTML is client side while your PHP logic is server side ..

What you should do in your OTHER file PHP that is what you do post is

HTML page of the form

<select name="mascotas" id="mascotas" class="form-control">
<option value="">- Seleccione Mascota -</option>
<?php
  include '/conexion.php';
  $IdEspe = $_POST['id'];
  $sql = "select mascotas.id, mascotas.nombre from mascotas where     idpaciente= $IdEspe";
  $result = $mysqli->query($sql);
  while( $row = $result->fetch_assoc()) {
        $idcitass=$row['id'];
        echo '<option   value="'.$row["id"]."~".$row["nombre"].'" >'.$row["nombre"].'</option>';
  }
?>
</select>  

And there to work it.

PHP File

$valor=explode("~",$_POST["mascotas"]);
$clase=$valor[0];
$clase2=$valor[1];

EDIT 2

I leave a link with an example of what you are doing, you can inspect the element and verify that the id and values are what you want.

Example

    
answered by 21.10.2016 / 15:37
source
1

Even if you do not use POST or GET to pass the data from one php file to another, you will have to use a form for the user to collect the data equally, but if you still do not want to use any of those forms to pass it to another file, it is best to use sessions.

In your code you see elements that have to be inside a form, but you can not see the declaration of any form

<form action="" method="GET">
    <!-- aquí los elementos del form -->
</form>

Once you have built your form well, you only have to validate it and process it.

if(isset($_POST["send"])) {

    if(isset($_POST["paciente"]) && isset($_POST["mascotas"])) {

        session_start();
        $_SESSION["paciente"] = $_POST["paciente"];
        $_SESSION["mascotas"] = $_POST["mascotas"];
    }

}

Then to access the data from any other page you just have to reuse session_start() on each page where you want to access the session variables

session_start();

and you can now access your session variables $_SESSION["paciente"] and $_SESSION["mascotas"]

    
answered by 21.10.2016 в 07:46