如何使用PHP将文本发布到HTML [英] How to post text to HTML with PHP

查看:153
本文介绍了如何使用PHP将文本发布到HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我在这里之后是检查复选框是否被选中,如果有的话,我想将文本添加到< ul> 类的名称,比方说,确认,所以< ul class =confirmed>



如果它没有被选中,那么我会将文本添加到一个名为< ul> 的类中未经证实。现在我有了检查复选框是否已被确认的php代码(见下面),但我不知道在这些陈述中要添加什么。这是我需要帮助/指导的地方。



PHP IF代码(勾选复选框):

<$ p如果(isset($ _POST [checkbox1])){
//写入< ul>< code><?php

if班级确认
} else {
//写入< ul> class unconfirmed
}


?>

这是我的HTML代码:

 <身体GT; 

< div id =content>

< div class =jumbotron>
< h1>周三是否有橄榄球?< / h1>
< h2>是。< / h2>
< / div>

< div class =alert alert-inforole =alert>已确认的玩家< / div>

< ul class =list-group>
< li class =list-group-item>
<! - 这是如果复选框被选中,php代码的结果将到哪里 - >
Lorem Ipsor
< / li>

< / ul>

< div class =alert alert-dangerrole =alert>未经确认的玩家< / div>
< li class =list-group-item>
<! - 如果复选框没有被选中,这是php代码的结果。
Lorem Ipsor
< / li>
< / div>

<! - form - >
< form action =check.phpmethod =POST>
< input type =checkboxname =checkbox1>
< input type =submit>< / input>
< / form>

< / body>

抱歉,如果我似乎让你感到困惑,正如我所说的,我不太好。任何帮助/指导都会很棒!

解决方案

好吧,当您勾选一个复选框时,它应该通过$ _POST返回一个值提交时为1或0。您可以修改您的模板,如下所示:

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

?>
< body>

< div id =content>

< div class =jumbotron>
< h1>周三是否有橄榄球?< / h1>
< h2>是。< / h2>
< / div>

< div class =alert alert-inforole =alert>已确认的玩家< / div>

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

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

< / ul>

< div class =alert alert-dangerrole =alert>未经确认的玩家< / div>

< / div>

<! - form - >
< form action =check.phpmethod =POST>
< input type =checkboxname =checkbox1/>
< input type =submit/>
< / form>

< / body>

为了确保您始终可以var_dump $ _POST来查看其中的内容并修改您的如果声明更准确,则应返回1或0 iirc。

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!

解决方案

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.

这篇关于如何使用PHP将文本发布到HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