Problem with PHP numbers

Asked

Viewed 141 times

0

I have the following exercise in PHP:

The cost of a new car to the consumer is the sum of the factory cost with the percentage of distributor and taxes (applied at the cost of factory). Assuming the percentage of the distributor is 28% and the 45% tax, write an algorithm to read the factory cost of a car, calculate and write the final cost to the consumer.

When the value of the car is a number with no decimals it works correctly. But when I report a figure like: 58,000.20. He makes the following mistake:

Notice: A non well Formed Numeric value encountered in C: xampp htdocs PHP exercicio06 pageImposts.php on line 3

Here is the code: (are two files)

<!DOCTYPE HTML>
<html>
<head>
    <title>Valor do carro - Index</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <script>
        function imposto() {
            if(document.formCarro.valor.value === ""){
                alert('INFORME ALGUM VALOR!');
                document.formCarro.valor.value();
            }else{
                document.formCarro.action='./pageImpostos.php';
                document.formCarro.submit();
            }
        }
    </script>
</head>
<body>
    <form name="formCarro" method="post" action="">
        <table>
            <tr>
                <th colspan="2">CARRO</th>
            </tr>
            <tr>
                <td class="page-01">
                    <label for="valor">Valor do carro:</label><br>
                    <input type="text" name="valor" id="valor" size="22" maxlength="10" placeholder="EX: 00000,00">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="Calcular" name="btnCarro" id="btnCarro" onclick="imposto();">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

<?php
    $valor = $_REQUEST["valor"];
    $valor = $valor + ($valor * 0.28) + ($valor * 0.45);
?>



<!DOCTYPE HTML>
<html>
<head>
    <title>Valor do carro</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <script>
        function volta() {
            document.formCarro.action='./index.php';
            document.formCarro.submit();
        }
    </script>
</head>
<body>
<form name="formCarro" method="post" action="">
    <table>
        <tr>
            <th colspan="2">CARRO</th>
        </tr>
        <tr>
            <td class="page-01">
                Valor final: <br>
                <?php echo number_format($valor, '2', ',', '.') ?>
            </td>
        </tr>
        <tr>
            <td>
                <input type="button" value="Home" name="btnVolta" id="btnVolta" onclick="volta();">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

1 answer

0


Try to input 58000.20 instead of 58,000.20. I think it is necessary to validate the entry of this data because in PHP the floating points are in this format: 1.0 or 5.44 and not 4.2 or 5.6. Moreover one cannot have the point separating the groups of hundred, thousand, etc. Take a look at https://secure.php.net/manual/en/language.types.float.php

Browser other questions tagged

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