在 javascript 列表中制作下拉列表中的选项 html 元素项?

<html>

<body>

<?php

//student interface

$file = fopen("subject.csv","r");

$subjects = fgetcsv($file);

fclose($file);

//puts the csv file provided into a 2d array

$studentTable = array();

if (($handle = fopen("students.csv", "r")) !== FALSE) {

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $studentTable[] = $data;

    }       fclose($handle);

}

//creates list that will store list of girls emails

$usernames = array();

//runs through studentTable and appends emails to toEmail. 

for ($I = 0; $I < sizeof($studentTable); $I++){

       array_push($usernames, $studentTable[$I][0]);

  }  


?>

<script type="text/javascript"> 

   

// Using PHP implode() function to turn the php list into a javascript list

var subjects = <?php echo '["' . implode('", "', $subjects) . '"]' ?>; 

var usernames = <?php echo '["' . implode('", "', $usernames) . '"]' ?>; 

var select = document.getElementById("usernames");

for(var i = 0; i < usernames.length; i++) {

    var opt = usernames[i];

    var el = document.createElement("option");

    el.textContent = opt;

    el.value = opt;

    select.appendChild(el);

}

var subchoice = [];

subchoice.push(document.getElementById("choice1"), document.getElementById("choice2"), document.getElementById("choice3"), document.getElementById("choice4"), document.getElementById("choice5"));

for(var x = 0; x < subchoice.length; x++) {

    for(var i = 0; i < subjects.length; i++) {

        var opt = subjects[i];

        var el = document.createElement("option");

        el.textContent = opt;

        el.value = opt;

        subchoice[x].appendChild(el);

    }

}

var six = [];

six = subjects;

six.push("null");

var select = document.getElementById("choice6");

for(var i = 0; i < six.length; i++) {

    var opt = six[i];

    var el = document.createElement("option");

    el.textContent = opt;

    el.value = opt;

    select.appendChild(el);

}

</script> 


我无法在下拉选项卡中显示任何内容。下拉列表应该是主题或用户名列表中的项目。但是,即使我认为我的编码正确,也没有任何反应。它给了我这个错误消息:“Uncaught SyntaxError: Unexpected token '<'” 我已经寻找这个额外的令牌但似乎找不到它,请帮忙!


qq_花开花谢_0
浏览 78回答 1
1回答

白猪掌柜的

<html><body><?php//student interface$file = fopen("subject.csv","r");$subjects = fgetcsv($file);fclose($file);//puts the csv file provided into a 2d array$studentTable = array();if (($handle = fopen("students.csv", "r")) !== FALSE) {&nbsp; &nbsp; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; $studentTable[] = $data;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;fclose($handle);}//creates list that will store list of girls emails$usernames = array();//runs through studentTable and appends emails to toEmail.&nbsp;for ($I = 0; $I < sizeof($studentTable); $I++){&nbsp; &nbsp; &nbsp; &nbsp;array_push($usernames, $studentTable[$I][0]);&nbsp; }&nbsp;&nbsp;?><h2> Select your subjects! </h2><form method="POST" action='handlestudents.php'><p> Select your name </p><select id='userN' name='userN'>&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected">Choose one</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($usernames as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?></select><p> Select your 1st choice subject </p>&nbsp; &nbsp; <select id='choice1' name='choice1'>&nbsp; &nbsp; <option selected="selected" >Choose a subject</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($subjects as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select><p> Select your 2nd choice subject </p>&nbsp; &nbsp; <select&nbsp; id='choice2' name='choice2'>&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" >Choose a subject</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($subjects as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select><p> Select your 3rd choice subject </p>&nbsp; &nbsp; <select id='choice3' name='choice3'>&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" >Choose a subject</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($subjects as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select><p> Select your 4th choice subject </p>&nbsp; &nbsp; <select&nbsp; id='choice4' name='choice4'>&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" >Choose a subject</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($subjects as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select><p> Select your 5th choice subject </p>&nbsp; &nbsp; <select id='choice5' name='choice5' >&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" >Choose a subject</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($subjects as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select><p> Select your 6th choice subject (none is an option) </p>&nbsp; &nbsp; <select id='choice6' name='choice6'>&nbsp; &nbsp; &nbsp; &nbsp; <option selected="selected" >Choose a subject</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; // Iterating through the product array&nbsp; &nbsp; &nbsp; &nbsp; foreach($subjects as $item){&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <option value = ""> None </option>&nbsp; &nbsp; </select><input type="submit" value="Submit"></form></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP