Neshat Neshat - 6 months ago 8
PHP Question

input checkbox always returns 0 in POST

I am trying to use checkboxes in a html form and send them to the my sql database by post. but i always get 0 no matter the checkbox is ckecked or not.

this is html form :

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


this is my php code:

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


and this is my storinCPC function in php:

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

Answer

Looking at a single of those input lines gives a clear understanding of your main issue:

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

Do you notice that you created a checkbox input field with name gradea, and right after created a hidden input field of the same name with value='0' which overwrites the first one?

Comments