简体   繁体   中英

php echo random value from text fields

I want to post all text box responses to results.php and then randomly choose one of the responses to display.

the form:

<form action="results.php" method="post">
<input maxlength="30" name="friend[]" size="30" type="text" placeholder="Enter an option" />
<input maxlength="30" name="friend[]" size="30" type="text" placeholder="Enter an option" />
<input maxlength="30" name="friend[]" size="30" type="text" placeholder="Enter an option" />
<input maxlength="30" name="friend[]" size="30" type="text" placeholder="Enter an option" />
<input type="submit" value="Submit" />

the php contents (obviously wrong, but something like this?)

foreach ($_POST['friend'] as $value) {          
    if ($value) {
      echo mt_rand($value);
    }
}

您可以使用array_rand

$random_input = $_POST['friend'][array_rand($_POST['friend'])];
echo $_POST['friend'][ rand(0, count($_POST['friend']) - 1) ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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