簡體   English   中英

PHP的總結果

[英]Total result for PHP

我有一個使用復選框構建的表單,但無法計算其總結果。 如果用戶選擇三個復選框,則我希望獲得總計,如果一個復選框然后為$ 1,則總計為$ 3。 我被卡住了,因為我無法進行計算。

<html 
<head>
    <title>Order</title>

</head>
<body>


<link rel= "stylesheet" href= "order.css">
<form action="complete.php" method="post">
<form name="order">
<fieldset><legend>Complete Order:</legend>
<h1> Choose Design </h1>
<p><label>Your Name: <input type="text" name="name"></label>
<label>Address: <input type="text" name="address"></label>
    <label>Credit Card #: <input type="text" name="creditcard"></label>
    <label>Date: <input type="date" id="datepicker" name='date' size='9' value="" > </label>


<br><label> Design Types: <img src="1.jpg"><input type="checkbox" name="1"></label> $1.00 
<label><img src="2.jpg"><input type="checkbox" name="2"> </label>$1.00
<label><img src="3.jpg"><input type="checkbox" name="3"> </label>$1.00

<br></p>
<input type="submit" value="Submit Order"> 
</form>
</body>

</html>

郵遞區號

<html>
<head>
    <title>Form Feedback</title>
</head>
<body>
<?php

    if ( !empty($_POST['name']) && !empty($_POST['address']) && !empty($_POST['creditcard']) ) {
        echo "<p>Thank you, <strong>{$_POST['name']}</strong>, for placing the order. 
        <p>Your item will be shipped to: 
        <tt>{$_POST['address']}</tt></p>
        <p>Following credit card has been charged: <em>{$_POST['creditcard']}</em>.</p>\n";





    } else { //Missing form value.
        echo '<p>Please go back and fill out the form again.</p>';
    }


?>

</body>

</html>

HTML的變化:

<label><img src="1.jpg"><input type="checkbox" name="fieldname[]" value="1"></label> $1.00 
<label><img src="2.jpg"><input type="checkbox" name="fieldname[]" value="1"> </label>$1.00
<label><img src="3.jpg"><input type="checkbox" name="fieldname[]" value="1"> </label>$1.00

PHP代碼:它將計算所選復選框的數量-

<?php 
if(isset($_POST['fieldname']) && !empty($_POST['fieldname'])){
    echo count($_POST['fieldname']);  //count the number of selected checkbox
    echo array_sum($_POST['fieldname']); //sum of selected checkbox values
}
?>

首先,它與復選框的名稱有關。 如果它們是一組值(例如,產品),則使用通用名稱作為數組:

<input type="checkbox" name="product[]" />
<input type="checkbox" name="product[]" />
<input type="checkbox" name="product[]" />

第二個問題,您的復選框不應該有一個值嗎,或者始終為1.00美元?

它將始終為$ 1.00,只需計算一下:

$value = isset($_POST['product']) ? count($_POST['product']) : 0;

如果應該有一個值:

<input type="checkbox" name="product[]" value="1.00" />
<input type="checkbox" name="product[]" value="1.50" />
<input type="checkbox" name="product[]" value="2.00" />

可以使用array_sum函數:

$value = isset($_POST['product']) ? array_sum($_POST['product']) : 0;

使用isset($_POST['checkboxName'])檢查是否已選中該復選框,然后為每個復選框添加1:

$check_value = 0;
$check_value += isset($_POST['1']) ? 1 : 0;
$check_value += isset($_POST['2']) ? 1 : 0;
$check_value += isset($_POST['3']) ? 1 : 0;

更改php部分:

<?php
    if ( !empty($_POST['name']) && !empty($_POST['address']) && !empty($_POST['creditcard']) ) {
        $cost = 0;
        if(isset($_POST['1'])) $cost += 1;
        if(isset($_POST['2'])) $cost += 1;
        if(isset($_POST['3'])) $cost += 1;
        echo "<p>Thank you, <strong>{$_POST['name']}</strong>, for placing the order. 
        <p>Your item will be shipped to: 
        <tt>{$_POST['address']}</tt></p>
        <p>Following credit card has been charged: <em>{$_POST['creditcard']}</em>.</p>
        <p>With amount: <em>{$cost}</em>.</p>\n";
    } else { //Missing form value.
        echo '<p>Please go back and fill out the form again.</p>';
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM