当用户填写字段时,PHP刷新到空白屏幕吗? [关闭]

问题描述 投票:0回答:1

问题1)我尝试加载此页面,它最初可以正常工作。我添加了一些内容并开始游戏,现在,当我输入数字并点击Submit时,页面变白并且什么也没有发生。我不知道我错过了什么,我已经检查了8次]

问题2)如果我在数字字段中输入字母,我如何使网站说“只接受数字”,而不是仅仅破坏或给出一些随机数字?

问题3)我目前正在使用Atom,是否可以通过Dreamweaver为我检查PHP?我已经使用了验证w3,但不确定它是否确实有效?

谢谢!

          <?php
                  if (isset($_POST['submit'])) {
                    switch ($_POST['convert']) {

                        case "ft-i":
                            $result = feet_to_inch($_POST['value']);
                            $old_unit = 'feet';
                            $new_unit = 'inches';
                            break;
                        case "i-ft":
                            $result = inch_to_feet($_POST['value']);
                            $old_unit = 'inches';
                            $new_unit = 'feet';
                            break;
                        case "in-cm":
                            $result = inches_to_centimeter($_POST['value']);
                            $old_unit = 'inches';
                            $new_unit = 'centimeter';
                            break;
                        case "cm-in":
                            $result = centimeter_to_inches($_POST['value']);
                            $old_unit = 'centimeter';
                            $new_unit = 'inches';
                            break;
                        case "f-c":
                            $result = fahrenheit_to_celsius($_POST['value']);
                            $old_unit = ' Farenheit';
                            $new_unit = ' Celcius';
                            break;
                        case "c-f":
                            $result = celsius_to_fahrenheit($_POST['value']);
                            $old_unit = ' Celcius';
                            $new_unit = ' Farenheit';
                            break;
                        case "m-cm":
                            $result = meter_to_centimeter($_POST['value']);
                            $old_unit = 'meter';
                            $new_unit = 'centimeter';
                            break;
                        case "cm-m":
                            $result = centimeter_to_meter($_POST['value']);
                            $old_unit = 'centimeter';
                            $new_unit = 'meter';
                            break;
                        case "cm-mm":
                            $result = centimeter_to_millimeter($_POST['value']);
                            $old_unit = 'centimeter';
                            $new_unit = 'millimeter';
                            break;
                        case "mm-cm":
                            $result = millimeter_to_centimeter($_POST['value']);
                            $old_unit = 'millimeter';
                            $new_unit = 'centimeter';
                            break;
                        case "wh-j":
                            $result = watthour_to_joule($_POST['value']);
                            $old_unit = 'watthour';
                            $new_unit = 'joule';
                            break;
                        case "j-wh":
                            $result = joule_to_watthour($_POST['value']);
                            $old_unit = 'joule';
                            $new_unit = 'watthour';
                            break;
                  }

                  function feet_to_inch($given_value) {
                    return $given_value*12;
                  }
                  function inch_to_feet($given_value) {
                    return $given_value/12;
                  }

                  function inches_to_centimeter($given_value) {
                    return $given_value/2.54;
                  }

                  function centimeter_to_inches($given_value) {
                    return $given_value*2.54;
                  }

                  function meter_to_centimeter($given_value) {
                    return $given_value*100;
                  }

                  function centimeter_to_meter($given_value) {
                    return $given_value/100;
                  }

                  function centimeter_to_millimeter($given_value) {
                    return $given_value*10;
                  }

                  function millimeter_to_centimeter($given_value) {
                    return $given_value/10;
                  }

                  function fahrenheit_to_celsius($given_value) {
                    return 5/9*($given_value-32);
                  }

                  function celsius_to_fahrenheit($given_value) {
                    return $given_value*9/5+32;
                  }

                  function watthour_to_joule($given_value) {
                    return $given_value*3600;
                  }

                  function joule_to_watthour($given_value) {
                    return $given_value/3600;
                  }
          }
          ?>


  <!DOCTYPE html>
   <html lang="en" dir="ltr">
    <head>
     <meta charset="utf-8">

    <title>
        FOLIO 2
    </title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link href="stylesheet.css" type="text/css" rel="stylesheet">
    </head>

  <body>
    <div class="sidenav">
        <a href="index.php">Home</a>
        <a href="folio1.php">Folio 1</a>
        <a href="folio2.php">Folio 2</a>
        <a href="folio3.php">Folio 3</a>
        <a href="folio4.php">Folio 4</a>
        <a href="folio5.php">Folio 5</a>
    </div>

    <div class="main">
      <center>
        <h1>Folio 2</h1>
          <br>
                <h5>The user can enter values to be used in common conversions.
                  These may include temperature, distance, speed, etc.
                  They will input any numeric values they like and can see the result.
                </h5>

                <form method="post">
                            Please enter a number <input type="text" name="value" />
                            <br><br><br>

                            <select name="convert">
                                <option value="">Please select a conversion</option>

                                <optgroup label="Imperial length">
                                  <option value="ft-i">Feet to Inches</option>
                                  <option value="i-ft">Inches to Feet</option>
                                </optgroup>

                                <optgroup label="Metric & Imperial lengths">
                                    <option value="in-cm">Inches to Centimetre</option>
                                    <option value="cm-in">Centimetre to Inches</option>
                                </optgroup>

                                <optgroup label="Metric length">
                                    <option value="m-cm">Metre to Centimetre</option>
                                    <option value="cm-m">Centimetre to Metre</option>

                                    <option value="cm-mm">Centimetre to Millimetre</option>
                                    <option value="mm-cm">Millimeter to Centimetre</option>
                                </optgroup>

                                <optgroup label="Units of Temperature">
                                    <option value="f-c">Farenheit to Celcius</option>
                                    <option value="c-f">Celcius to Farenheit</option>
                                </optgroup>

                                <optgroup label="Units of Energy">
                                    <option value="wh-j">Watt-Hours to Joules</option>
                                    <option value="j-wh">Joules to Watt-Hours</option>
                                </optgroup>

                            </select>

                            <br><br><br>

                            <input type="submit" name="submit" value="Convert" />
                        </form>

                <?php
                  if (!empty($result))
                  echo '<p>'.$_POST['value'].$old_unit.' equals '.$result.$new_unit.'</p>';
                ?>

          <br><br>
          <a href="https://docs.google.com>
          See planning for Folio 2
          </a>
    </div>
  </body>
