根据选中的复选框更新值

时间:2017-10-09 12:25:55

标签: php html

我正在尝试根据表单上的选择更新文本框中的值。当用户选中一个选项框时,我试图让总成本增加一定的数量。其他所有工作都在表单上,​​如果我手动更改成本值,它将正确地发布到数据库。这可能与我的方法有关,还是我需要采用不同的技术?

<HEAD>
<script>
function tally()
{
    Cost = 60;

    if (Document.edituser.survivor10.checked ) { Cost = Cost + 10;    }
    if (document.edituser.high5.checked ) { Cost = Cost + 10;    }
    if (document.edituser.margin.checked == true ) { Cost = Cost + 10;  }
    if (document.edituser.survivor20.checked == true ) { Cost = Cost + 20;  }
    if (document.edituser.confidence.checked == true ) { Cost = Cost + 10;  }
    if (document.edituser.loser.checked == true ) { Cost = Cost + 10;  }
    if (document.edituser.vegas.checked == true ) { Cost = Cost + 10;  }                
    document.edituser.cost.value =  Cost;

}

<?php
require('includes/application_top.php');

include('includes/classes/class.formvalidation.php');

if (isset($_POST['submit'])) {
$my_form = new validator;

if($my_form->checkEmail($_POST['email'])) { // check for good mail

    if ($my_form->validate_fields('firstname,lastname,email,password')) { // 
comma delimited list of the required form fields
        if ($_POST['password'] == $_POST['password2']) {
            $salt = substr($crypto->encrypt((uniqid(mt_rand(), true))), 0, 10);
            $secure_password = $crypto->encrypt($salt . $crypto->encrypt($_POST['password']));
            $sql = "update nflp_users ";
            $sql .= "set password = '".$secure_password."', salt = '".$salt."', firstname = '".$_POST['firstname']."', lastname = '".$_POST['lastname']."',  textOption = '".$_POST['textOption']."', phone = '".$_POST['phone']."', carrier = '".$_POST['carrier']."', email = '".$_POST['email']."', survivor10 = '".$_POST['survivor10']."', survivor20 = '".$_POST['survivor20']."', loser = '".$_POST['loser']."', margin = '".$_POST['margin']."', high5 = '".$_POST['high5']."', vegas = '".$_POST['vegas']."', confidence = '".$_POST['confidence']."', cost = '".$_POST['cost']."'";               
            $sql .= "where userID = " . $user->userID . ";";
            //die($sql);
            $mysqli->query($sql) or die($mysqli->error);

            //set confirmation message
            $display = '<div class="responseOk">Account updated successfully.</div><br/>';
        } else {
            $display = '<div class="responseError">Passwords do not match, please try again.</div><br/>';
        }
    } else {
        $display = str_replace($_SESSION['email_field_name'], 'Email', $my_form->error);
        $display = '<div class="responseError">' . $display . '</div><br/>';
    }
} else {
    $display = '<div class="responseError">There seems to be a problem with your email address, please check.</div><br/>';
}
}

include('includes/header.php');

$sql = "select * from " . DB_PREFIX . "users where userID = " . $user->userID;
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$row = $query->fetch_assoc();
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$survivor10 = $row['survivor10'];
$survivor20 = $row['survivor20'];
$loser = $row['loser'];
$margin = $row['margin'];
$high5 = $row['high5'];
$confidence = $row['confidence'];
$vegas = $row['vegas'];
//  $textOption = $row['textOption'];
//  $phone = $row['phone'];
//  $carrier = $row['carrier'];
$cost = $row['cost'];

}

if (!empty($_POST['firstname'])) $firstname = $_POST['firstname'];
if (!empty($_POST['lastname'])) $lastname = $_POST['lastname'];
if (!empty($_POST['email'])) $email = $_POST['email'];
if (!empty($_POST['survivor10'])) $survivor10 = $_POST['survivor10'];
if (!empty($_POST['survivor20'])) $survivor20 = $_POST['survivor20'];
if (!empty($_POST['loser'])) $loser = $_POST['loser'];
if (!empty($_POST['margin'])) $margin = $_POST['margin'];
if (!empty($_POST['high5'])) $high5 = $_POST['high5'];
if (!empty($_POST['confidence'])) $confidence = $_POST['confidence'];
if (!empty($_POST['vegas'])) $vegas = $_POST['vegas'];
// if (!empty($_POST['textOption'])) $textOption = $_POST['textOption'];
// if (!empty($_POST['phone'])) $phone = $_POST['phone'];
// if (!empty($_POST['carrier'])) $carrier = $_POST['carrier'];
if (!empty($_POST['cost'])) $cost = $_POST['cost'];
?>
<h1>Edit User Account Details</h1>
<?php if(isset($display)) echo $display; ?>
<form action="user_edit.php" method="post" name="edituser">
    <fieldset>
    <legend style="font-weight:bold;">Enter User Details:</legend>
        <p>First Name:<br />
        <input type="text" name="firstname" value="<?php echo $firstname; ?>"></p>

        <p>Last Name:<br />
        <input type="text" name="lastname" value="<?php echo $lastname; ?>"></p>

        <p>Email:<br />
        <input type="text" name="email" value="<?php echo $email; ?>" size="30"></p>

        <p>New Password:<br />
        <input type="password" name="password" value=""></p>

        <p>Confirm Password:<br />
        <input type="password" name="password2" value=""></p><br>
        <tr><td></td></tr>
    <legend style="font-weight:bold;">Side Pools:</legend>
    <tr>
        <p><type=hidden value=" " name="survivor10" checked>        
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="survivor10" <? if($survivor10== "1") {echo "checked";} ?>><b> Survivor $10</b></p>
        <p><type=hidden value=" " name="survivor20" checked>
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="survivor20" <? if($survivor20== "1") {echo "checked";} ?>><b> Survivor2 $20</b></p>
        <p><type=hidden value=" " name="loser" checked>
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="loser" <? if($loser== "1") {echo "checked";} ?> ><b> Loser $10</b></p>       
        <p><type=hidden value=" " name="high5" checked>
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="high5" <? if($high5== "1") {echo "checked";}  ?>><b> High 5 $10</b></p>
        <p><type=hidden value=" " name="margin" checked>
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="margin" <? if($margin== "1") {echo "checked";} ?> ><b> Margin $10</b></p>
        <p><type=hidden value=" " name="vegas" checked>
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="vegas" <? if($vegas== "1") {echo "checked";} ?> ><b> Vegas $10</b></p>
        <p><type=hidden value=" " name="confidence" checked>
        <INPUT onclick=tally() TYPE="checkbox" value="1" NAME="confidence" <? if($confidence== "1") {echo "checked";} ?>><b> Confidence $10</b></p>
    </tr><br>
        <td><font color=red>Your Total Fee Is :</font><input type="int" size="3"  name="cost" value= "<? if($cost!= "") {echo "$cost"; } else {echo "60";}?>"</td><br><br>
<!--<tr>Text alert option: Message and data rates may apply. Expect approx. 3 msgs/week.</tr> -->
        <p><input type="submit" name="submit" value="Submit" class="btn btn-primary"></p>

    </fieldset>
</form>

1 个答案:

答案 0 :(得分:0)

JavaScript区分大小写,因此Documentdocument是两回事。在您的情况下,您想使用document。因此,您应该更改所有条件内的条件,以使用document

顺便说一下。永远不要相信用户!您可以在客户端进行计算,作为用户的指标,但您应该在服务器端再次执行此操作,否则每个人都可以将成本发布为类似 - 甚至是负数。

相关问题