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

发布于 2024-12-09 15:06:28 字数 2294 浏览 5 评论 0原文

我已经实现了子类来处理从父类继承的用户输入,该父类处理数据库连接、读取和写入。

在我的父类中,我有一个名为“hsc”的函数,它处理“htmlspecialchars”,该函数过滤用户输入并返回字符串。

hsc 函数

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

我的注册表单有问题。当我在sign_up对象上调用上述函数时,即使在提交表单之前,表单也会中断,然后只会输出第一个标签。

输出

你的名字(表单的其余部分丢失)

问题代码

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

如果我从值中删除上述 php 代码,表单显示正确,我也可以回显 php 标签内的文本,所以问题似乎出在函数上?

在试图解决这个问题时,我是否使用了正确的方法,即使用类来验证用户输入?

希望有人可以提供帮助

谢谢,

请参阅我在下面的表格中使用的代码

    <?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>

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 function

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.

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?

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单调的奢华 2024-12-16 15:06:28

这里定义了类 new Signup_User(); ,条件是提交表单后为 true。

尝试在 if 条件之前移动 $sign_up = new Signup_User(); :-

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

}

Here to have defined the class new Signup_User(); in a condition that is true after submitting form.

try moving $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();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文