【问题标题】:Setting Session Variables and Calculating from other .php files设置会话变量并从其他 .php 文件计算
【发布时间】:2017-08-29 00:42:59
【问题描述】:

我正在尝试使用 php 在初始 loginPage.php 上设置会话变量并显示在 paymentPage.php 上,但是我在设置 $_SESSION 变量以使其显示时遇到了麻烦。 loginPage 和 paymentPage 之间有一个网页,这似乎让我感到困惑。

另外,我需要计算上一页上选定项目的总数,但我不确定如何将美元金额与未显示但在支付页面上计算总数的复选框放在一起。

loginPage.php

<?php

    session_start();

?>

<html>

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <title>Form Login</title>

</head>



<body OnLoad="document.main.username.focus();">

  <table>

    <tr>

      <td colspan="2">

        <h2>Welcome to the Equine Shop!</h2>

        <h4>Please enter a personalized Username, Email Address and Password to continue to our online store</h4>

      </td>

    </tr>

    <!-- create the main form with an input text box named uid and a password text box named mypassword -->

    <form name="main" method="post" action="EquineShop.php">

      <form action="paymentPage.php" method=post>

        <tr>

          <td>Username:</td>

          <td><input type="text" name="username" type="text" size="50"></td>

        </tr>

        <tr>

          <td>Email Address:</td>

          <td><input type="text" name="emailadd" type="text" size="50"></td>

        </tr>

        <tr>

          <td>Password:</td>

          <td><input type="password" name="pass" type="text" size="50"></td>

        </tr>

        <tr>

          <td colspan="2" align="center"><input name="btnsubmit" type="submit" value="Submit"></td>

        </tr>

  </table>

  </form>

  </form>

</body>

</html>

EquineShop.php

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Equine Shop Home Page </title>
</head>

