如何 post 使用 PHP 将文本发送到 HTML
How to post text to HTML with PHP
好的,所以我在这里要做的是检查 checkbox
是否已被勾选,如果有,那么我想将文本添加到 <ul>
class 命名,比方说,"confirmed",所以 <ul class="confirmed">
,
如果还没有检查,那么我会将文本添加到名为 "unconfirmed" 的 <ul>
class 中。现在我有了 php 代码来检查复选框是否已被确认(如下所示),但我不知道要在语句中添加什么。这是我需要的地方 help/guidance.
PHP IF 代码(检查复选框是否被勾选):
<?php
if (isset($_POST["checkbox1"])){
//write to <ul> class confirmed
} else {
//write to <ul> class unconfirmed
}
?>
这是我的 HTML 代码:
<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>
对不起,如果我让你感到困惑,就像我说的,我不太好。任何 help/guidance 都会很棒!
好的,所以当您勾选一个复选框时,它应该 return 通过 $_POST 提交的值是 1 或 0。您可以这样修改您的模板:
<?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>
为了确保您始终可以 var_dump $_POST 来查看其中的内容并修改您的 if 语句以使其更准确,它应该 return 1 或 0 iirc。
在不使用 AJAX 的情况下,您可以尝试嵌套 php 代码,如下所示:
<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>
好的,所以我在这里要做的是检查 checkbox
是否已被勾选,如果有,那么我想将文本添加到 <ul>
class 命名,比方说,"confirmed",所以 <ul class="confirmed">
,
如果还没有检查,那么我会将文本添加到名为 "unconfirmed" 的 <ul>
class 中。现在我有了 php 代码来检查复选框是否已被确认(如下所示),但我不知道要在语句中添加什么。这是我需要的地方 help/guidance.
PHP IF 代码(检查复选框是否被勾选):
<?php
if (isset($_POST["checkbox1"])){
//write to <ul> class confirmed
} else {
//write to <ul> class unconfirmed
}
?>
这是我的 HTML 代码:
<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>
对不起,如果我让你感到困惑,就像我说的,我不太好。任何 help/guidance 都会很棒!
好的,所以当您勾选一个复选框时,它应该 return 通过 $_POST 提交的值是 1 或 0。您可以这样修改您的模板:
<?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>
为了确保您始终可以 var_dump $_POST 来查看其中的内容并修改您的 if 语句以使其更准确,它应该 return 1 或 0 iirc。
在不使用 AJAX 的情况下,您可以尝试嵌套 php 代码,如下所示:
<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>