PHP表单输入随机文字

怎样把这些随机文字输入到隐藏表单里?
<?php
function genRandomString($len)
{
$chars = array(

"一",
"二",
"三",
"四",
"五",
'六',
'七',
'八',
'九',
"十"
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = "";
for ($i=0; $i<$len; $i++)
{
$rand = $chars[mt_rand(0,$charsLen)];
if(stripos($output,$rand)=== false)
{
$output .= $rand;
}else
{
$i--;
}
}
return $output;
}
$str = genRandomString(1);
echo $str;
?>

<!--在html中加入隐藏的css属性-->
<form style="display:none;" name="form" method="post" action="">
  <input type="text" name="string" value="<?=$str?>" />
</form>

追问

可以放原整的源码吗> <?

追答

一般为了页面的整洁和可读性,不会在html代码中加入过于复杂的php脚本,只会将保存结果的变量在页面输出,或嵌套循环。如果你非要显示的话也可以。

 <input type="text" name="string" value="<?php 
 #PHP脚本
 ?>" />

追问

第一个方法行通了可是怎把顶部我的PHP隐藏掉><?

追答#可以将顶部的php重新写入到一个文件中,如:one.php。在form中将其包含就行
<?php include(dirname(__FILE__)."/one.php"); ?>
<form>...</form>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-16

rand.php

<?php
function genRandomString($len){
    $chars = array( "一","二","三","四","五",'六','七','八','九',"十");
    for($i=0;$i<$len;$i++){
        $index = array_rand($chars);
        $output.= $chars[$index];
    }
    return $output;
}
?>


在页面中输出

<?php require 'rand.php'; ?>
<input type="text" name="string" value="<?php echo genRandomString(5); ?>" />

相似回答