<?php
    if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
        session_unset();
        session_destroy();
    }
    $_SESSION['LAST_ACTIVITY'] = time();
    ?>

  <body OnLoad="document.main.username.focus();">

    <form name="main" method="post" action="paymentPage.php">

      <h1 style="text-align:center;">Welcome to the Equine Shop!</h1>
      <table border="5">
        <tr>
          <td>
            <p style="font-size:24px;">Halters</p>
            <p>$25.00</p>
            <p>Built to last, these halters are made of premium quality</p>
            <p><a href="http://localhost/week2/halters.html">Click for more details!</a></p>
            <p><img src="https://www.valleyvet.com/swatches/35434_S_001_vvs.jpg" width="300" height="250" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Saddle Pads</p>
            <p>$37.00</p>
            <p>You'll love this all purpose saddle pad!</p>
            <p><a href="http://localhost/week2/saddlepad.html">Click for more details!</a></p>
            <p><img src="https://img.smartpak.com/images/product/20333_Navy.jpg" width="300" height="250" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Girths</p>
            <p>$200.00</p>
            <p>This leather english girth is made of quality leather.</p>
            <p><a href="http://localhost/week2/girths.html">Click for more details!</a></p>
            <p><img src="https://img.smartpak.com/images/product/highres/14428_Chocolate_Top_silo.jpg?width=460&quality=100" width="300" height="250" /></p>
          </td>

          <td>
            <p style="font-size:24px;">English Saddles</p>
            <p>$2,200.00</p>
            <p>Soft and comfortable padding with adjustable tree.</p>
            <p><a href="http://localhost/week2/englishsaddles.html">Click for more details!</a></p>
            <p><img src="https://s-media-cache-ak0.pinimg.com/736x/9a/82/6d/9a826d879d8fbea19fe167f267b0d61f.jpg" width="300" height="300" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Bridles</p>
            <p>$150.00</p>
            <p>This plain raised flash bridles delivers complete comfort.</p>
            <p><a href="http://localhost/week2/bridles.html">Click for more details!</a></p>
            <p><img src="https://www.equestrianweb.com/media/catalog/product/cache/1/image/650x650/7d70730b2ff80c936b5c98bf27fe5aba/e/c/ec18-geral.jpg" width="300" height="300" /></p>
          </td>
        </tr>

        <tr>
          <td>
            <p style="font-size:24px;">Grooming Materials</p>
            <p>$85.00</p>
            <p>The complete grooming kit!</p>
            <p><a href="http://localhost/week2/grooming.html">Click for more details!</a></p>
            <p><img src="https://s-media-cache-ak0.pinimg.com/236x/57/db/22/57db225ed12aa5c8009c4861ad6873cb.jpg" width="300" height="300" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Blankets</p>
            <p>$150.00</p>
            <p>1680 denier, turnout, waterproof blanket.</p>
            <p><a href="http://localhost/week2/blankets.html">Click for more details!</a></p>
            <p><img src="http://cdnll.doversaddlery.com/images/xl/0024378.jpg" width="300" height="300" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Sheets</p>
            <p>$124.00</p>
            <p>1200 denier, turnout waterproof sheet.</p>
            <p><a href="http://localhost/week2/sheets.html">Click for more details!</a></p>
            <p><img src="http://cdnll.doversaddlery.com/images/xl/0024280.jpg" width="300" height="300" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Fly Masks</p>
            <p>$30.00</p>
            <p>Keep flys off your horses face this summer!</p>
            <p><a href="http://localhost/week2/flymasks.html">Click for more details!</a></p>
            <p><img src="https://img.smartpak.com/images/product/highres/21540_Standard_WEars.jpg?width=460" width="300" height="300" /></p>
          </td>

          <td>
            <p style="font-size:24px;">First Aid</p>
            <p>$100.00</p>
            <p>The complete first aid kit for your horse</p>
            <p><a href="http://localhost/week2/firstaid.html">Click for more details!</a></p>
            <p><img src="http://cdnll.doversaddlery.com/images/xl/0011304.jpg" width="300" height="300" /></p>
          </td>
        </tr>

        <tr>
          <td>
            <p style="font-size:24px;">Spurs</p>
            <p>$45.00</p>
            <p>These soft touch spurs are great for sensitive horses</p>
            <p><a href="http://localhost/week2/spurs.html">Click for more details!</a></p>
            <p><img src="https://img.smartpak.com/images/product/300x300/19089.jpg?width=224&quality=100" width="300" height="300" /></p>
          </td>

          <td>
            <p style="font-size:24px;">Whips</p>
            <p>$50.00</p>
            <p>Dressage whips for optimum riding</p>
            <p><a href="http://localhost/week2/whips.html">Click for more details!</a></p>
            <p><img src="https://ii.bigdweb.com/fcgi-bin/iipsrv.fcgi?FIF=/images/bigdweb/source/3011f_c1105.tif&qlt=75&wid=225&cvt=jpeg" width="300" height="300" /></p>
          </td>
        </tr>
      </table>


      <p style="font-size:20px;"><b> Order Form</b></p>
      First Name:<br>
      <input type="text" name="firstname"><br> Last Name:<br>
      <input type="text" name="lastname"><br> Street Address:<br>
      <input type="text" name="street"><br> City:
      <br>
      <input type="text" name="city"><br> State:
      <br>
      <input type="text" name="state"><br> Zip:
      <br>
      <input type="text" name="zip"><br>
      <br>

      <table>
        Product:
        <tr>
          <td><input type="checkbox" name="tack[]" value="Halter">Halter</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="SaddlePad">Saddle Pad</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="Girth">Girth</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="Saddle">English Saddle</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="Bridle">Bridle</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="Grooming">Grooming Material </td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="Blanket">Blanket</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="FirstAid">First Aid</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="spur">Spur</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>

        <tr>
          <td><input type="checkbox" name="tack[]" value="Whip">Whip</td>
          <td>Quantity: <select name="quantity">
                            <option>0</option>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select></td>
        </tr>
      </table>

      <br><input type="submit" value="Proceed to Checkout" />

    </form>
  </body>

</html>

paymentPage.php

<html>

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <title>Equine Shop Payment Form</title>

</head>

<body>

    <?php 

        // Display in a table      

       echo "<h1> Equine Shop Payment Form </h1>";

       echo "<table border='1'>";

       echo "<tr>

             <th>User Name</th>

             <th>E-mail Address</th>

             </tr>";

       echo "<tr>

             <td>$_SESSION['username']</td>

             <td>$_SESSION['emailadd']</td>

             </tr>";

       echo "</table><br><br>";      

?>



    <?php

    $selected = $_POST['tack'];

        if(empty($selected)){

            echo("You didn't select any products!");

        } else {

            $checked = count($selected);

            echo("<b>Shopping Cart: </b><br><br>");

            for($i = 0; $i < $checked; $i++) {

                echo($selected[$i] . "<br>");



            }

        }

