I need to check whether checkbox is checked or not. Normally I would do it like this:
<?php
$checked = isset($_POST['checkbox']);
?>
But I don't know what is the name. More at screenshot (I'm using Laravel 4).
Screenshot
You simply can't. The data for unchecked checkboxes are not send to the server.
You could do a workaround with javascript where the JS appends some hidden fields before submit with the nonchecked boxes
Supposedly you should know what the list of checkboxes is/was that you asked the user to check. Checked checkboxes are submitted to the server, unchecked ones aren't. You can calculate the difference between these two lists.
if you are using jquery, and you know the id of the checkbox,
then, you can detect with following code:
var isChecked = $("#cbId").is(":checked");
<?php
if ( ! isset($_POST['checkbox_name']))
{
"Not checked";
}
?>
If checkbox didn't check - you will not have this variable in $_REQUEST.
<form action="">
<input type="checkbox" name="ch1"/>
<input type="checkbox" name="ch2"/>
<input type="checkbox" checked="checked" name="ch3"/>
<input type="submit" name="Post" value="Post">
</form>
When you click on "Post". In backend you'll see:
<?php
if(isset($_REQUEST['ch1']))
echo 'ch1 is checked!';
if(isset($_REQUEST['ch2']))
echo 'ch2 is checked!';
if(isset($_REQUEST['ch3']))
echo 'ch3 is checked!';
?>
In my case you'll see: "ch3 is checked!".
Related
im kinda trying to get into programming in general and was wondering how to uncheck / check with updating the array-
like as soon as someone checks a 2nd checkbox it should uncheck the first option and update the search (w the new data)- im a mere beginner and kinda lost rn so would appreciate any form of help
<form action="index.php" id="form1" name="form1" method="get">
<?php $i = 0; foreach ($row_page_nav_kategorie as $row_page_nav_kategorie) { ?>
<label class="checkbox-container">
<input <?php if (strpos($url,$row_page_nav_kategorie['typ']) == true) {echo 'checked="checked"';}?>
type="checkbox"
class="checkmark"
name="hotelKategorie[]"
id="ckb5"
value="<?php echo $row_page_nav_kategorie['typ']; ?>"
onclick="kategorie(<?php echo $i ?>);"
onchange="submit()"/>
<?php echo $row_page_nav_kategorie['typ']; $i++;?>
</label>
<?php } ?>
</form>
You should use radio buttons, but if you want to overwrite checkbox functionality below code will take care of it, I have added a common class on your inputs:
function checkboxClick(obj) {
var cbs = document.getElementsByClassName("checkkbox-option");
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = false;
}
obj.checked = true;
}
Demo
First of all welcome to the community!
As for your question, there is multiple ways to handle this, one of wich is as followed:
In HTML there's an attribute called radio wich you can add to your input by using type='radio'. In a set of radio buttons, only one can be checked at any time. If you then want to immedietely submit your form, you can use something like onChange='this.form.submit()'. This will submit your form when the value is changed, such as pressing on a different radio button.
Something to keep note of is that the attribute onChange is case sensitive as far as i'm aware. You were heading in the right direction with onchange="submit(), but your code doesn't know what to submit. this.form.submit() will submit the form that the element is in.
Use Radio Buttons or use JavaScript on your page to dynamically uncheck other checkboxes when you click on one.
If you want to dynamically update the page content with the new search results you should also look into AJAX which basically means you will call PHP functions from JavaScript code and those will return JSON arrays that you can exploit to modify your page's DOM.
Try THIS
HTML:
<label><input type="checkbox" name="check1" class="checkbox" /> CheckBox1</label>
<label><input type="checkbox" name="check2" class="checkbox" /> CheckBox2</label>
<label><input type="checkbox" name="check3" class="checkbox" /> CheckBox3</label>
<label><input type="checkbox" name="check4" class="checkbox" /> CheckBox4</label>
jQuery:
$(".checkbox").change(function() {
$(".checkbox").prop('checked', false);
$(this).prop('checked', true);
});
if you want to uncheck the selected checkbox
$(".checkbox").change(function() {
$(".checkbox").not(this).prop('checked', false);
});
hope this helps, thanks !!!
i hope you can help me, i think the solution is simple, but i am beginner, so kick my brain.
Now i want on a new page that the checkbox will be shown as either checked or unchecked depending on the result.
The results are available as 1 or 0, but how can I now display a checkbox as checked or unchecked?
I'm grateful for every tip.
If it is Javascript you can add your PHP variable to it like so:
echo '<input type="checkbox" name="checkbox1" value="'.$checkboxValue'.">
If you wanted to get the value of the check box you have to submit it in a form like so:
<form action="index.php" method="post">
Checkbox: <input type="checkbox" name="checkbox1">
<input type="submit">
</form>
index.php
<?php
if (isset($_POST['checkbox1']){
$checkboxValue = $_POST['checkbox1'];
}
?>
Note: The $_POST value is from the name attribute of your checkbox
I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}
A simple question but I can't seem to find the answer.
I have a simple form that inserts the value 'on' to a database when a checkbox is checked
How do I get the checkbox checked when the data is pulled from the database when the form is revisited and displaying with the database information?
I have tried this but it doesn't work
<input type="checkbox" name="positioning" class="input_margin" value="<? $row['positioning']; ?>">Positioning<br />
(I only need help with this, I have the sql query etc set up fine)
Thanks
Use a conditional statement to check the database value and echo 'checked' if it should be checked
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']=='on'){echo 'checked';} ?>>
You need to use the checked property and not value.
The value field is what is provided if the box is checked (e.g positioning=on) for value="on"
<input type="checkbox" name="positioning" class="input_margin" <? if($row['positioning']) { ?> checked<? } ?>/>Positioning<br />
Include the value attribute so the checkbox input will post "on" if checked.
Otherwise the form will initially retrieve a value but fail to post on any subsequent form submission. to the database
<input type="checkbox" name="positioning" class="input_margin" value="on" <? if($row['positioning'] =='on'){echo 'checked';} ?> />Positioning<br />
I am facing a problem with simple validating of radio button though database.
What I want to do is to just simply check whether radio button for each question is selected or not. I know the simple checking. But as I am using php variable name for every radio button name, it's very hard to convert to javascript variable and check. And I am stuck at this point.
<form id="formID" method="post" action="user_anger_quiz.php?rating_finished=true">
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,1"/>
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,2" />
<p id="anger_rating_submit">
<input type="submit" value="Rate my anger level"/></p>
</form>
Any suggestions would be appreciated in advanced.
regarding to your comment:
I guess that you will use php to check it due to the fact that you just add php as a tag.
In php you can use $_POST or $_GET on action page of your form to fetch the radio buttons.
Otherwise you can use javascript / javascript library
I think you're asking to make sure there is a radio button checked before submitting? Try the below untested function using jquery to see if there is a radio button selected. Run that before the form is submitted.
function somethingChecked()
{
if (!$("input[name='<?php echo $question_form_name; ?>']:checked").val()) {
alert('You need to check something');
return false;
} else {
return true;
}
}