PHP函数的返回值为html标记

时间:2016-08-27 05:07:54

标签: php html

我的Class.php内容:

<?php
class Class
{
    public function validate()
    {
        if(empty($_POST['first_name']))
        {
            return 'first name is required!';
        }
        elseif(empty($_POST['middle_name']))
        {
            return 'middle name is required!';
        }
        elseif(empty($_POST['last_name']))
        {
            return 'last name is required'
        }
        else {
            return 'success!';
        }
    }
}

现在在我的html文件中,我希望同时将my function validate的返回值显示为不同的html ID。

<?php
require 'Class.php';
$class = new Class();
$class->validate()
?>

<!DOCTYPE html>
<html>
<head>
    <title>SOME FORM</title>
</head>
<body>
    <form action="" method="POST">
        <input type="text" name="first_name">
        <span id="first_empty"></span> <!-- Return first name is required! -->

        <input type="text" name="middle_name">
        <span id="middle_empty"></span> <!-- Return middle name is required! -->

        <input type="text" name="last_name">
        <span id="last_empty"></span> <!-- Return last name is required! -->

        <input type="submit" value="Submit">
    </form>
</body>
</html>

我想同时显示所有3个回报。感谢

2 个答案:

答案 0 :(得分:1)

您需要稍微修改一下您的功能。

到目前为止,您将逐个返回验证消息。如果要将所有错误消息一起返回,则应创建一个数组。

遵循这种方法:

您的函数包含两个变量:

$ errors数组存储所有错误消息

$ response Array

“message” - &gt;适当的消息解释当前状态。

“错误” - &gt;包含错误列表。如果没有错误,它就为空。

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            $(document).dblclick(function (e)
                                {
                var container = $(".yui-calcontainer");
                if (!container.is(e.target) // if the target of the click isn't the container...
                    && container.has(e.target).length === 0) // ... nor a descendant of the container
                {
                    container.toggle();
                }
            });
        </script>
    </head>
    <body style="height:100% ; width:100%;";>

        <div id="cal1_0Container" style="clear: both; position: absolute; z-index: 9987;" class="yui-calcontainer single">
            Calendar Here
        </div>
    </body>
</html>

然后,调用该函数并检查错误:

 public function validate()
 {
    $response = array();
    $errors   = array();

    if (empty($_POST['first_name'])) {

        $errors['first_name'] = 'first name is required!';

    } elseif(empty($_POST['middle_name'])) {

        $errors['middle_name'] = 'middle name is required!';

    } elseif(empty($_POST['last_name'])) {

        $errors['last_name'] = 'last name is required'
    }

    if (empty($errors)) {
        $response['message'] = 'Success';
        $response['errors']  = null;
    } else {
         $response['message'] = 'Something went wrong';
         $response['errors']  = $errors;
    }
}

答案 1 :(得分:0)

试试这个:

(sum(findInterval(df$cont,c(min(df$cont[df$dich=='B']),max(df$cont[df$dich=='A'])))==1L)-1L)/nrow(df)*100;
## [1] 25