【问题标题】:Output in a form format that can be submitted after reading from a text file以从文本文件读取后可以提交的表单格式输出
【发布时间】:2013-04-20 09:38:46
【问题描述】:

我有以下文本文件:

$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';

以下代码以表单格式读取文本文件和输出,并带有下拉菜单以选择多个问题。这就是我正在尝试的方式,

<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 文件。谁能帮我解决这个问题?

【问题讨论】:

    标签: html output submission forms http-post


    【解决方案1】:

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

    echo "<select>";
    

    到:

    echo "<select name='answers[]'>";
    

    在 processor.php 文件中,您将能够选择所有答案作为数组:

    var_dump($_POST['answers'])
    

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

    <input type="text" name="question" value="<?
    

    【讨论】:

    • 感谢您的洞察力,我似乎​​不知道它将如何与我的代码集成,请您协助我进行集成。因为在输入您突出显示的更正后,我仍然会遇到相同的错误。谢谢。
    • 它会删除我之前创建的表单并显示一个扭曲的表单,其中部分代码显示在浏览器上。我不知道如何将其链接到此处以供您查看确切的输出,但这只是对我尝试在本地服务器上运行它时发生的情况的解释
    • 可以上传截图吗?
    • 我有截图,这可能是幼稚的,但我不知道在这个网站上的哪里上传。
    • 我还没有上传截图的资格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2019-11-15
    • 1970-01-01
    • 2013-10-25
    相关资源
    最近更新 更多