2

I have a page to create a new user. I have a php script that does various checks on the data entered in text fields. When it finds non-conforming entries, I need to provide feedback to the user to state why the process has been terminated. My thought was to simply have an empty div that I would populate with a message specific to the failing condition.

However, it looks like this might be more difficult than I had thought. I'd prefer to to this in my php script. I'm pretty new to web development. I'm open to suggestions about the best / easiest way to accomplish this. Thanks.

<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();
    }
?>
4

3 回答 3

3

在这里,您可以在 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>
于 2013-06-28T20:48:46.117 回答
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 来异步显示。

于 2013-06-28T20:48:08.757 回答
0

您可以像这样将该脚本放在 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>
于 2013-06-28T20:47:06.090 回答