</html>
php css atom-editor mamp dreamweaver
1个回答
0
投票

您的代码看起来不错。尝试使用此代码将所有功能写在php标记内,但在主if块。您也可以尝试使用<input type="number">验证数字,否则可以尝试使用javascript。

<?php
if (isset($_POST['submit']))
{

    switch ($_POST['convert'])
    {

        case "ft-i":
            $result = feet_to_inch($_POST['value']);
            //$result = 12;
            $old_unit = 'feet';
            $new_unit = 'inches';
            echo $result;
        break;
        case "i-ft":
            $result = inch_to_feet($_POST['value']);
            $old_unit = 'inches';
            $new_unit = 'feet';
        break;
        case "in-cm":
            $result = inches_to_centimeter($_POST['value']);
            $old_unit = 'inches';
            $new_unit = 'centimeter';
        break;
        case "cm-in":
            $result = centimeter_to_inches($_POST['value']);
            $old_unit = 'centimeter';
            $new_unit = 'inches';
        break;
        case "f-c":
            $result = fahrenheit_to_celsius($_POST['value']);
            $old_unit = ' Farenheit';
            $new_unit = ' Celcius';
        break;
        case "c-f":
            $result = celsius_to_fahrenheit($_POST['value']);
            $old_unit = ' Celcius';
            $new_unit = ' Farenheit';
        break;
        case "m-cm":
            $result = meter_to_centimeter($_POST['value']);
            $old_unit = 'meter';
            $new_unit = 'centimeter';
        break;
        case "cm-m":
            $result = centimeter_to_meter($_POST['value']);
            $old_unit = 'centimeter';
            $new_unit = 'meter';
        break;
        case "cm-mm":
            $result = centimeter_to_millimeter($_POST['value']);
            $old_unit = 'centimeter';
            $new_unit = 'millimeter';
        break;
        case "mm-cm":
            $result = millimeter_to_centimeter($_POST['value']);
            $old_unit = 'millimeter';
            $new_unit = 'centimeter';
        break;
        case "wh-j":
            $result = watthour_to_joule($_POST['value']);
            $old_unit = 'watthour';
            $new_unit = 'joule';
        break;
        case "j-wh":
            $result = joule_to_watthour($_POST['value']);
            $old_unit = 'joule';
            $new_unit = 'watthour';
        break;
    }

}
function feet_to_inch($given_value)
{
    return $given_value / 12;
}
function inch_to_feet($given_value)
{
    return $given_value / 12;
}

function inches_to_centimeter($given_value)
{
    return $given_value / 2.54;
}

function centimeter_to_inches($given_value)
{
    return $given_value * 2.54;
}

function meter_to_centimeter($given_value)
{
    return $given_value * 100;
}

function centimeter_to_meter($given_value)
{
    return $given_value / 100;
}

function centimeter_to_millimeter($given_value)
{
    return $given_value * 10;
}

function millimeter_to_centimeter($given_value)
{
    return $given_value / 10;
}

function fahrenheit_to_celsius($given_value)
{
    return 5 / 9 * ($given_value - 32);
}

function celsius_to_fahrenheit($given_value)
{
    return $given_value * 9 / 5 + 32;
}

function watthour_to_joule($given_value)
{
    return $given_value * 3600;
}

function joule_to_watthour($given_value)
{
    return $given_value / 3600;
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.