2017-04-04 90 views
1

我想使用php來設置初始loginPage.php會話變量,並顯示在paymentPage.php,但我有麻煩設置$ _SESSION變量,以便它出現。在loginPage和付款頁面之間有一個網頁,這似乎讓我失望了。設置會話變量和從其他.php文件計算

此外,我需要計算上一頁中所選項目的總數,但我不確定如何將一個美元金額與不顯示但是計算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> 

回答

1

看我真的通過您的代碼看,我不知道是否有什麼東西失蹤,但我並沒有什麼太大的意義對我來說。無論如何,如果它適合你,這很好。

這裏有一個錯字,我發現:

paymentPage.php

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

而且爲你計算..我不知道的人。你讓dom有點靜態以獲得這些價值的foreach產品或任何東西。你可以做的最好的事情是給每個元素一個獨特的Id oder類,並通過JS驗證它是否得到檢查。對你來說還有很長的路要走。但是請記住,我們不寫你的代碼。

+0

我很欣賞你的誠實,我很新,這是我從來沒有與HTML或PHP工作過。我會採納你的建議,看看我能想出什麼!我並不希望你給我代碼,只是指出我需要改進的地方以及你對我的問題的看法。謝謝! – pCaMp

1

你忘了問一個問題並解釋你的問題。

訪問會話的任何頁面必須(1)以包含session_start()的PHP塊開始。您向我們展示的4個腳本中只有一個會執行此操作。

1)並不完全正確 - 但例外的討論太複雜,進入這裏

+0

好的,每個頁面都需要包含session_start()以便將變量傳遞給其他頁面?我會努力解決我的問題。我想我的問題是1)爲什麼我的變量沒有被傳遞給其他頁面,2)以word的形式,我應該如何去從EquineShop.php頁面獲取paymentPage.php的總金額。不尋找代碼,但我似乎無法繞過它。感謝您的幫助! – pCaMp

0

我發現的另一個問題是你的三江源頁:

<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>