I am trying to pass a JavaScript variable to a PHP variable but I am not certain how this can be done. My JavaScript is within a EOD tag. In particular I want my PHP code to access the chosenStudent variable from the javascript code.
Here is my code:
$Content3 = <<<EOD
<form id="myGroupSelectForm">
<select id="selectGroup">
<option>Choose a Group</option>
</select>
<select id="selectStudent">
<option>Choose a Student</option>
</select>
</form>
<script type="text/javascript">
var select = document.getElementById("selectGroup");
var options = {$js_array_leadersGroupsName};
var i;
for(i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
</script>
<script>
var studentList = {$js_array_students_lists};
var select2 = document.getElementById("selectStudent");
var a = document.getElementById('selectGroup');
a.addEventListener('change', function() {
var i;
for(i = 0; i < options.length; i++) {
if ((this.value) == options[i]) {
var chosenStudentList = studentList[i];
}
}
var select = document.getElementById("selectStudent");
var length = select.options.length;
for (i = length-1; i >= 0; i--) {
select.options[i] = null;
}
var i;
for(i = 0; i < chosenStudentList.length; i++) {
var opt = chosenStudentList[i][0];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select2.appendChild(el);
}
}, false);
</script>
<script>
var b = document.getElementById('selectStudent');
b.addEventListener('change', function() {
var chosenSudent = this.value;
}, false);
</script>
EOD;
$Content3 .="\n";
return $Content3;
Any pointers on how this could be done would be gratefully received.
****EDIT****
I have edited the code in an attempt to pass the variable to PHP via an AJAX call but I am still having issues.
<script src="jquery-3.5.1.min.js">
var b = document.getElementById('selectStudent');
b.addEventListener('change', function() {
var chosenSudent = this.value;
}, false);
$.post('WickCustomLD.php', {variable: chosenSudent});
</script>
EOD;
$Content3 .="\n";
return $Content3;
$chosenStudent = $_POST['variable'];
echo $chosenStudent;
Again, any pointers to help me get this working would be gratefully receieved.