Transform letters into numbers with a Function

Asked

Viewed 1,261 times

0

I am developing a code that consists of taking a letter and transforming it into a corresponding number (Example A->1, B->2...), and this letter is received through a input text.

I found a topic that helped me in the conversion part of these values here: Function that enumerates letters of the alphabet

however, I need to know how to perform this calculation with the letters that the user type

For now my code is like this:

index php.

<html> 
<head> 
    <title> Leituras de Dados Excel </title>
</head>
    <body>
        <form action="excel.php" method="post">
            <div style="background-color:blue; height:130px; width:190px; position:absolute; top: 250px; left:600px;">
                <br>
                    <input id="1" name="coord1" type="text" class="input" style="position:relative; left:10; height:50px; width:50px; font-size:30px; text-transform: uppercase; text-align:center;" maxlength="1">
                    <input id="2" name="coord2" type="text" class="input" style="position:absolute; left:70; height:50px; width:50px; font-size:30px; text-transform: uppercase; text-align:center;"maxlength="1">
                    <input id="3" name="coord3" type="text" class="input" style="position:absolute; left:130; height:50px; width:50px; font-size:30px; text-transform: uppercase; text-align:center;"maxlength="1">
                <br>
                <br>
                <input type="submit" value="Converter" style="position:relative; left:55;">
            </div>
        </form>
    </body>
</html>

excel.php

<?php
    $coord1 = $_REQUEST['coord1'];
    $coord2 = $_REQUEST['coord2'];
    $coord3 = $_REQUEST['coord3'];

    $coordenadaFinal = $coord1."-".$coord2."-".$coord3;    
?>

<html>
<head><tile>Resultado</tile>
    <script type="text/javascript">
        function primeira(){
            function converte(letras) {
                var alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                var codigos = [];
                for (var i in letras) {
                    codigos.push(alfabeto.indexOf(letras[i].toUpperCase()) + 1);
                }
                return codigos;
            }

            resultado = converte("l");
                for (var i in resultado) {
                    alert(resultado);

                }
        } // fim da function geral 

        function segunda(){
                    function converte(letras) {
                        var alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        var codigos = [];
                        for (var i in letras) {
                            codigos.push(alfabeto.indexOf(letras[i].toUpperCase()) + 27);
                        }
                        return codigos;
                    }

                    resultado = converte("l");
                        for (var i in resultado) {
                            alert(resultado);

                        }
                } // fim da function geral 

        function terceira(){
                    function converte(letras) {
                        var alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        var codigos = [];
                        for (var i in letras) {
                            codigos.push(alfabeto.indexOf(letras[i].toUpperCase()) + 703);
                        }
                        return codigos;
                    }

                    resultado = converte("o");
                        for (var i in resultado) {
                            alert(resultado);

                        }
                } // fim da function geral 

    </script>   
</head>
    <body>
        <div id="resposta" style="background-color:blue; height:130px; width:190px; position:absolute; top: 250px; left:600px; font-size:30px; color:white;"> 
            <p>Você inseriu:</p>
            <p><?php echo strtoupper($coordenadaFinal) ; ?></p>
            <input type="button" value="Voltar" onclick="javascript: location.href='index.php';" >
            <input type="button" value="Mostrar valor 1" onclick="primeira()" >
            <input type="button" value="Mostrar valor 2" onclick="segunda()" >
            <input type="button" value="Mostrar valor 3" onclick="terceira()" >

        </div>
    </body>

</html>

Everything is working properly, it receives the variables that at first I should treat, and performs the function with the values I have established correctly within resultado = converte("alguma letra");, however I am unable to pass to the value of Function the letter the user has typed and which is stored in cood1, coord2 and coord3.

I’ve tried a few things like: result = convert("coord1"); result = convert(coord1); result = convert($_REQUEST['coord1']);

But I was unsuccessful in any of them... Thank you in advance :)

  • Try it this way resultado = converte("<?php echo $coord1;?>");

  • 1

    Worked perfectly - Thanks

No answers

Browser other questions tagged

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