提问者:小点点

为什么我的会话变量不相乘?


这是我的第一个PHP项目,html工作得很好。 然而,我在PHP中的变量似乎不起作用,因为第二个页面显示的是完全的胡言乱语。 第一页大部分用于填写信息,我假设这正在工作,第二部分显示信息,只是使用PHP。 但我觉得我的变数不起作用了。 也许是我搞砸了什么代码? 有人知道怎么了吗?

page1.php(填写信息的地方)

<?php
session_start();
?>
<html>
<head>
<title>Currency Calculator</title>
<style>
input[type=number] {
  -moz-appearance: textfield;
}

body {
    background-color: lightblue;
}

p {
  font-family:Arial;
}

a.button {
    -webkit-appearance: button;
    -moz-appearance: button;
    appearance: button;
    padding: 10px 24px;
    text-decoration: Arial;
    color: black;
    background-color: lightgreen;
}

a.button:hover {
    box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}

</style>
</head>
<body>

<h1> Convert currency to EUR </h1>

<form action="page2.php" method="get">
<p> Amount in euro: </p> 
<input type="number" name="amount" min="0" step=".01">
<br> <br>
<p> To which currency should it be converted? </p>
<select name="currency" id="currency">
<option value="0" selected>Select a currency</option>
  <option value="1.1011">US Dollar</option>
  <option value="1,5160">Canadian Dollar</option>
  <option value="0.8980">British Pound</option>
  <option value="10.5623">Swedish Crown</option>
  <option value="7.4551">Danish Crown</option>
  <option value="75.2170">Argentinian Peso</option>
  <option value="1.0670">Swiss Frank</option>
  <option value="1.6640">Australian Dollar</option>
  <option value="7.8811">Chinese Yuan</option>
  <option value="7.4892">Turkish Lira</option>
</select> 
<br> <br> <br>
<a href="page2.php" class="button">Convert!</a>
</form>

<?php

$_GET['amount'] = $am;

$_SESSION['x'] = $am;

$_GET['currency'] = $cur;

$_SESSION['y'] = $cur;

?>  

</body>
</html> 

page2.php(其中显示了计算结果)

<?php
session_start();
?>
<html>
<head>
<style>
body {
    background-color: lightblue;
}

p {
  font-family:Arial;
}
</style>
</head>
 <body>

 <?php

 $_SESSION['x'] * $_SESSION['y'] = $money;

 echo . $_SESSION['x'] . "<p> of your selected currency is equal to </p>" . $money . "<p>. </p><br>";
 echo "<p>1 EUR is equal to </p>" . $_SESSION['y'] . "<p> of your selected currency.</p>";

 ?>


 </body>
</html>

提前谢谢你!


共1个答案

匿名用户

你的代码中有很多逻辑和语法错误。 我建议您重读这篇文章,您正在使用的教程来学习编程的基础知识,并正确地理解它们(首先是函数,任务等)

page1.php

   <?php
    session_start();
    ?>
    <html>
    <head>
    <title>Currency Calculator</title>
    <style>
    input[type=number] {
      -moz-appearance: textfield;
    }

    body {
        background-color: lightblue;
    }

    p {
      font-family:Arial;
    }

    a.button {
        -webkit-appearance: button;
        -moz-appearance: button;
        appearance: button;
        padding: 10px 24px;
        text-decoration: Arial;
        color: black;
        background-color: lightgreen;
    }

    a.button:hover {
        box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
    }

    </style>
    </head>
    <body>

    <h1> Convert currency to EUR </h1>

    <form action="page2.php" method="get">
    <p> Amount in euro: </p> 
    <input type="number" name="amount" min="0" step=".01">
    <br> <br>
    <p> To which currency should it be converted? </p>
    <select name="currency" id="currency">
    <option value="0" selected>Select a currency</option>
      <option value="1.1011">US Dollar</option>
      <option value="1,5160">Canadian Dollar</option>
      <option value="0.8980">British Pound</option>
      <option value="10.5623">Swedish Crown</option>
      <option value="7.4551">Danish Crown</option>
      <option value="75.2170">Argentinian Peso</option>
      <option value="1.0670">Swiss Frank</option>
      <option value="1.6640">Australian Dollar</option>
      <option value="7.8811">Chinese Yuan</option>
      <option value="7.4892">Turkish Lira</option>
    </select> 
    <br> <br> <br>
    <button type="submit" class="button">Convert!</a>
    </form> 

    </body>
    </html> 

page2.php

<?php
session_start();
?>
<html>
<head>
<style>
body {
    background-color: lightblue;
}

p {
  font-family:Arial;
}
</style>
</head>
 <body>

 <?php

 $money =  $_GET['amount'] * $_GET['currency'] ;

 echo   $_GET['amount'] . "<p> of your selected currency is equal to </p>" . $money . "<p>. </p><br>";
 echo "<p>1 EUR is equal to </p>" .$_GET['currency'] . "<p> of your selected currency.</p>";

 ?>


 </body>
</html>

以上修正后的代码应该可以工作。

相关问题