【问题标题】:Inserting checkboxes inside php code在php代码中插入复选框
【发布时间】:2018-07-10 20:49:10
【问题描述】:

我正在尝试使用复选框来更新 MySQL 数据库中的信息。

“已发送”列是一个布尔值。

有没有办法在我的 php 代码中放置一个复选框,tbody 所以,而不是 <td>".$infoItems['Sent']."</td> 有一个复选框而不是零。

<table class="scroll">
  <thead>
    <tr>
      <th>Schools Name</th>
      <th>Schools Email</th>
      <th>Sent</th>
    </tr>
  </thead>

  <tbody>
      <?php

      $execItems = $conn->query("SELECT Name,SchoolMail,Sent FROM Schools");

      while($infoItems = $execItems->fetch_array())
      {
        echo    "
                <tr>
                    <td>".$infoItems['Name']."</td>
                    <td>".$infoItems['SchoolMail']."</td>

                    <td>".$infoItems['Sent']."</td>
                    /* ^Insert a checkbox here instead of this ^ */
                </tr>
            ";
        }
    ?>
    </tbody>
</table>
</body>

</html>

【问题讨论】:

  • 什么复选框和什么更新?你没试过吗?
  • 这是两个不同的问题吗...?
  • &lt;input type=checkbox...&gt; 等等?

标签: php html mysql checkbox


【解决方案1】:

你可以试试这样的

echo '<tr>
    <td>'.$infoItems['Name'].'</td>
    <td>'.$infoItems['SchoolMail'].'</td>
    <td>
        <input type="checkbox"'.($infoItems['Sent']?' checked':'').'
               data-mail="'.$infoItems['SchoolMail'].'"
               data-name="'.$infoItems['Name'].'"
               onchange="ajax_call_function(this)"
               />
    </td>
</tr>' ;

它会调用一个javascript函数:

<script type="text/javascript">
    function ajax_call_function(element) {
        console.log(element.getAttribute("data-name"));
        console.log(element.getAttribute("data-mail"));

        // make your AJAX call to a server-side script 
        // that updates your database with these informations.
        //
        // OR
        //
        // Update some hidden fields in a form and submit it. 

    }
</script>

【讨论】:

  • 是的,但 OP 还谈到了更新数据库信息。
  • 感谢您的意见!这对复选框有帮助,但是当我插入 SchoolMail 并发送电子邮件时,如何使复选框更新?
  • 提示:不要和引号打架,要么省略它们,就像你可以为大多数带有单个单词值的 HTML 标记属性一样,或者使用其他类型。例如:&lt;input type=checkbox&gt;&lt;input type='checkbox'&gt; 都可以。
  • @Felmorne 我已经更新了我的答案...但是有很多方法可以做到这一点。
  • @Syscall 对不起,如果我打扰了,但是如果您当然有空,我们可以直接沟通吗
猜你喜欢
  • 2013-10-04
  • 2020-10-10
  • 2017-11-12
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多