dongyao4003 2014-03-16 20:29
浏览 62
已采纳

PHP通过href传递会话变量?

first of all, i am only 6 weeks into php and programming in general so bear with me. i'm creating an application that starts on the main page listing ten parts with price and displaying them. then you can click on the add part link to kick you to a new area where you select the quantity of that part and then place it in the cart. the cart displays your item, quantity, price etc. I cant figure out how to send the selected part when clicking the link to the getQuantity function where it says...... You have selected....... help and thank you in advance!

<?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条回答 默认 最新

  • dqab0824 2014-03-16 20:39
    关注

    Try:

    <p align="center">You have selected the <?php echo ($parts[$_GET['partId']]->getName()); ?>           </p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。