【问题标题】:Hide/Show toggle echo results in php在php中隐藏/显示切换回显结果
【发布时间】:2016-04-16 11:33:45
【问题描述】:

我正在尝试切换我已经从表中回显的结果,但我没有运气。 我已经在 HTML 中尝试过相同的代码,并且效果很好。 我已经为此工作了一段时间,想知道是否有人可以为我指明正确的方向。

我累了什么

  • 在 php echo 中添加 javascript。
  • 为 id 添加一个计数器,使其在循环中唯一
  • 放置回声的每一行都会导致回声(“”);标签。

添加一个计数器,使 id 在循环中唯一。

PHP

   $i = 1;
   while ($output = $fc_sel->fetch_assoc()) {
   $fc_run .= $output['Food_Cat_name'] . $output['Food_Cat_Desc'] . '<br>';
   $_SESSION['Food_Cat_name'] = $output['Food_Cat_name']; //echo out product name
   $_SESSION['Food_Cat_Desc'] = $output['Food_Cat_Desc']; //echo out product desc
 echo"

 <div id='first_product'>

 <button onclick='toggle_visibility('tog')'>Toggle</button>
  <div id='tog'>

        <div id='red_head'>
            <p id='menu_title' class ='hidden' onclick='toggle_visibility('tog')'>  Add your first menu item</p>
        </div>
        <h3 id='menu'>Menu Section</h3>

        <form name='first_prod' id='first_prod' enctype='multipart/form-data' action='testing.php' method='POST' accept-charset='utf-8' >               

            <label id='cat_label' name='cat_label'>Name</label>
            <input type='text' id='cat_name' name='cat_name' value=''>

            <label id='desc_label' name='desc_label'>Description</label>
            <input type='text' id='cat_desc' name='cat_desc' value=''>



        </form>


    </div>
    </div>

    ";  
    }
    }

JAVASCRIPT

 <script>
    //turn entire div into toggle
        function toggle_visibility(id) {

            var e = document.getElementById(id);
            if (e.style.display == 'block' || e.style.display == '')
                e.style.display = 'none';
            else
                e.style.display = 'block';
        }

    </script> 

【问题讨论】:

  • 你混淆了java和javascript,甚至标签描述都提到了它
  • @Mihai 是的,你是对的,我刚刚注意到了。我已经修改了
  • 'toggle_visibility('tog')' :您需要在函数中转义字符,如下所示:'toggle_visibility(\'tog\')'
  • @VincentG 我忘了说我也厌倦了。
  • 与循环代码共享代码,浏览器渲染的 HTML 就像在 chrome 加载页面中一样 Ctrl + u;

标签: javascript php mysql


【解决方案1】:

onclick='toggle_visibility('tog')' 中的单引号与外部单引号冲突。因此,要么转义它们,要么在外部对或内部对中使用双引号。

然后在 PHP 中,删除 &lt;?php echo。只需将 HTML 放入 PHP 中即可。 echo 只会使事情复杂化。如果您需要 HTML 中的动态数据,只需使用 &lt;?php ... ?&gt; 将其注入该位置即可。但到目前为止,我还没有在您的 HTML 中看到任何内容:它是静态的。

这是一个有效的 sn-p,在两个地方进行了更改之后:

//turn entire div into toggle
function toggle_visibility(id) {

  var e = document.getElementById(id);
  if (e.style.display == 'block' || e.style.display == '')
    e.style.display = 'none';
  else
    e.style.display = 'block';
}
<div id='first_product'>
  <button onclick="toggle_visibility('tog')">Toggle</button>
  <div id='tog'>
    <div id='red_head'>
      <p id='menu_title' class ='hidden' onclick="toggle_visibility('tog')">  Add your first menu item</p>
    </div>
    <h3 id='menu'>Menu Section</h3>
    <form name='first_prod' id='first_prod' enctype='multipart/form-data' action='testing.php' method='POST' accept-charset='utf-8' >               
      <label id='cat_label' name='cat_label'>Name</label>
      <input type='text' id='cat_name' name='cat_name' value=''>
      <label id='desc_label' name='desc_label'>Description</label>
      <input type='text' id='cat_desc' name='cat_desc' value=''>
    </form>
  </div>
</div>

修改问题后编辑

问题中的代码已扩展,因此 HTML 现在是循环生成的。