?>

      <br><br>

      <?php

    echo "<b>Total: </b>"

?>



        <form>

          <br><br>Credit Card Type:<br>

          <input type="radio" name="ccard" <?php if (isset($ccard) && $ccard=="visa" ) echo "checked";?> value="visa">Visa

          <br>

          <input type="radio" name="ccard" <?php if (isset($ccard) && $ccard=="master" ) echo "checked";?> value="master">MasterCard

          <br>

          <input type="radio" name="ccard" <?php if (isset($ccard) && $ccard=="discover" ) echo "checked";?> value="discover">Discover

          <br>

          <input type="radio" name="ccard" <?php if (isset($ccard) && $ccard=="amex" ) echo "checked";?> value="amex">American Express<br>

        </form>

        <br> Card Number:<br>

        <input type="text" name="card" maxlength="16">

        <br> Exp Date:<br>

        <input type="text" name="exp" maxlength="4">

        <br> Security Number:<br>

        <input type="text" name="secnum" maxlength="3">

        <br>



        <form action="">

          Comments:<br>

          <input type="text" name="comments" maxlength="300"><br>

        </form>



        <form name="main" method="post" action="thankYou.php">

          <input type="submit" value="Submit Order">

        </form>

</body>

</html>

thankYou.php

<html>

<head>
  <title> Thank You for your Purchase! </title>
</head>

<body>
  <?php
    echo "<b>Thank you for your Purchase!</b>"
?>
    <br>
    <?php 
    echo "Your order will be processed immediatley and arrive within 7 - 10 business days"
?>

    <br><br>
    <form name="main" method="post" action="loginPage.php">
      <input type="submit" value="Log Out" />
      <?php
        session_unset();
        session_destroy();
    ?>
    </form>
</body>

</html>

【问题讨论】:

    标签: php html session session-variables


    【解决方案1】:

    我发现的另一个问题是在您的感谢页面上:

    <form name="main" method="post" action="loginPage.php">
      <input type="submit" value="Log Out" />
      <?php
        session_unset();
        session_destroy();
    ?>
    </form>
    

    我认为您正在尝试做的事情:您正在尝试制作一个表单,当您单击提交按钮时会删除所有会话变量。

    你实际上在做什么:

    您正在呈现一个在提交时将发送一个 POST 到loginPage.php 的表单。当您在服务器上构建此表单时,您也已经取消设置 Session 变量。因为上面的代码等于我在下面展示给你的代码:

    <?php //deleting session state
        session_unset();
        session_destroy();
     //now lets output some html, being a form:
    ?>    
    <form name="main" method="post" action="loginPage.php">
      <input type="submit" value="Log Out" />
    </form>
    

    【讨论】:

      【解决方案2】:

      您忘记提出问题并解释您的问题。

      访问会话的任何页面必须(1) 以包含session_start() 的 PHP 块开头。您向我们展示的 4 个脚本中只有 1 个这样做。

      1) 严格来说并不正确 - 但对异常的讨论过于复杂,无法在此展开

      【讨论】:

      • 好的,所以每个页面都需要包含 session_start() 以便将变量传递给其他页面?我会努力纠正我的问题。我想我的问题是 1)为什么我的变量没有被传递到其他页面和 2)以 word 形式,我应该如何从 EquineShop.php 页面获取 paymentPage.php 的总美元金额。不是在寻找代码,但我似乎无法理解它。感谢您的帮助!
      【解决方案3】:

      看,我确实查看了您的代码,但我不知道是否缺少某些内容,但对我来说并没有太大意义。无论如何,如果它对你有用,那很好。

      这是我发现的一个错字:

      paymentPage.php

      echo "<tr>
              <td>" . $_SESSION['username'] . "</td>
              <td>" . $_SESSION['emailadd'] . "</td>
           </tr>";
      

      为了你的计算..我不认识人。您将 dom 设置为静态的,以便为每个产品或其他任何东西获取这些值。您可以做的最好的事情是给每个元素一个唯一的 Id oder 类,然后通过 JS 对其进行验证以查看它是否得到了检查。你还有很长的路要走。但请记住,我们不会编写你的代码。

      【讨论】:

      • 感谢您的诚实,我对此很陌生,因为我以前从未使用过 HTML 或 PHP。我会采纳你的建议,看看我能想出什么!我没想到你会给我代码,只是一些关于我需要改进的地方以及你对我的问题的看法。谢谢!
      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多