Pull in random form for testing
I would like some help in starting this project, some advice on where to look. On one of my sites we have a jQuery Tools powered overlay that holds a contact form. The form is located in the /forms directory. I would like to create several (5-6) forms that have different layouts and test which works best with our users.
I think that creating a "control" file in php, and linking to it instead of directly to a form to pull in a random form would work.
I am not sure about the code, but know that I need to: - read the directory with opendir and readdir? - add FILES to an array (use is_dir ) - generate a random number between 0 and the count() of the array - include the specified random file. - call this function on the click event of the link either inline or jQuery
Would I do this in a separate file? Any advice is much appreciated. Let m开发者_运维技巧e know if I need to clarify or add more details.
Thank you.
Sounds like you definitely have the right idea. This should be about right, almost exactly as you outlined it. Unless you have a need to load this up with jQuery after initial page load, I'd recommend doing it in the PHP during initial page load.
adapted from readdir()
, example 2:
// load random form
$fileList = array();
if ($handle = opendir('./forms')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileList[] = $file;
}
}
closedir($handle);
}
$randIndex = array_rand($fileList);
include './forms/' . $fileList[$randIndex];
Note: This code assumes that the forms directory only contains form files and no other files or directories that need to be excluded.
Also, if you need to capture the file's output in a string rather than print it directly to the browser, see include
, example 6.
精彩评论