user3320983
user3320983

Reputation: 3

if-else statement do not run

I hope you could help me with my problem. My script for validating form (checks if the text-box is not empty and if is it a number; note: in my region we use in decimal number comma not point, PHP works only with decimal point) do not work properly. HTML:

...
<form action="http://localhost/kalkulacka.php" method="post" name="formular">
  <table>
    <tr>
      <td>1 dávka(g):</td>
      <td><input type="text" name="davka" maxlength="7" size="7"></td>
    </tr>
    <tr>
      <td>Bielkoviny v 1 dávke(g):</td>
      <td><input type="text" name="bielkoviny" maxlength="7" size="7"></td>
    </tr>
    <tr>
      <td>Sacharidy v 1 dávke(g):</td>
      <td><input type="text" name="sacharidy" maxlength="7" size="7"></td>
    </tr>
    <tr>
      <td>Balenie(g):</td>
      <td><input type="text" name="balenie" maxlength="7" size="7"></td>
    <tr>
      <td>Cena za balenie(€):</td>
      <td><input type="text" name="cena" maxlength="7" size="7"></td>
    </tr>
    <tr>
      <td><input type="submit" value="Vypočítaj"></td>
    </tr>
  </table>
</form>

php:

<?php

$davka=$_POST['davka'];
$bielkoviny=$_POST['bielkoviny'];
$sacharidy=$_POST['sacharidy'];
$balenie=$_POST['balenie'];
$cena=$_POST['cena'];
$is_valid=true;

function vypocitaj($serving, $proteins, $pack, $price)
{
    $proteins_percentage=($proteins/$serving)*100;
    $price_per_gram=$price/($pack/100*$proteins_percentage);
    printf("1 gram bielkoviny stoji: %.3f", $price_per_gram);
}

if (empty($davka)||empty($bielkoviny)||empty($sacharidy)||empty($balenie)||empty($cena))
{
    echo "Some boxes are empty!";
    $is_valid=false;
}
else /*when I remove this whole "else" block, script at least verifies if are boxes empty by if statement above, but when I add this block, it not even verifies if are boxes empty*/
{
    if (!is_numeric($davka))
    {
        $davka=str_replace(",", ".", $davka);
        if (!is_numeric($davka)
        {
            $is_valid=false;
        }
    }
    if ($is_valid==true && !is_numeric($bielkoviny))
    {
        $bielkoviny=str_replace(",", ".", $bielkoviny);
        if (!is_numeric($bielkoviny)
        {
            $is_valid=false;
        }
    }
    if ($is_valid==true && !is_numeric($sacharidy))
    { 
        $sacharidy=str_replace(",", ".", $sacharidy);
        if (!is_numeric($sacharidy)
        {
            $is_valid=false;
        }
    }
    if ($is_valid==true && !is_numeric($balenie))
    {
        $balenie=str_replace(",", ".", $balenie);
        if (!is_numeric($balenie)
        {
            $is_valid=false;
        }
    }
    if ($is_valid==true && !is_numeric($cena))
    {
        $cena=str_replace(",", ".", $cena);
        if (!is_numeric($cena)
        {
            $is_valid=false;
        }
    }
    if ($is_valid)
    {
        vypocitaj($davka, $bielkoviny, $balenie, $cena);
    }
    else
    {
        echo "Only number are accepted!";
    }
}
?>

The same script written in JavaScript's syntax works properly in JavaScript, but in PHP won't work. When I removed the main "else" statement the main "if" statement worked, but when I added it, the whole code did not work. When I tried each function separately - they worked.

I'm stuck for several days in this and I can't move. I would like to know the reason why it is not work properly. I appreciate any advice. Thanks ;) Sorry for my English.

Upvotes: 0

Views: 127

Answers (1)

feeela
feeela

Reputation: 29932

I just pasted that code to my IDE it told me that there is a missing closing brace:

if (!is_numeric($davka))
{
    $davka=str_replace(",", ".", $davka);
    if (!is_numeric($davka)
                        // ^ missing brace here
    {
        $is_valid=false;
    }
}

…it should be:

if (!is_numeric($davka))
{
    $davka=str_replace(",", ".", $davka);
    if (!is_numeric($davka))
    {
        $is_valid=false;
    }
}

If that solves the problem, please use an editor with syntax highlighting…

Upvotes: 2

Related Questions