Post value from checkbox to

Hi!

Im trying to figure out how to post either 0 or 1 depending on what value is selected in below switch.
I can generate a 0 or a 1 and show it on the page with id=“console-event” but i cant figure out how to parse the value in to input name=“oppethus” and post it.

<form method="post" name="form2" action="boka_edit.php" id="form2">
   
<div class="form-check pl-0 mt-2">
<input class="form-check-input" type="checkbox" id="oppethus" data-toggle="toggle" data-width="150" data-size="lg" data-off="Privat" data-on="Öppen" data-onstyle="success" data-offstyle="danger" >
<label class="form-check-label" for="oppethus">Välj öppen om alla är välkomna</label>

<script>

  $(function() {
$('#oppethus').change(function() {
  $('#console-event').html(+ $(this).prop('checked'))
})
  })

</script>
</div>


<p id="console-event">placeholder for 1 or 0</p>  
<input type="hidden" id="997" name="oppethus" value="0">

In JQuery you can do like so

<label><input id="cb" name="cb" type="checkbox">confirm</label>
<button id="btn">Confirm</button>

<script>
    $( document ).ready(function() {
      $("#btn").click(function() {
        $("#cb").prop( "checked", true );
      });
    });
</script

If you send the form type checkbox to PHP then PHP will only receive a value if the checkbox is checked . And you must add a name tag too. So you should try to use isset().

<?php
if(isset($_POST['cb'])) {
    // checkbox is checked
}

Thanks for the feedback, but maby i took the code to much out of context let me clarify.
So when submitting im sending date(bookingdate) timeslot(slotid) from session(fastighet) swtich checkbox öppen(oppethus). Method sugested did not work as intended for me. How can i post either 0 or 1 to oppethus?

    <?php
include_once('standard.php');
?>
<!doctype html>
<html lang="sv">
    <?php include_once 'head.php'; ?>
    <?php include_once("databas.php"); ?>
<!-- function for changeing slotbuttons on select to green and the others to white. consider using jquery and $("input[type='button'].booking").css("color", "white") would for instance set the color white on ALL buttons with class="booking"-->
<?php
$sqlCount = "SELECT count(yttre.id) as totalSlots FROM bastun_slots AS yttre WHERE yttre.id NOT IN (SELECT inre.slotid FROM bastun_bookings AS inre WHERE inre.bookingdate LIKE '".$_POST[txtDate]."') order by id asc";
$result2 = $conn->query($sqlCount);
if ($result2->num_rows > 0) {
    // output data of each row
    while($row = $result2->fetch_assoc()) {
        $totalSlots = $row["totalSlots"];
    }
} else {
    //echo "0 results";
}
?>
<script>
function selectTimeslot(mytimeslot) {
    var totalSlots2 = "<?php echo $totalSlots; ?>";
    totalSlots2 = parseInt(totalSlots2);
    totalSlots2 = totalSlots2+1;
    //window.alert(mytimeslot);
    var count;
    for(count = 1; count < totalSlots2; count++){
        if(document.getElementById(count)){
            document.getElementById(document.getElementById(count).value).style.color = "#FFFFFF";
        }
        }
    document.getElementById(document.getElementById(mytimeslot).value).style.color = "#000";
    document.getElementById("999").value = document.getElementById(mytimeslot).value;
}
</script>
<!-- function ends"-->
 <body>
    <?php include_once 'script.php'; ?>
    <?php include_once 'header.php'; ?>
    <div class="container">
        <a href="start.php"><button type="button" class="shadow btn btn-info btn-lg btn-block mb-2">START</button></a>
<!-- Test av befintlig bokning -->
    <div class="shadow p-3 bg-white rounded">
    <div class="col">
