Generate random string in .tpl file

时间:2018-01-23 19:20:00

标签: php html validation smarty

I have the following input field:

<input name="domain" value="{$domain}" size="50" 
data-hostname-check="{$options}" 
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.

1 个答案:

答案 0 :(得分:2)

It sounds like you're looking for a simple PHP echo in the placeholder attribute.

I've gone with substr(md5(mt_rand()), 0, 7) to generate a random lowercase alphabetic string in the following, but feel free to adapt to suit:

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

Hope this helps :)