0

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);
    }
}
4

2 回答 2

1

You could use array_rand:

$random_input = $_POST['friend'][array_rand($_POST['friend'])];
于 2012-10-08T22:32:20.450 回答
0
echo $_POST['friend'][ rand(0, count($_POST['friend']) - 1) ];
于 2012-10-08T22:32:42.777 回答