【问题标题】:On PHP form, drop down list displays wrong data from .txt file在 PHP 表单上,下拉列表显示 .txt 文件中的错误数据
【发布时间】:2016-03-08 00:50:57
【问题描述】:

我查看了有关此的相关帖子,并看到一个现在让我认为我做错了。我对 PHP 非常非常陌生,但愿意学习。

我需要一个 index.php 表单,其中包含学生姓名输入框、学生编号输入框和课程下拉列表。

我的 PHP 脚本不允许包含数据 - 必须从非可选的 .txt 文件中检索数据。

我有 course.txt,其中包含四个课程名称,每个课程名称都有自己独特的课程代码和可以注册的最大人数。课程内容.txt: 动画电影设计:AFD-250:6 数码雕塑:DS-410:4 动画史:HA-240:6 视觉效果:VE-298:4

我有 coursesfinal.txt 已转换为逗号分隔文件。 coursefinal.txt的内容: 动画电影设计,AFD-250,6 数码雕塑,DS-410,4 动画史,HA-240,6 视觉效果,VE-298,4

目前这两个输入框工作正常。我的问题是下拉列表。我希望它显示课程名称,然后是空格,然后是课程代码。此时它只显示课程代码。我也不明白为什么它显示第二个数据字段。

谢谢。

index.php 代码...

<?php
// Convert courses.txt file to comma delimited file coursesfinal.txt
 $in = "courses.txt";
 $out = "coursesfinal.txt";

 $IN = fopen ($in, 'r') or die ("$in cannot be opened for reading.");
 $OUT = fopen ($out, 'w') or die ("$out cannot be opened for writing.");

 if (flock($OUT, LOCK_EX)) {
    while ($inline = fgets ($IN) ) {
       $splitarray = explode (":", $inline);
       $outline = implode(",", $splitarray);
       fputs ($OUT, $outline);
    }
    flock($OUT, LOCK_UN);
 }

 fclose ($IN);
 fclose ($OUT);
 
// Search coursesfinal.txt file for course to match user input
 $datafile = "coursesfinal.txt";

 // If selection has been made, find a match
 if (isset ($_POST['courses'])) {
    $courses = strip_tags ($_POST['']);
    $DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading.");

    $found = FALSE;
    while ($record = fgets ($DB) and ! $found) {
       $field = explode (",", htmlentities (trim ($record)));
       $found = $courses === $field[0];
    }

    fclose ($DB);

    if ($found) {
       echo "<p>You have selected: $field[0] $field[1]</p>\n";
    }
 }






?>


<html>
<head>
<title>Registration Form</title>
<style>
body{background-color: #ffffe6; width:610px;}
h1 {color: #29a3a3;}
.inputbox {padding: 7px; border:  #F0F0F0 2px solid; border-radius: 4px;}
.btn {padding: 10px;background-color: #29a3a3; border: solid thin #000000; color: #FFF; font-weight: bolder; cursor: pointer;}
</style>
</head>

<body>
    <h1>Course Registration</h1>
    <form method="post" action="index.php">

        <fieldset><legend><strong>Student Information</strong></legend>
            <dl>
            <dt>Student Name:</dt>
            <dd><input class="inputbox" name="studentname" type="text" id="studentname" required autofocus placeholder="Please enter your first and last name" tabindex="10" size="50"></dd>
            <br>
            <br>
            <dt for="number">Student Number:</dt>
            <dd><input class="inputbox" name="studentnumber" type="text" required id="studentnumber" placeholder="Please enter using the following format: PX-03-046" tabindex="20" size="50"></dd>
            </dl>
            <br>
        </fieldset> 
        <br>
        
        <fieldset><legend><strong>Available Courses</strong></legend>  
        <br>  
        

Select a Course: <select name="course"> 
    <option value="-1" selected>Select From...</option> 
<?php
// Generate the form 
$DB = fopen ($datafile, 'r') or die ("$datafile cannot be opened for reading."); 
while ($record = fgets ($DB) ) { 
$field = explode (",", htmlentities (trim ($record))); 
echo " <option value=\"$field[0]\">$field[1]</option>\n"; 
} 
fclose ($DB); 

echo " </select>\n"; 


?>

    <br>
    <br>
    <br>
    <br>
    <br>
    <br>                               
    </fieldset>

    <div>
        <p>
            <input name="reset" type="reset" tabindex="40" value="Clear Form" class="btn">
            <input name="submit" type="submit" tabindex="50" value="Submit Form" class="btn">
        </p>
    </div>

</form>

</body>
</html>

【问题讨论】:

    标签: php forms dropdown


    【解决方案1】:

    查看您的以下代码:

    while ($record = fgets ($DB) ) { 
       $field = explode (",", htmlentities (trim ($record))); 
       echo " <option value=\"$field[0]\">$field[1]</option>\n"; 
    }
    

    如果你用一个参数调用fgets(),它会一直读取直到找到换行符或EOF(以先到者为准)。确保格式化 coursefinal.txt,例如:

    Animation Film Design,AFD-250
    6 Digital Sculpture,DS-410
    4 History of Animation,HA-240,
    6 Visual Effects,VE-298
    

    然后你就可以了

    while ($record = fgets ($DB) ) { 
       $field = explode (",", htmlentities (trim ($record))); 
       echo " <option value=\"$field[1]\">$field[0] $field[1]</option>\n"; 
    }
    

    注意explode() 带有逗号分隔符会将字符串“Animation Film Design,AFD-250”拆分为数组

    $field[0] == "Animation Film Design"
    $field[1] == "AFD-250"
    

    【讨论】:

    • 感谢您花时间帮助我。我的 coursesfinal.txt 文件中的数据与您的相似。四行,每行包含课程名称、课程代码和最大注册人数,名称、代码和最大人数之间用逗号分隔。
    • 我将我的代码修改为您提供的内容,它的显示完全符合我的要求。谢谢!谷歌搜索和 youtube 视频的时间 - 我找不到答案,但仍然学到了。
    • 在清理和验证方面,我应该对输入框和从下拉列表中选择的选项都这样做吗?
    • 使用上述建议更正我的代码后,下拉列表显示了所有四个选项。我将 1 行代码移到了我认为应该在的位置,现在列表只显示第 4 个选项。新代码[php]
      课程选择
      选择课程: [/php]
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多