简体   繁体   English

html表单输入值,不会回显php对象函数htmlspecialchars

[英]html form input value, will not echo php object function htmlspecialchars

I have implemented child classes to process user input inherited from a parent class which handles the database connections, read and write. 我实现了子类,以处理从父类继承的用户输入,该父类处理数据库的连接,读取和写入。

In my parent class I have a function called 'hsc' which handles 'htmlspecialchars' and the function filters user input and returns the string. 在我的父类中,我有一个名为“ hsc”的函数,该函数处理“ htmlspecialchars”,并且该函数过滤用户输入并返回字符串。

hsc function hsc功能

public function hsc($string) {
    return htmlspecialchars($string);
}

I am having a problem with my signup form. 我的注册表单有问题。 When I call the above function on the sign_up object, even before the form is submited the form breaks, then will only output the first label. 当我在sign_up对象上调用上述函数时,即使在提交表单之前,表单中断也将仅输出第一个标签。

output 输出

Your First Name (rest of the form is missing) 您的名字(表格的其余部分丢失)

problem code 问题代码

    value="<?php echo $sign_up->hsc($_POST['name']);?>" />

if I remove the above php code from value, the form displays correctly, also i can echo out text within the php tags, so the problem seems to be with the function? 如果我从值中删除了上面的php代码,则窗体可以正确显示,我也可以在php标签中回显文本,所以问题似乎出在函数上?

While trying to figure this out, am I using the correct approach, ie, using classes too validate user input? 在尝试弄清楚这一点时,我是否使用了正确的方法,即使用类也可以验证用户输入?

Hope someone can help 希望有人能帮忙

Thanks 谢谢

please see the code I am using for the form below 请在下面的表格中查看我使用的代码

    <?php
    include('./classes/signup_class.php');

    if(isset($_POST['submit'])) {
//require('./classes/signup_class.php');
try {   
        $sign_up = new Signup_User();
        $sign_up->processUserInput();
        $errors = $sign_up->getErrorMessages();

    }catch (Exception $e) {
        echo $e->getMessage();
    }

}
    ?>


    <form id="sign_up" method="post" action="">
<p>
<label for="name">Your First Name</label>
<input name="name" id="name" type="text" 
    value="<?php echo   $sign_up->hsc($_POST['name']);?>" />
</p>
<p>
<label for="surname">Your Last Name</label>
<input name="surname" id="surname" type="text" /> 
</p>
<p>
<label for="email">Your Email Address</label>
<input name="email" id="email" type="text"/>
</p>
<p>
<label for="emailconf">Confirm Your Email Address</label>
<input name="emailconf" id="emailconf" type="text"/>
</p>
<p>
<label for="gender">Your Gender</label>
<select class="gender_select" name="gender">
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</p>
<p>
<label for="password">Choose Your Password (8 characters)</label>
<input name="password" id="password" type="password">
</p>
<p>
<label for="passconf">Confirm Your Password</label>
<input name="passconf" id="passconf" type="password">
</p>
<input name="submit" id="submit" type="submit" value="signup" class="sign_up" >
    </form>

Here to have defined the class new Signup_User(); 这里要定义类new Signup_User(); in a condition that is true after submitting form. 在提交表格后为真的情况下。

try moving $sign_up = new Signup_User(); 尝试移动$sign_up = new Signup_User(); before that if condition:- 在此之前,如果条件:-

$sign_up = new Signup_User();

if(isset($_POST['submit'])) {
//require('./classes/signup_class.php');
try {   
        $sign_up->processUserInput();
        $errors = $sign_up->getErrorMessages();

    }catch (Exception $e) {
        echo $e->getMessage();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM