Skip to content Skip to sidebar Skip to footer

How To Post Text To HTML With PHP

Okay so what I am after for here is to check if a checkbox has been ticked, if it has, then I would like to add the text into a
    class named, let's say, 'confirmed', so &

Solution 1:

Alright so when you tick a checkbox it should return a value through the $_POST which is 1 or 0 on submit. You may modify your template like this:

<?php 
    if ($_POST['checkbox1']) {
    $class = 'confirmed';
    } 
    else {
    $class = 'unconfirmed';
    }

?>
<body>

<div id="content">

    <div class="jumbotron">
  <h1>Is there football on Wednesday?</h1>
  <h2>Yes.</h2>
</div>

<div class="alert alert-info" role="alert">Confirmed Players</div>

<ul class="list-group <?php echo $class; ?>">

  <li class="list-group-item">
    Lorem Ipsor
  </li>

</ul>

<div class="alert alert-danger" role="alert">Unconfirmed Players</div>

</div>

<!--form-->
<form action="check.php" method="POST">
    <input type="checkbox" name="checkbox1" />
    <input type="submit" />
</form>

</body>

To be sure you can always var_dump the $_POST to see what is coming through in it and modify your if statement to be more accurate, it should return 1 or 0 iirc.


Solution 2:

Without using AJAX you can try nesting php code like this:

<ul class="list-group">
  <li class="list-group-item">
<?php

if (isset($_POST["checkbox1"])){
    //write to <ul> class confirmed
    }


?> 
  </li>

</ul>

<div class="alert alert-danger" role="alert">Unconfirmed Players</div>
  <li class="list-group-item">
<?php

if (!isset($_POST["checkbox1"])){
    //write to <ul> class unconfirmed
    }



?> 
  </li>

Post a Comment for "How To Post Text To HTML With PHP"