首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以表单格式输出,可在从文本文件读取后提交

以表单格式输出,可在从文本文件读取后提交
EN

Stack Overflow用户
提问于 2013-04-20 17:38:47
回答 1查看 155关注 0票数 0

我有以下文本文件:

代码语言:javascript
复制
$string = 
'1 The most important feature of spiral model is: requirement analysis. risk    management. quality management. configuration management 2 The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3 One of the fault base testing techniques is: unit testing. beta testing. Stress testing. mutation testing 4 A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5 RS is also known as: specification of White box testing. Stress testing. Integrated testing. Black box testing 6 what is you name: Collins. Achieng. Ogwethi. Kamanda';

以下代码以表单格式读取文本文件和输出,并使用下拉菜单选择多个问题。这就是我想要做的

代码语言:javascript
复制
<html>
<head>
<title>read</title>
</head>
<body>
    <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><br />
<?php
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$string = fread($openFile, filesize("questionandanswers.txt"));

//Regex to get from the first number to the string's end, so we ignore the Number     Question... bit;
preg_match('/\d.*/', $string, $match);

//We get all the strings starting with a number until it finds another number (which will be the beginning of another question;
preg_match_all('/\d\D*/', $match[0], $results);

$qas = array(); // We prepare an array with all the questions/answers
foreach($results[0] as $result){
//Separating the answer from the string with all the answers.
list($question, $all_answers) = explode(':', $result);

//Separating the different answers
$answers_array = explode('.', $all_answers);

//Stuffing the question and the array with all the answers into the previously prepared   array;
$qas[] = array('question' => $question, 'answers' => $answers_array);
}
?>
<form name="processor" action="processor.php" method="post">
<?php
//Looping through the array and outputting all the info into a form;
foreach($qas as $k => $v)?>
echo "<label>{$v['question']}</label><br/>";
echo "<select name='answers[]'>";

//we loop through $v['answers'] because its an array within the array with all the    answers.
foreach($v['answers'] as $answer)
{
    echo "<option>$answer</option>";//the output
}
echo "</select>";
echo "<br/><br/>";
?>
<input type="submit" value="Submit">
</form>
</body>
</html>

我希望能够将输出提交到另一个处理输出为$_POST的php文件。有谁能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2013-04-20 23:55:38

我认为你只需要改正这一行:

代码语言:javascript
复制
echo "<select>";

至:

代码语言:javascript
复制
echo "<select name='answers[]'>";

在processor.php文件中,您将能够以数组的形式选择所有答案:

代码语言:javascript
复制
var_dump($_POST['answers'])

但您应该重新考虑这一行,因为它似乎破坏了文档模型:

代码语言:javascript
复制
<input type="text" name="question" value="<?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16118810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档