在PHP Smarty模板中创建随机字符串

问题描述 投票:1回答:1

我有以下输入字段:

{assign var="name" value="test12345.example.com"}

<input name="domain" value="{$domain}" size="50" 
data-hostname-check="{$options}"
placeholder="<?php echo substr(md5(mt_rand()), 0, 7); ?>"
onchange="if (
typeof simulateCart == 'function' && this.checkValidity())
simulateCart.call(this);"/>

我需要的是为未填充的输入字段生成随机字符串。但我不知道如何做到这一点。此输入字段位于使用smarty模板的if循环中。

目前需要onchange,所以如果我删除它你可以将输入字段留空但是在我们可以将它插入数据库之前应该有一个标签

目前我有一个var(静态),我可以用它来占位符文本。我试过的回声不起作用。也许有人可以帮我解决这个问题。如果我把它放在占位符中,将显示“”中的整个文本。

另一个问题是表单有输入验证,你必须按一个按钮。我知道有一个jQuery on.click如何在我的情况下使用它。

php jquery forms validation smarty
1个回答
0
投票

你可以编写smarty插件,输出随机字符串。

请参阅https://www.smarty.net/docs/en/plugins.functions.tpl中的“带输出的函数插件”一章

您只需要使用您的功能创建文件

function smarty_function_randomstring($params, Smarty_Internal_Template $template) {
    return substr(md5(mt_rand()), 0, 7);
}

Ant把它放在插件目录https://www.smarty.net/docs/en/variable.plugins.dir.tpl

然后你就可以像这样使用它

<input placeholder =“{randomstring}”

© www.soinside.com 2019 - 2024. All rights reserved.