从表单获取Checkboxes值并存储true / false或1/0

时间:2014-04-03 16:32:07

标签: php html mysql

我有一张表格,让票务购买者在购买门票后填写,我需要姓名,他们来自哪里,以及兴趣(这是复选框)。我还需要能够动态添加字段,所以我已经在使用数组了。我已将它连接到我的数据库,我将正确的数据存储在文本/输入中。但是复选框随机填充。我知道未经检查的盒子没有被发送所以我需要为它们分配某种价值吗?这是我到目前为止所做的工作:

<?php 
//Include the database class
require("db.class.php");
?>
<!DOCTYPE html>
<head>
<title>Vizion Conference Registration Form</title>
<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
<script type="text/javascript">
$(function() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function() {
            $('<div id="guest' + i +'"><p><label for="p_scnts"><input type="text" id="first[]" size="20" name="first[]" value="" placeholder="First" /></label><label for="p_scnts"><input type="text" id="last[]" size="20" name="last[]" value="" placeholder="Last" /></label><h3>Interests<small>(Select all that apply)</small></h3><div id="cat"><label><input type="checkbox" id="Soc[]" name="option1[]" value="1">Social Media</label><label><input type="checkbox" id="Web[]" name="option2[]" value="1">Website</label><label><input type="checkbox" id="Vid[]" name="option3[]" value="1">Video</label><label><input type="checkbox" id="Des[]" name="option4[]" value="1">Design</label><label><input type="checkbox" id="Mar[]" name="option5[]" value="1">Marketing</label><label><input type="checkbox" id="Aud[]" name="option6[]" value="1">Audio</label><label><input type="checkbox" id="Leg[]" name="option7[]" value="1">Legal</label><label>Other<input type="input" id="other[]" name="option8[]" value=""></label></div></p><a href="#" id="remScnt">Remove</a></div>').appendTo(scntDiv);
            i++;
            return false;
    });

    $('#remScnt').live('click', function() { 
            if( i > 2 ) {
                    $(this).parent('div').remove();
                    i--;
            }
            return false;
    });
});

</script>

</head>
<body>

<?php
//If form was submitted
if (isset($_POST['btnSubmit'])) {

//create instance of database class
$db = new mysqldb();
$db->select_db();

//Check if user has actually added additional fields to prevent a php error
if (isset($_POST['first'])) {

    //get last inserted userid
    $inserted_Guests_id = $db->last_insert_id();

    for ( $i=0;$i<count($_POST['first']);$i++) {
        $FirstName = $_POST['first'][$i];
        $LastName = $_POST['last'][$i];
        $HomeChurch = $_POST['hmch'];
        $SocialMedia = isset($_POST['option1']) ? 1 : 0 ;
        $Websites = isset($_POST['option2']) ? 1 : 0;
        $Video = isset($_POST['option3']) ? 1 : 0;
        $Design = isset($_POST['option4']) ? 1 : 0;
        $Marketing = isset($_POST['option5']) ? 1 : 0;
        $Audio = isset($_POST['option6']) ? 1 : 0;
        $Legal = isset($_POST['option7']) ? 1 : 0;
        $Other = $_POST['option8'][$i];

    $sql_Guests = sprintf("INSERT INTO Guests (FirstName, LastName, HomeChurch, SocialMedia, Websites, Video, Design, Marketing, Audio, Legal, Other) VALUES ('$FirstName','$LastName','$HomeChurch','$SocialMedia','$Websites','$Video','$Design','$Marketing','$Audio','$Legal','$Other')");  
$result_Guests = $db->query($sql_Guests);

    }

    //Loop through added fields
    }else {

    //No additional fields added by user

}
echo "<h1>User Added, <strong>" . count($_POST['first']) . "</strong> website(s) added for this user!</h1>";

//disconnect mysql connection
$db->kill();
}
?>
<?php if (!isset($_POST['btnSubmit'])) { ?>
<h2>Vizion Conference Registration Form</h2>

<form action="reg.php" method="post">
<div id="p_scents">
<label>Home Church</label><input type="text" name="hmch" id="hmch" placeholder="Somewhere Bible Church" />

<h2 style="float:left; margin-right:10px">Guests</h2>
<h3 style="float:left"><a href="#" id="addScnt">Add another Guest</a></h3><div style="clear:both"></div>

</div>
<input type="submit" name="btnSubmit" value="Register" />
</form>
<?php } ?>
</body>
</html>

0 个答案:

没有答案