Conditional on PHP

3

I have the following code in HTML and PHP. I can not find the error in the PHP code, because it does not read the expressions and does not throw the result at me. I'm really new to programming.

  $( document ).ready(function() 
                      {
  $("#boton_calcular").click(function()
                             {
    $.post("Rumbo_php.php",
           {
      azm: $("#azm1").val(),
    },
           function(data, status){
      console.log("Datos recibidos: " + data + "\nStatus: " + status);
      if(status=='success')
      {

        $("#caja_resultado").val( data );
      }
    });

  });

});
<?php 

    //Capturo parametros 
    $azm1 = $_POST['azm1'];
	
    //Realizo funcion para el paso de dms a deg
	
    function rm(azm)
    {
        	
        $exp = $azm>=0 && $azm<=90;
        $exp1 = $azm>90 && $azm<=180;
        $exp2 = $azm>180 && $azm<=270;
        $exp3 = $azm>270 && $azm<=360;
        		
        if (eval($exp))
        {
        	echo("Rumbo Calculado es: N"+$azm+"E");
        } else if (eval($exp1)){
        	echo("Rumbo Calculado es: S"+(180-$azm)+" E");
        } else if (eval($exp2)){
        	echo("Rumbo Calculado es: S"+($azm-180)+"W");    
        } else if (eval($exp3)){
        	echo("Rumbo Calculado es: N"+(360-$azm)+"W");    
        } else { 
        	echo("No es un angulo valido");
        } 
        return $resultado;
    }

    //Calculo las coordenadas de el punto

    $rum = rm($azm1);

    echo $rum;

?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="jquery-1.12.3.min.js"></script>
   </head>
<body>
<input type="text" id="azm1" value="3"> </input>Rumbo
<br><br>
<button id="boton_calcular">Calcular Rumbo</button> 
<input type="text" id="caja_resultado"> </input>
    
asked by JS28 28.09.2016 в 02:36
source

2 answers

3

In your PHP you have:

$azm1 = $_POST['azm1'];

And in the JavaScript

$.post("Rumbo_php.php",
        {
            azm: $("#azm1").val(),              
        },

That azm: replace it with azm1: leaving as follows:

$.post("Rumbo_php.php",
        {
            azm1: $("#azm1").val(),             
        },

Replaces:

function rm(azm)

By:

function rm($azm)

Your function is as follows:

function rm($azm)
{
    $exp = ($azm>=0 && $azm<=90) ? true : false;
    $exp1 = ($azm>90 && $azm<=180) ? true : false;
    $exp2 = ($azm>180 && $azm<=270) ? true : false;
    $exp3 = ($azm>270 && $azm<=360) ? true : false; 
    $resultado = "";
    if ($exp)
    {
        $resultado = "Rumbo Calculado es: N". $azm ."E";
    } else if ($exp1){      
        $resultado = "Rumbo Calculado es: S".(180-$azm)." E";
    } else if ($exp2){
        $resultado = "Rumbo Calculado es: S".($azm-180)."W";    
    } else if ($exp3){
        $resultado = "Rumbo Calculado es: N".(360-$azm)."W";    
    } else { 
        $resultado = "No es un angulo valido";
    } 
return $resultado;
}
//Calculo las coordenadas de el punto   
$rum = rm($azm1);   
echo $rum;
?>

The problem I saw is that the string count in PHP you were doing with the sign + however it is done with the point .

On the other hand, when you define variables that will store boolean values (true / false) it is better to let them know, I made a "If short":

Variable = (condicion) ? Sí cumple : no cumple;

If I do it in a traditional way it would be:

Variable;
if (condicion) { 
//hacer algo si es verdad como asignar el valor a la variable:
Variable = algo si la condición es verdadera;
}else{ 
//hacer algo si la condición no se cumple, como asignarle otro valor a la variable:
Variable = algo si la condición es falsa;
} 

The use of eval in my opinion was being extra, so I removed it since the function was evaluating if the condition was true or false.

To return values of the functions, they must be used, the variable $resultado remained defined but blank.

You can see a small example running at: link // It will be working temporarily

just replace that 89 with the values you like.

By the way, since you want to learn PHP here I leave you some websites, I hope you find them useful:

  • link

  • link

  • link

  • link

  • link

  • For JavaScript and jQuery

  • link

  • link

  • link

  • link

  • link

  • answered by 28.09.2016 / 03:05
    source
    1

    You are missing a $ in the function parameter. For the eval problem, replace it by looking directly at the Boolean. It should look like this:

    function rm($azm)
    {
    
    $exp = $azm>=0 && $azm<=90;
    $exp1 = $azm>90 && $azm<=180;
    $exp2 = $azm>180 && $azm<=270;
    $exp3 = $azm>270 && $azm<=360;
    
       if ($exp)
    {
    echo("Rumbo Calculado es: N"+$azm+"E");
    } else if ($exp1){      
     echo("Rumbo Calculado es: S"+(180-$azm)+" E");
    } else if ($exp2){
     echo("Rumbo Calculado es: S"+($azm-180)+"W");    
    } else if ($exp3){
     echo("Rumbo Calculado es: N"+(360-$azm)+"W");    
    } else { 
     echo("No es un angulo valido");
    } 
        return $resultado;
    
    }
    

    As I said @fredyfx look at the javascript code and replace it with what he suggests

    Look at the web server error log. In Apache this appeared:

    PHP Notice:  Undefined variable: resultado in /home/roberto/public_html/Rumbo_php.php on line 29, referer: http://localhost/~roberto/sof.html
    

    What made it easier to find your problem

        
    answered by 28.09.2016 в 03:10