Reputation: 9053
I have form and here is check box, how to pass value from check box to PHP? For example.: If checkbox is checked pass 1
, if not checked 0
.
I'm using following:
<input type="checkbox" name="letter" value="letter" id="letter" checked="checked">
This is my form:
<form class="form" method="post" action="GetFbId.php">
<h2>Registration</h2><hr/>
<label>First Name: </label>
<input type="text" name="first_name" id="first_name" required>
<label>Last Name: </label>
<input type="text" name="last_name" id="last_name" required>
<label>City: </label>
<input type="text" name="city" id="city" required>
<label>Email: </label>
<input type="text" name="email" id="email" required><br>
Accept to get letters
<input type="checkbox" name="letter" value="letter" id="letter" checked="checked">
<input type="submit" name="register" id="register" value="Register">
</form>
Upvotes: 1
Views: 25527
Reputation: 431
When check box is checked after submitting the form we can get the check box value in php
<form class="form" method="post" action="GetFbId.php">
<h2>Registration</h2><hr/>
<label>First Name: </label>
<input type="text" name="first_name" id="first_name" required>
<label>Last Name: </label>
<input type="text" name="last_name" id="last_name" required>
<label>City: </label>
<input type="text" name="city" id="city" required>
<label>Email: </label>
<input type="text" name="email" id="email" required><br>
Accept to get letters
<input type="checkbox" name="letter" value="letter" id="letter" checked="checked">
<input type="submit" name="register" id="register" value="Register">
get check box value in GetFbId.php after submitting the form
<?php
//get post value
if(isset($_POST['letter']))
{
echo $letter = $_POST['letter']; //get check box value
}
?>
Upvotes: 3
Reputation: 126
In PHP, checkbox inputs will either be set in the $_POST
array, or they won't be. Checked checkboxes will be set, unchecked checkboxes will not.
If you change the value of the checkbox to 1 so that $_POST['letter']
would be 1
<input type="checkbox" name="letter" value="1" id="letter">
Then, if you need a 0 saved into the $letter
variable if it's not checked,
$letter = isset($_POST['letter']) ? $_POST['letter'] : 0;
Another option is to do it on the front end where you save a value of 0 or 1 into a hidden input and check the value of $_POST['letter']
on the PHP side because it will always have a value
<input type="hidden" value="0" name="letter" id="letter">
<input type="checkbox" name="select_letter" value="1" id="select_letter"
onchange="document.getElementById('letter').value = this.checked ? 1 : 0">
Upvotes: 5
Reputation: 872
<?php
if(isset($_POST['letter'])){
$letter = true;
}else{
$letter = false;
}
?>
Is this what you are asking for?
Upvotes: 2
Reputation: 5849
$_POST['letter']
will be set if, and only if, the checkbox was checked during the form submission.
Just test like so:
<?php
if(isset($_POST['letter']))
{
// $_POST['letter'] is set, checkbox has been checked.
}
Upvotes: 10
Reputation: 5166
you can use like this
<?
if(isset($_POST['register']))
{
// other code
$checkboxval= $_POST['letter']; //if checked 1 if not 0
}
?>
Upvotes: 2
Reputation: 602
Can you try something like this:
<?php
if(isset($_REQUEST['letter']) && $_REQUEST['letter']=="letter"){
$letter = $_POST['letter']; // check box value
}
?>
Upvotes: 2