<?php
$todayDate = date('Y-m-d', strtotime('yesterday'));
$sql = "SELECT *, bastun_bookings.id as minrad FROM bastun_bookings, bastun_slots where bastun_bookings.fastighet = '".$_SESSION["Fastighet"]."' and bastun_slots.id = bastun_bookings.slotid and bastun_bookings.bookingdate > '$todayDate'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
   while($row = $result->fetch_assoc()) {
        $bookingDate = date_create($row["bookingdate"]);
        $startTime = date_create($row["starttime"]);
        $endTime = date_create($row["endtime"]);
        $BookingSlotId = ($row["minrad"]);
    echo '<div class="alert alert-warning shadow" role="alert">' . "Du har bokat bastun: " . date_format($bookingDate, 'M d,'). " " . date_format($startTime, 'H:i'). "-" . date_format($endTime, 'H:i'). " Vill du ta bort bokningen?";
    echo '</div>';
    echo '<form name="form3" action="boka_delete.php" id="form3" method="post">';
    echo '<input type="hidden" id="888" name="BookingSlotIdDelete" value="'.$BookingSlotId.'">';
    echo '<input class="btn btn-block btn-warning shadow btn-lg" type="submit" name="btnDelete" value="RADERA BOKNINGEN">';
    echo '</form>';
    //echo $BookingSlotId;
    $bookingexists = 1;

    }
} 
else{
?>
        <script type="text/javascript">
        function submitform1()
        {
        document.form1.submit();
        }
        </script>
        <form method="post" name="form1" action="boka.php" id="form1">
        <!-- <section class="section" -->
        <div class="row">
            <div class="col">
            <input value="<?php echo $_POST[txtDate]; ?>" class="datepicker form-control form-control-lg mb-3" name="txtDate" type="text" placeholder="V&auml;lj datum h&auml;r..." onchange="submitform1()">
           
        <script type="text/javascript">
            var $input = $( '.datepicker' ).pickadate({
            monthsFull: ['Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni', 'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December'],
            monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'Maj', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'],
            weekdaysFull: ['Söndag', 'Måndag', 'Tisdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lördag'],
            weekdaysShort: ['Sön', 'Mån', 'Tis', 'Ons', 'Tor', 'Fre', 'Lör'],
            formatSubmit: 'yyyy-mm-dd',
            firstDay: 1,
            clear: false,
            close: false,
            today: false,
            hiddenName: true,
            //disable: [
            //    4, 5
            //    ],
            })
            var picker = $input.pickadate('picker')
        </script>
        </div>
        </div>
        <!-- </section>  -->
    <?php
    $sql = "SELECT * FROM bastun_slots AS yttre WHERE yttre.id NOT IN (SELECT inre.slotid FROM bastun_bookings AS inre WHERE inre.bookingdate LIKE '".$_POST[txtDate]."') order by starttime asc";
    $result = $conn->query($sql);
    
    $dayofweek = date('w', strtotime($_POST[txtDate]));
    $unixdate = date('Y', strtotime($_POST[txtDate]));
    $pickeddate = date("Y-m-d", strtotime($_POST[txtDate]));
    $todayDateNow = date('Y-m-d', strtotime('today'));
    
    if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Kolla om raden är torsdag eller fredag och sätt värdet 1 till PrintButton.
        $PrintButton = ( ( $dayofweek >= 4 ) && ( $dayofweek <= 5 ) );
        //echo $PrintButton;
        //echo $pickeddate . '<br>';
        //echo $todayDateNow. '<br>'; 
        
        $starttimeVar = date_create($row["starttime"]);
        $endtimeVar = date_create($row["endtime"]);
        
        $starttimeVar = date_format($starttimeVar, 'H:i');
        $endtimeVar = date_format($endtimeVar, 'H:i');
        // Kontrollera om det är 1970 eller om datumet valt är äldre än dagens datum isåfall skriv inte ut något.  
        
        if ($unixdate == "1970" or $todayDateNow > $pickeddate) {
        //Kontrollera sen om day of week är 4 eller 5 och slotid är 4. Torsdag och fredag ska bara gå att boka slot 4.
        
        }elseif ($PrintButton == 1 and $row["id"] == 4) {
            echo ' <button class="btn btn-info btn-lg btn-block" role="button"' ;
            echo " type=button id=" . $row["id"]. " onclick=selectTimeslot(". $row["id"].") name=btnSlot value=" . $row["id"].">" . $starttimeVar. " - " . $endtimeVar. "</button>";
        }elseif ($PrintButton == 1 and $row["id"] < 4) {
        // Den här ska inte skrivas ut.
        }else {
            echo ' <button class="btn btn-info btn-lg btn-block" role="button"' ;
            echo " type=button id=" . $row["id"]. " onclick=selectTimeslot(". $row["id"].") name=btnSlot value=" . $row["id"].">" . $starttimeVar. " - " . $endtimeVar. "</button>";
        }
    }
} else {
   //echo "Inga lediga tider";
}
//$conn->close();
?>
</form>
    <form method="post" name="form2" action="boka_edit.php" id="form2">
    <input type="hidden" id="999" name="txtSlot" value="0">
    <input type="hidden" id="998" name="txtDate" value="<?php echo $_POST[txtDate]; ?>">
    
    

   
<div class="form-check pl-0 mt-2">
<label class="form-check-label" for="oppethus"><input class="form-check-input" id="cb" name="cb" type="checkbox" data-toggle="toggle" data-width="150" data-size="lg" data-off="Privat" data-on="Öppen" data-onstyle="success" data-offstyle="danger" > Välj öppen om alla är välkomna</label>



<script>

  $(function() {
    $('#cb').change(function() {
      $('#console-event').html(+ $(this).prop('checked'))
    })
  })

</script>
    </div>
    
    <!--p id="console-event">value</p-->
    <div id="console-event"></div>


    <input type="hidden" id="cb" name="oppethus" value="0">
    <input class="shadow btn btn-success btn-lg btn-block mb-2 mt-2" type="submit" onClick="this.form.submit(); this.disabled=true; this.value='Sending…'; " value="SKICKA BOKNINGEN">
    </div>
</div>


The way a checkbox works is, it is either checked and passing a value, or it isn’t checked and won’t send anything. You are safe to assume that if it was not sent in the request, that it is false.

Is that signifying something? Like there is an opening if it is green?

If switched to green it meeans that the meet is not private all members can attend.
Im cool with it not sending anything if its not switched to green it can just send a 1 if set to green.

T.

That’s essentially what would happen. If it is checked, it would send whatever you have in the value attribute. If it is not checked, it won’t send any value to the processor.

Sponsor our Newsletter | Privacy Policy | Terms of Service