Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于php if语句在div中添加/更改文本_Php_Html_Web Applications - Fatal编程技术网

基于php if语句在div中添加/更改文本

基于php if语句在div中添加/更改文本,php,html,web-applications,Php,Html,Web Applications,我有一个页面来创建一个新用户。我有一个php脚本,可以对文本字段中输入的数据进行各种检查。当它发现不一致的条目时,我需要向用户提供反馈,说明流程终止的原因。我的想法是简单地拥有一个空div,我将使用特定于失败条件的消息填充该div 然而,看起来这可能比我想象的要困难。我更愿意在我的php脚本中使用它。我对网络开发相当陌生。我愿意接受关于实现这一目标的最佳/最简单方法的建议。谢谢 <div id="page"> <img id="mainpic" src="images/

我有一个页面来创建一个新用户。我有一个php脚本,可以对文本字段中输入的数据进行各种检查。当它发现不一致的条目时,我需要向用户提供反馈,说明流程终止的原因。我的想法是简单地拥有一个空div,我将使用特定于失败条件的消息填充该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添加动态文本的地方

您可以将该脚本放入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>

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

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,会异步显示错误消息。

这里,您可以在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>