【问题标题】:Show/Hide if php variable it's true如果 php 变量为真,则显示/隐藏
【发布时间】:2017-06-14 08:38:04
【问题描述】:

这是我的 html 代码:

<div id="view" class="login-box animated fadeInUp"<?php if ($reg===true){?>style="display:none"<?php } ?>>

        </div>

        <div id="popup"<?php if ($reg===false){?>style="display:none"<?php } ?>>
        </div>

这是我的 php 代码的一部分:

<?php
 ob_start();
 session_start();
error_reporting( ~E_DEPRECATED & ~E_NOTICE );

 $conn = mysqli_connect('localhost','root','','slide-uploader');

 if ( !$conn ) {
  die("Connection failed : " . mysqli_connect_error());
 }

 $error = false;
 $reg = false;

 /*Verifico i campi della form*/
 if ( isset($_POST['submit']) ) {


  $email = trim($_POST['user_email']);
  $email = strip_tags($email);
  $email = htmlspecialchars($email);
  $mailValidation = explode('@',$email)[1];
  $matricola = trim($_POST['user_matricola']);
   $name = ucfirst(explode('.',$email)[0]);
   $surname = ucfirst(preg_replace('/[0-9]+/', '', explode('@',explode('.',$email)[1]))[0]);
   $queryS = "SELECT * FROM studente WHERE Matricola ='$matricola'";
   $resS = mysqli_query($conn,$queryS);
   $count = mysqli_num_rows($resS);
   if($count<1){
     $query = "INSERT INTO studente (Matricola, Nome, Cognome, User) VALUES('$matricola','$name','$surname','$email')";
     $res = mysqli_query($conn,$query);
   }else{
     some code...
   }

   if ($res) {
    some code...
    unset($email);
    $reg = true;
    some code...
   } else {
     some code...
   }
  }
 }
ob_end_flush();
?>

我登录用户,然后返回上一页(window.history.go (-1)),但$reg 似乎没有设置。

我应该如何在 html 页面中设置 $reg 变量? html页面和php页面是两个独立的文件。

我应该使用变量$ _SESSION吗?

【问题讨论】:

  • 当您说$reg variable in the html page? 时,您是指.html 文件吗? PHP 不能在 html 文件中运行。
  • @Andreas 实际上可以将您的服务器配置为将 .html 文件解析为 PHP。但不推荐。
  • 您的代码容易受到 SQL 注入的影响,您需要解决这个问题。
  • @StuntHacks 这听起来很奇怪。实在看不出来这有什么意义。我的意思是你只需要将文件从 .html 重命名为 .php
  • @Andreas 我知道。我不知道为什么有人会这样做。只是想说,这确实是可能的。

标签: php html mysqli


【解决方案1】:

我无法完全理解您的网站应该做什么,但如果您只想根据变量是 true 还是 false 来隐藏/显示 html 内容,您可以使用类似这样的逻辑:

if ( $var ) //$var is true; display content
{
    ?>
        <div>Your html content that is supposed to be shown</div>
    <?php
}

您甚至可以添加else,如果不是true,则显示不同的内容。

if ( $var ) //$var is true; display content
{
    ?>
        <div>Your html content that is supposed to be shown</div>
    <?php
}
else
{
    ?>
        <div>A different html content</div>
    <?php
}

【讨论】:

    【解决方案2】:

    我不知道您为什么在真假两种情况下都使用“display:none”。你可以试试“display:block;”如果 $reg 为真(在两个地方的 style 之前也包括一个空格)

    <div id="view" class="login-box animated fadeInUp" <?php if ($reg===true){?> style="display:block;"<?php } ?>>
    
        </div>
    
    <div id="popup"<?php if ($reg===false){?> style="display:none"<?php } ?>>
        </div>
    

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      相关资源
      最近更新 更多