How to post text to HTML with PHP [closed]

2019-09-02 04:54发布

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 <ul> class named, let's say, "confirmed", so <ul class="confirmed">,

If it hasn't been checked, then I would add the text to a <ul> class named "unconfirmed". Now I have the php code that checks if the checkbox has been confirmed (seen below), but I don't know what to add inside the statements. This is where I need help/guidance.

PHP IF code (Check if checkbox is ticked):

<?php

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


?> 

Here's my HTML Code:

<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">
  <li class="list-group-item">
<!--this is where to result of the php code would go if the checkbox is checked-->
    Lorem Ipsor
  </li>

</ul>

<div class="alert alert-danger" role="alert">Unconfirmed Players</div>
  <li class="list-group-item">
<!--this is where to result of the php code would go if the checkbox is not checked-->
    Lorem Ipsor
  </li>
</div>

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

</body>

Sorry if I seem to be confusing you, as I said, I'm not too well. Any help/guidance would be great!

2条回答
男人必须洒脱
2楼-- · 2019-09-02 05:20

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.

查看更多
你好瞎i
3楼-- · 2019-09-02 05:41

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>
查看更多
登录 后发表回答