您现在应该注意只生成唯一的 id 值。因此,您可能需要在多个位置添加&lt;?=$i?&gt;,如下所示:

<?php
session_start();
// ....
$i = 1;
while ($output = $fc_sel->fetch_assoc()) {
    $fc_run .= $output['Food_Cat_name'] . $output['Food_Cat_Desc'] . '<br>';
    $_SESSION['Food_Cat_name'] = $output['Food_Cat_name'];
    $_SESSION['Food_Cat_Desc'] = $output['Food_Cat_Desc'];
?>

<div id='first_product<?=$i>'>

<button onclick="toggle_visibility('tog<?=$i>')">Toggle</button>
<div id='tog<?=$i>'>
    <div id='red_head<?=$i>'>

...等等。请注意,PHP 在 HTML 输出开始之前已关闭。这样做,而不是大的echo。然后在它的最后,再次打开 PHP 标签以结束循环:

<?php
}
?>

【讨论】:

  • 有没有绕过单引号和双引号的东西?我问的原因是因为我可以回显数据并且在我的回声中我使用双引号,使用带有回声标签的双引号与回声的双引号冲突。如果这有意义
  • 只在需要的地方使用 PHP echo。不要在很长的 HTML 部分中使用它。 HTML 应该放在 PHP 文件中的 &lt;?php ... ?&gt; 之外。
  • 这很有帮助。但我发现这是我没有在 tog 末尾添加 $i 的事实
  • 好的。当然,我不能说任何关于 $i 的内容,因为它不在您最初的问题中。现在我已经添加了一个关于它的部分。
  • 谢谢。是的,我没有意识到这是问题所在。不过谢谢
【解决方案2】:

请修改您的代码

echo "

 <div id='first_product'>

 <button onclick='toggle_visibility(\"tog\")'>Toggle</button>
  <div id='tog'>

        <div id='red_head'>
            <p id='menu_title' class ='hidden' onclick='toggle_visibility(\"tog\")'>  Add your first menu item</p>
        </div>
        <h3 id='menu'>Menu Section</h3>

        <form name='first_prod' id='first_prod' enctype='multipart/form-data' action='testing.php' method='POST' accept-charset='utf-8' >               

            <label id='cat_label' name='cat_label'>Name</label>
            <input type='text' id='cat_name' name='cat_name' value=''>

            <label id='desc_label' name='desc_label'>Description</label>
            <input type='text' id='cat_desc' name='cat_desc' value=''>



        </form>


    </div>
    </div>

    " 

【讨论】:

    【解决方案3】:
        <button value='tog' onclick='toggle_visibility(this)'>Toggle</button>
    

    只需使用“this”并为其工作的按钮分配值

    【讨论】:

    • 这不起作用。 toggle_visibility 函数需要一个字符串,其目的也不是隐藏您单击的按钮。
    【解决方案4】:

    试试这个;)

    <?php
    $i = 1;
    while($output = $fc_sel->fetch_assoc()){
      $fc_run .= $output['Food_Cat_name'] . $output['Food_Cat_Desc'] . '<br>';
      $_SESSION['Food_Cat_name'] = $output['Food_Cat_name']; //echo out product name
      $_SESSION['Food_Cat_Desc'] = $output['Food_Cat_Desc']; //echo out product desc
      ?>
      <div id="first_product<?php echo $i; ?>">
        <button onclick="javascript:toggle_visibility('tog<?php echo $i; ?>')">Toggle</button>
        <div id="tog<?php echo $i; ?>">
          <div id="red_head<?php echo $i; ?>">
            <p id="menu_title<?php echo $i; ?>" class ="hidden" onclick="toggle_visibility('tog<?php echo $i; ?>')">  Add your first menu item</p>
          </div>
          <h3 id="menu<?php echo $i; ?>">Menu Section</h3>
          <form name="first_prod" id="first_prod<?php echo $i; ?>" enctype="multipart/form-data" action="testing.php" method="POST" accept-charset="utf-8">
            <label id="cat_label<?php echo $i; ?>" name="cat_label">Name</label>
            <input type="text" id="cat_name<?php echo $i; ?>" name="cat_name" value="">
            <label id="desc_label<?php echo $i; ?>" name="desc_label">Description</label>
            <input type="text" id="cat_desc<?php echo $i; ?>" name="cat_desc" value="">
          </form>
        </div>
      </div>
      <?php
      $i++;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多