PHP通过href传递会话变量?

时间:2014-03-16 20:29:26

标签: php

首先,我只有6周的时间进入php和编程,所以请耐心等待。我正在创建一个从主页面开始的应用程序,列出价格的十个部分并显示它们。然后,您可以单击添加零件链接,将您带到一个新区域,在该区域中选择该零件的数量,然后将其放入购物车。购物车显示您的物品,数量,价格等。我无法弄清楚当点击getQuantity功能的链接时如何发送所选部分......它已经选择.......帮助和提前谢谢你!

<?php
session_start();


class Part {
    private $partId;
    private $partName;
    private $partPrice;

    public function __construct($partId, $partName, $partPrice) {
        $this->partId = $partId;
        $this->partName = $partName;
        $this->partPrice = $partPrice;
        }
    public function getId() {
        return $this->partId;
    }
    public function getName() {
        return $this->partName;
    }
    public function getPrice() {
        return $this->partPrice;
    }

}
$parts = array(
    1 => new Part (1, "Front Bumper", 199.99),
    2 => new Part (2, "Rear Bumper", 169.990),
    3 => new Part (3, "Front Struts", 219.99),
    4 => new Part (4, "Rear Struts", 219.99),
    5 => new Part (5, "Exhaust", 399.99),
    6 => new Part (6, "Headers", 299.99),
    7 => new Part (7, "Air Intake", 149.99),
    8 => new Part (8, "Turbo", 499.99),
    9 => new Part (9, "Supercharger", 429.99),
    10 => new Part (10, "Oil Pan", 79.99)
);

if (!isset($_SESSION["list"])) $_SESSION["list"] = array();

if (isset($_POST["selectNo"])) {
    displayCart();
    }
elseif (isset($_GET["action"]) and $_GET["action"] == "getQuantity") {
    getQuantity();
}
else {  
    displayList();
}

function displayList() {
    displayPageHeader();
    global $parts;
?>
    <h1 align="center">Parts List</h1>
    <dl align="center">
<?php foreach($parts as $part){ ?>
    <dt>******<?php echo $part->getName() ?>******<dt>
    <dd>$<?php echo number_format($part->getPrice(), 2) ?>
    <a href="midtermMain.php?action=getQuantity&partId=<?php echo $part->getId() ?>" style="text-decoration:none">&nbsp;&nbsp;Add Part To Shopping Cart<a/><br><br>
    </dd>
<?php 
} 
?>
    </dl>
    </body>
</html>
<?php
}

function displayPageHeader() {
?>
<html>
<head>
<style>
th, td {
    border:1px solid gray;
    height:20px;
    width:200px;
    text-align:center;
}
</style>
</head>
    <body>
    <hr>
    <h1 align="center">Welcome to Hayes' After-market Auto Parts Online Warehouse!</h1>
    <br>
    <hr>

<?php
}

function getQuantity() {
    displayPageHeader();
    global $parts;

?>
    <br>
    <p align="center">You have selected the            </p>
    <form align="center" action="midtermMain.php" method="post">
    <label for="quantity">Please select the Quantity you would like.</label>
    <input type="text" size="10" name="selectNo" id="selectNo" placeholder="quantity" value=""/><br>
    <input type="submit" name="submit" id="submit" value="Place in Cart"/>
    </form>
<?php
}

function displayCart() {
    displayPageHeader();
    global $parts;
?>

    <br>
    <table style="border:1px solid gray;border-collapse:collapse;">
        <tr><th>ITEM</th><th>QUANTITY</th><th>UNIT PRICE</th><th>TOTAL PRICE</th></tr>
        <tr><td> </td><td> </td><td> </td><td> </td></tr>
    </table><br><br>
    <caption>ADDRESS ENTRY</caption><br><br>
    <form action="midtermMain.php" method="post">
    <label for="street">STREET</label>
    <input type="text"  name="street" id="street" placeholder="street" value=""/><br>
    <label for="city">CITY</label>
    <input type="text"  name="city" id="city" placeholder="city" value=""/><br>
    <label for="state">STATE</label>
    <select name="state">
    <option value="AL">AL</option>
    <option value="AK">AK</option>
    <option value="AZ">AZ</option>
    <option value="AR">AR</option>
    <option value="CA">CA</option>
    <option value="CO">CO</option>
    <option value="CT">CT</option>
    <option value="DE">DE</option>
    <option value="DC">DC</option>
    <option value="FL">FL</option>
    <option value="GA">GA</option>
    <option value="HI">HI</option>
    <option value="ID">ID</option>
    <option value="IL">IL</option>
    <option value="IN">IN</option>
    <option value="IA">IA</option>
    <option value="KS">KS</option>
    <option value="KY">KY</option>
    <option value="LA">LA</option>
    <option value="ME">ME</option>
    <option value="MD">MD</option>
    <option value="MA">MA</option>
    <option value="MI">MI</option>
    <option value="MN">MN</option>
    <option value="MS">MS</option>
    <option value="MO">MO</option>
    <option value="MT">MT</option>
    <option value="NE">NE</option>
    <option value="NV">NV</option>
    <option value="NH">NH</option>
    <option value="NJ">NJ</option>
    <option value="NM">NM</option>
    <option value="NY">NY</option>
    <option value="NC">NC</option>
    <option value="ND">ND</option>
    <option value="OH">OH</option>
    <option value="OK">OK</option>
    <option value="OR">OR</option>
    <option value="PA">PA</option>
    <option value="RI">RI</option>
    <option value="SC">SC</option>
    <option value="SD">SD</option>
    <option value="TN">TN</option>
    <option value="TX">TX</option>
    <option value="UT">UT</option>
    <option value="VT">VT</option>
    <option value="VA">VA</option>
    <option value="WA">WA</option>
    <option value="WV">WV</option>
    <option value="WI">WI</option>
    <option value="WY">WY</option>
    </select><br>
    <label for="zip">ZIP</label>
    <input type="text"  name="zip" id="zip" placeholder="zip" value=""/><br>
<?php
}

![enter image description here][1]
![enter image description here][2]

1 个答案:

答案 0 :(得分:0)

尝试:

<p align="center">You have selected the <?php echo ($parts[$_GET['partId']]->getName()); ?>           </p>