douken0530 2018-03-05 22:15
浏览 130

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

I have the following input field:

{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);"/>

What i need is to generate a random string for if the input field when its not filled. But I have no clue how to do this. This input field is in an if loop using smarty template.

The onchange is required at the moment so if I remove this you can leave the input field blank but there should be a label before we can insert it into the database

At the moment I have a var (static) whichh i can use for the placeholder text. The echo which i tried does not work. Maybe someone can help me fixing this. If i put this in the placeholder the whole text in the "" will be displayed.

Another issue is that the form has an input validation and you have to press on a button. I know there is an jQuery on.click how can i use this in my situation.

  • 写回答

1条回答 默认 最新

  • dsfsfdsf4544 2018-03-06 06:22
    关注

    You can write smarty plugin wich will output random string.

    See chapter "function plugin with output" here https://www.smarty.net/docs/en/plugins.functions.tpl

    You need only create file with you functuion

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

    Ant put it in plugin directory https://www.smarty.net/docs/en/variable.plugins.dir.tpl

    Then you can use it in smarty like this

    <input placeholder="{randomstring}"

    评论

报告相同问题?

悬赏问题

  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统