【问题标题】:Add/change text in div based on php if statement基于 php if 语句在 div 中添加/更改文本
【发布时间】:2013-06-28 20:44:45
【问题描述】:

我有一个创建新用户的页面。我有一个 php 脚本,它对文本字段中输入的数据进行各种检查。当它发现不符合的条目时,我需要向用户提供反馈,以说明进程终止的原因。我的想法是简单地有一个空的 div,我将使用特定于失败条件的消息填充它。

但是,看起来这可能比我想象的要困难。我宁愿在我的 php 脚本中这样做。我对网络开发很陌生。我愿意接受有关实现此目标的最佳/最简单方法的建议。谢谢。

<div id="page">
    <img id="mainpic" src="images/banner.png" height="390" width="982">

    <div id="stylized" class="leftsidebox"></div>

     <div id="stylized" class="myform">
        <form id="form" name="form" method="post" action="scripts/create_admin.php">

            <label>Security Code
                <span class="small">Enter the security code provided to you</span>
            </label>
            <input type="text" name="securitycode" id="name" />

            <button type="submit">Create Administrator</button>
        </form>
    </div>

    <div id="stylized" class="rightsidebox">
        <div id="stylized" class="feedback">
            <h1>this is where I want to add dynamic text from php</h1>
        </div>

    </div>
</div>


<?php

    include('connection.php');

    //retrieve input values from the form
    $security_code = $_REQUEST['securitycode'];

    if (strlen($security_code) == 0) {
        //Add the text "Please enter a security code." to the div class="feedback"
        die();
    }
?>

【问题讨论】:

    标签: php html web-applications


    【解决方案1】:

    在 html 之前添加 php 代码

    <?php
    
        include('connection.php');
    
        //retrieve input values from the form
        $security_code = $_REQUEST['securitycode'];
    
        if (strlen($security_code) == 0) {
        $error="Please enter a security code.";    
        //Add the text "Please enter a security code." to the div class="feedback"
            die();
        }
    ?>
    
    <div id="stylized" class="feedback">
                <h1><?php if(isset($error)){ echo $error; } ?></h1>
            </div>
    

    【讨论】:

      【解决方案2】:

      您需要重新加载页面。如果您在安全代码中检测到错误,只需使用您的错误消息创建一个变量并再次显示该页面。然后页面将检查每个显示是否有消息。如果是,则显示它。

      if (strlen($security_code) == 0) {
              $message = "Security code was wrong";
              require(page.php) //Call your view
      }
      

      然后在你的页面中:

      <div id="stylized" class="leftsidebox">
         <?php if(isset($message)){ echo $message; } ?>
      </div>
      

      如果您想避免重新加载页面,我建议您研究 AJAX。取而代之的是,您在提交表单时会收到一个 ajax 请求(如果有返回错误),并且您的错误消息将通过使用 javascript 操作 DOM 来异步显示。

      【讨论】:

        【解决方案3】:

        你可以像这样将该脚本放在 div 标签内:

        <div id="stylized" class="rightsidebox">
            <div id="stylized" class="feedback">
                <h1>
        
         <?php
        
        include('connection.php');
        
        //retrieve input values from the form
        $security_code = $_REQUEST['securitycode'];
        
        if (strlen($security_code) == 0) {
            echo "Please enter a security code.";
        }
        ?>
                </h1>
            </div>
        
        </div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-20
          • 2019-08-26
          • 1970-01-01
          相关资源
          最近更新 更多