0

i have this code:

<form id="form" method="post" action="">
<input type="checkbox" name="checkbox" onchange="$('#form').submit();">
<label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
</label>
</form>


<?php if (isset($_POST['checkbox'])){
echo "ok";} ?>

but the post of the checkbox doing nothing

can you help me sorry im newbie

11
  • 1
    so where's the jquery that goes with this? you also tagged as CI. Look at you console. Commented Apr 25, 2016 at 22:50
  • ^^ and you're forgetting the action tag which. Maybe consider PHP SELF for this page Commented Apr 25, 2016 at 22:52
  • you have no php tags around your php Commented Apr 25, 2016 at 22:55
  • it doesnt work ... Commented Apr 25, 2016 at 23:04
  • 1
    Works for me: dev.bridgebase.com/barmar_test/testcheckbox.php Commented Apr 25, 2016 at 23:30

2 Answers 2

0

This is the exact code that I tested and it works.

When it's displaying the form, it uses isset($_POST['checkbox']) to decide whether to add checked="checked" to the HTML, so it remembers what the user's choice was. Then the PHP code at the end does different things depending on whether it's checked.

<html>
<body>
<script   src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<form id="form" method="post" action="">
<input type="checkbox" name="checkbox" onchange="$('#form').submit();" <?php if(isset($_POST['checkbox'])) { echo 'checked="checked"'; } ?>>
<label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
</label>
</form>


<?php
     if (isset($_POST['checkbox'])) {
         echo "box is checked";
     } else {
         echo "box is not checked";
     }
?>
</body>
</html>

DEMO

Sign up to request clarification or add additional context in comments.

7 Comments

it works thanks ! but if the checkbox is checked="checked" its doesnt work
Only checked boxes are sent to the server. So if the box is not checked, isset() will be false. If the box is originally checked in the HTML, clicking on it unchecks it, so nothing is sent in $_POST['checkbox'].
So it only prints ok when the user checks the box, not when the uncheck it.
I need that also when you unchecked the checkbox the program do something...its possible ? @Barmar
I've updated the answer to show how it can remember whether the box is checked, and show a different answer depending.
|
0

lot of solutions but here the fast one :

<form id="form" method="post" action="">
<input type="checkbox" name="checkbox" onchange="this.form.submit();">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</form>

3 Comments

i tested in chrome & Firefox , you may use IE , am I right?
copy and paste my code and check it again , i tested in chrome it is fine
if you insisted to use JQuery , your code is fine just check if you include the JQuery library correct , put it between head tags

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.