PHP

时间:2017-03-21 18:56:42

标签: php html

我有一个带复选框的表单,但我无法计算它的总结果。如果用户选择三个复选框,我想获得总数,如果一个然后是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>

Php代码

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

首先,它是关于复选框的名称。如果它们是一组值(例如:products),请使用通用名称作为数组:

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

答案 2 :(得分:0)

使用filenum = 1 while # getting input from user outfile = open ("userfile" + str(filenum), 'w') filenum += 1 # rest of your loop 检查复选框是否已选中,然后为每个复选框添加1:

isset($_POST['checkboxName'])

答案 3 :(得分: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>';
    }
?>