输入复选框始终在POST中返回0

时间:2016-05-22 05:06:58

标签: php html checkbox

我正在尝试使用html表单中的复选框,并通过post将它们发送到我的sql数据库。但是无论复选框是否被打开,我总是得到0。

这是html表格:

   <form action = "MediaList2.php" method = "POST">
        <li><input type="checkbox" name="gradea" value="1"> CPC for Grade A websites<br><input type='hidden' value='0' name='gradea'></li>
        <li><input type="checkbox" name="gradeb" value="1"> CPC for Grade B websites<br><input type='hidden' value='0' name='gradeb'></li>
        <li><input type="checkbox" name="gradec" value="1"> CPC for Grade C websites<br><input type='hidden' value='0' name='gradec'></li>
        <li><input type="text" name="clicksno" placeholder="if you want a specific number for your clicks please mention it here ">Number of Clicks<br></li>
        <li> Duration<input type="text" name="start_date" class="form-control" id="start_date" placeholder="yyyy-mm-dd" /> <span> TO </span>  <input type="text" name="end_date" class="form-control" id="end_date" placeholder="yyyy-mm-dd" />
 </li>
        <li> Budget <input type ="text" name ="budget" required></li>

             <button type= "submit">Submit</button> 
             </form>

这是我的PHP代码:

    try{
   // receiving the post params
    $gradea= $_POST['gradea'];
    $gradeb= $_POST['gradeb'];
    $gradec= $_POST['gradec'];
    $clicksno= $_POST['clicksno'];
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    $budget = $_POST['budget'];
    if($budget > $_SESSION['totalcampaignbudget'])
    {
               echo "Your CPC budget should not be bigger than your total budget!";
               exit(0);
    }
    $user = $db->storeinCPC($gradea , $gradeb, $gradec, $clicksno, $start_date, $end_date, $budget, $campaignname, $email);

这是我在php中的storinCPC函数:

public function storeinCPC($gradea , $gradeb, $gradec, $clicksno, $start_date, $end_date, $budget, $campaignname, $email)
    {                                                                            // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
                                                                                      // Check connection
                                                                                       if ($conn->connect_error)
                                                                                       {
                                                                                       die("Connection failed: " . $conn->connect_error);
                                                                                       }                                                                             
    $q1 = "SELECT * from CPC where tocampaign = '$campaignname' AND toperson = '$email' " ;
    $res1 = $conn -> query($q1);
    $count = $res1 ->num_rows;
    if($count != null )
    {
        $query = "UPDATE CPC set clicksno ='$clicksno' , GradeA = '$gradea', GradeB='$gradeb', GradeC = '$gradec',budget='$budget', tocampaign='$campaignname', startdate='$start_date', enddate='$end_date', toperson='$email', updated_at = NOW()  WHERE   toperson = '$email' AND tocampaign = '$campaignname'";
    }
    else if($count ==0)
    {
    $query = "INSERT INTO CPC (clicksno , GradeA, GradeB, GradeC,budget, tocampaign, startdate, enddate, toperson, created_at, updated_at ) VALUES ('$clicksno', '$gradea', '$gradeb' , '$gradec', '$budget', '$campaignname', '$start_date', '$end_date', '$email', NOW(), NOW())";
    }
    $result = $conn->query($query);
    }

1 个答案:

答案 0 :(得分:2)

查看其中一条输入行可以清楚地了解您的主要问题:

<li><input type="checkbox" name="gradea" value="1"> CPC for Grade A websites<br><input type='hidden' value='0' name='gradea'></li>

您是否注意到您创建了一个名为gradea的复选框输入字段,并在创建了一个与value='0'同名的隐藏输入字段后覆盖了第一个字段?