I am trying to create a short form in HTML consisting of a list box, a comment box and a submit button. The form allows a person to select up to 7 items from the list box and then leave a comment about his/her selection. The items of the list box themselves come from a XML file that is located on the same server as the html, php, js, and css files are.
Populating the list box from the XML file and the rest of the form work fine. But I run into an issue as soon as I try to use the list box in combination with selectize.js. Selectize.js itself is working and I can alter the list box, define the maximum of items which can be selected etc. But the options of the list box are missing all of the sudden. It is as if the items of the XML file are not loaded correctly anymore. And as soon as I disable selectize.js, the options are back.
I apologize in advance if this is a very basic or even repeated question. I am not very good with jQuery and scrambled the code together from various sources. I also tried to find help by googling similar issues for about a day but so far had no luck. So any help is greatly appreciated.
Here's my code.
index.php
<?php
if($_POST['submit'] == "Submit") {
$errorMessage = "";
if(empty($_POST['select-songs'])) {
$errorMessage .= "<li>You forgot to select a song!</li>";
}
$log = "";
foreach ($_POST['select-songs'] as $song) {
$log .= $song . ",";
}
$log .= $_POST['comment'];
// $songs = $_POST['select-songs'];
// $comment = $_POST['comment'];
if(!empty($errorMessage)) {
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
} else {
$fs = fopen("mydata.txt","a");
fwrite($fs,$log . "\n");
fclose($fs);
header("Location: thankyou.html");
exit;
}
}
?>
<html>
<head>
<title>Fav5 Song Selector</title>
<meta charset="UTF-8">
<!-- jQuery libraries -->
<script src="libraries/jquery-3.2.0.min.js"></script>
<script src="libraries/selectize.min.js"></script>
<!-- scripts -->
<script language="javascript" type="text/javascript" src="ui.js"></script> <!-- include the reload.js file -->
<!-- stylesheets -->
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="selectize.css">
</head>
<body>
<h1>Fav5 Demo UI</h1>
<form action="index.php" method="post">
<select id="songLib" name="songLib[]" class="demo-default">
<option value="" disabled selected hidden>Select a maximum of 7 songs from your playlist</option>
</select>
</br>
<textarea name="comment" rows="5 "cols="25"></textarea>
</br>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
ui.js
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'songs.xml',
dataType: 'xml',
success: function(xml){
$(xml).find('song').each(function() {
$('#songLib').append(
'<option>' +
$(this).find('title').text() +
'</option>'
);
});
}
});
$('#songLib').selectize({
delimiter: ',',
persist: false,
maxItems: 7
});
});