dst2017 2014-02-18 19:57
浏览 53
已采纳

if-else语句不运行

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.

  • 写回答

1条回答 默认 最新

  • dpkt17803 2014-02-18 20:35
    关注

    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…

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程