简体   繁体   English

如何在表单中缓存用户输入

[英]How to cache user input in form

I'm trying to cache the user input in my form. 我正在尝试将用户输入缓存在表单中。 My code is: 我的代码是:

    echo '
<form method="POST">
    Name:<br /><input class="contact_name" type="text" name="contact_name" maxlength="32" placeholder="Enter Name" /><br />
    Email:<br /><input class="contact_email" type="text" name="contact_email" maxlength="50" placeholder="Email Address" /><br />
    Subject:<br /><input class="contact_subject" type="text" name="contact_subject" maxlength="50" placeholder="Subject Line" /><br />
    Message:<br /><textarea class="message_area" name="contact_message" rows="10" cols="50" maxlength="1000" placeholder="Message ..." /></textarea><br />
    <input class="submit_button" name="submit_button" type="submit" value="Send" />
</form>
';

I tried searching for the answer and the only thing I found was adding: 我尝试搜索答案,唯一发现的是添加:

<?php if(isset($contact_name)) { echo $contact_name; } ?>

This however does not work for me as my form is within a PHP echo and I'm trying to make a basic wordpress plugin. 但是,这对我不起作用,因为我的表单在PHP echo中,并且我正在尝试制作一个基本的wordpress插件。 Whenever I bring the form outside the echo and , the style messes up and the form style itself breaks. 每当我将表单带到echo和之外时,样式就会混乱并且表单样式本身会中断。 So I was wondering if I can keep my form inside my echo along with a placeholder and be able to cache user inputs so when an error displays cause they didn't fill one of the spots out, it won't erase everything. 因此,我想知道是否可以将我的表单与占位符一起保留在回声中并能够缓存用户输入,以便当错误显示导致他们没有填补其中一个斑点时,它不会擦除所有内容。

Thank you. 谢谢。

Then, just drop the echo and switch to HTML mode: 然后,只需echo并切换到HTML模式:

?>
<form method="post">
Name:<br /><input ... value="<?php echo (isset($contact_name) ? htmlspecialchars($contact_name, ENT_QUOTES, 'UTF-8') : ''; ?>" />
...
<?php

If you need this as a string, you could use output buffering: 如果需要将此作为字符串,则可以使用输出缓冲:

ob_start();
?>
<input ... />
<?php

echo ob_get_clean();

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

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