首先PHP高校分配 - 使用隐藏值传递参数?

问题描述 投票:2回答:2

我一直对这个PHP分配的天,不能让过去我已经打在了墙上。第一页有一个“计算”按钮,计算出的号码显示在同一页面上。我想,我有正常工作。第一页还具有是假设将用户带到第二页和仅显示某些的从第一页的信息的“确认”按钮。我无法计算出的数字转移到第二页。

我联系我的教授的意见,他回答说,“验证和计算在回发发生,并且确认将回传,然后向用户发送至其他页面。当您将数据传输到其他页面,你将不得不手动创建的get或添加值的形式”隐藏的价值...

老实说,我不知道他是什么意思。我们甚至还没有覆盖隐藏值。我没有在这里找到许多例子为,但成功的尝试。

这是我的第一个和第二个页面的代码。

<?php
// get the data from the form
$sales_price = filter_input(INPUT_POST,'sales_price', FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST,'discount_percent',FILTER_VALIDATE_FLOAT);
$total_price = filter_input(INPUT_POST,'total_price', FILTER_VALIDATE_FLOAT);

// validate sales_price
if ($sales_price === FALSE) {
    $sales_priceError = 'Sales price must be a valid amount';
} else if ($sales_price < 1.0) {
    $sales_priceError = 'Sales price must be greater than 0';
} else {
    $sales_priceError = '';
}

// validate discount_percent
if ($discount_percent === FALSE) {
    $discount_percentError = 'Discount percent must be a valid amount';
} else if ($discount_percent < 1.0) {
    $discount_percentError = 'Discount percent must be greater than 0';
} else {
    $discount_percentError = '';
}


// calculate the discount and the discounted price
$discount_amount = $sales_price * $discount_percent / 100;
$total_price = $sales_price - $discount_amount;


?>

<!doctype html>
<html lang="en">
<head>
    <title>Quote</title>
</head>
<body>
<section>
    <h1>Price quotation</h1>
    <form id="priceForm" name="priceForm" method="post" action=''>
        <label for="sales_price">Sales price </label>
        <input type="text" id="sales_price" name="sales_price" required
               value="<?php echo $sales_price; ?>"/>
        <?php if (!empty($sales_priceError)) : ?>
            <span style="color:red;background-color: white">
                    <?php echo $sales_priceError; ?>
                </span>
        <?php endif; ?>
        <br/>
        <br/>
        <label for="discount_percent">Discount percent </label>
        <input type="text" id="discount_percent" name="discount_percent" required
               value="<?php echo $discount_percent; ?>"/>
        <?php if (!empty($discount_percentError)) : ?>
            <span style="color:red;background-color: white">
                    <?php echo $discount_percentError; ?>
                </span>
        <?php endif; ?>
        <p>Discount amount <?php echo '$' . number_format($discount_amount, 2); ?></p>
        <p>Total price <?php echo '$' . number_format($total_price, 2); ?></p>
        <input type="submit" name="submitButton" id="submitButton" value="Calculate"/>
    </form>
    <form id="confirmForm" name="confirmForm" method="post" action="confirm.php">
        <input type="hidden" id="sales_price" name="sales_price"/>
        <input type="hidden" id="discount_percent" name="discount_percent"/>
        <input type="hidden" id="discount_amount" name="discount_amount"/>
        <input type="hidden" id="total_price" name="total_price"/>
        <input type="submit" name="confirmSubmit" id="confirmSubmit" value="Confirm"/>
    </form>

    <p>Enter price and discount amount and click Calculate</p>
</section>
</body>
</html>



AND MY SECOND PAGE  ...  


<?php
$sales_price = filter_input(INPUT_POST, 'sales_price', FILTER_VALIDATE_FLOAT);
$discount_amount = filter_input(INPUT_POST, 'discount_amount', FILTER_VALIDATE_FLOAT);
$discount_percent = filter_input(INPUT_POST, 'discount_percent', FILTER_VALIDATE_FLOAT);
$total_price = filter_input(INPUT_POST, 'total_price', FILTER_VALIDATE_FLOAT);
$name = filter_input(INPUT_POST, 'name');
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);


// validate name
if ($name === '') {
    $nameError = 'Name is required';
} else {
    $nameError = '';
}

?>

<!doctype html>
<html lang="en">
<head>
    <title>Confirmation</title>
</head>
<body>
<section>
    <h1>Quotation confirmation</h1>
    <p>Sales price $<?php echo number_format($sales_price, 2); ?></p>
    <p>Discount amount $<?php echo number_format($discount_amount, 2); ?></p>
    <p>Total price $<?php echo number_format($total_price, 2); ?> </p>

    <h2> Send confirmation to</h2>
    <form id="nameForm" name="nameForm" method="post">
        <label for="name">Name </label>
        <input type="text" id="name" name="name" required
               value="<?php echo $name; ?>"/>
        <?php if (!empty($nameError)) : ?>
            <span style="color:red;background-color: white">
                    <?php echo $nameError; ?>
                </span>
        <?php endif; ?>
        <br/>
        <br/>
        <label for="email">Email address</label>
        <input type="email" id="email" name="email" required/>
    </form>
    <br/>
    <input type="submit" name="quoteSubmit" id="quoteSubmit" value="Send Quotation"/>
    <form id="returnForm" name="returnForm" action="index.php">
        <input type="Submit" name="returnSubmit" id="returnSubmit" value="Return"/>
    </form>

</section>
</body>
</html>
php
2个回答
1
投票

问题是在你的输入类型,你还没有给这就是为什么没有值传递给下一个页面输入类型的任意值。试着做这样的事情你所有隐藏的输入:

<input type="hidden" id="sales_price" name="sales_price" value="<?php echo $sales_price ?> "/>

0
投票

你是八九不离十。

隐藏的方法

隐藏的输入需要传递的值,否则,他们将返回空白 - 例如:

<input type="hidden" id="sales_price" name="sales_price" value="<?php echo $sales_price;?>"/>

然后,您将能够访问他们confirm.php任何其他变量$_POST['sales_price'];

GET方法

您的另一种选择是使用GET变量,并将其分配给您的表单动作:例如:

<form id="confirmForm" name="confirmForm" method="post" action="confirm.php?sales_price=<?php echo $sales_price;?>&discount_amount=<?php echo $discount_amount;?>">

这些然后可以通过confirm.php像访问:

$_GET['sales_price'];

推荐问答