Modify session variable in onclick

1

I have 2 images, the idea is that when I click on one of them, I save the associated category in a session variable. I searched for information and tried with ajax, but mysteriously it returns ... code ... not the variable, that is ... something is wrong. The code : File manage.php

        function modificarValor(valor){
           var url = 'b_categoria.php';
           $.ajax({
              type: 'POST',
              url: url,
              data: {valor},
              success:function(data){
              console.log("Valor Variable : "+ data);
             }
           });
          } 

        <div class="marco_central2">
          <div style="float:left;padding:10px;padding-top:184px;">
              <a href="/ACCE/adelante"  class="reload_link" onclick="modificarValor(1)">
              <?php echo $this->Html->image('menu/HIT-CATEGORIA-2.png'); ?></a>
          </div>
          <div style="float:right;padding:10px;padding-top:184px;">
              <a href="/ELEC/adelante"  class="reload_link" onclick="modificarValor(2)">
              <?php echo $this->Html->image('menu/HIT-CATEGORIA-1.png'); ?></a>
          </div>
        </div>

b_categoria.php file

<?php
$valor = $_POST['valor'];
session_start();

switch ($valor) {
    case 1:
        $_SESSION['categoria_actual'] = "accesorios";
        echo 'Accesorios';
        break;
    case 2:
        $_SESSION['categoria_actual'] = "servicios";
        echo 'Servicios';
    break;
}

?>

I clarify that it is an application made in cakephp 1.3 very old, that developed in the place where I work .. and there are some php encrypted, the developers disappeared years ago. I will appreciate any help!

    
asked by look68 24.01.2017 в 16:30
source

1 answer

1

As for the server, if the request returns the same html of the page it is because it is not entering any of the cases of your switch-case . It may be because the value contained in your variable $valor is being treated as a string instead of integer which is what you are trying to compare in switch .

Do a var_dump($valor) and make sure what your variable contains and what type it is before making any further comparison. If you only need to work with whole numbers, try parse the value to whole number to proceed if necessary.

Finally, add a default to your switch-case to send a default response if none of the values match your comparison. Our job as programmers also consists of covering as many cases as possible to prevent unwanted behavior.

Regarding the client; add the error callback to your ajax request because not everything is always pink:

success: function() { ... },
error: function(err) { console.error(err) } 

Check well how you are sending the data of the request, if you look closely at the function that contains the ajax, in questions of ecmascript5, you are passing an erroneous value to the parameter data ; data: {valor}

Use JSON as standard in each request you make, both on the server side and the client, add the parameters contentType: 'application/json' and dataType: 'json' to make sure of it.

Check from your browser the status of the request when it comes and goes, see how the data is being sent and what answer you are receiving from the server.

It's a question of debugging and analyzing, sooner or later you find the joke.

    
answered by 26.01.2017 / 04:37
source