Hi,
I have this select menu with id=“exam”, surrounded by a form, in a HTML page,
<form action="?<?php htmlout($action);?>" method="post">
<input type="hidden" name="action" value="hauptFormular" />
............................
<select name="exam" id="exam" style="background-color: #FFDDF4">
<option></option>
<?php foreach($exams as $key=>$option):
$selected = ($exam == $key) ? 'selected' : '';
echo "<option value='$key' $selected>$option</option>";
endforeach; ?>
</select>
........................
</form>
This HTML page also has cell in a table with id=‘year’.
<td id="year" class="left"></td>
.onchange the javascript calls a function yearHandler()
document.getElementById('exam').onchange = callFunctions;
function callFunctions(){
examHandler();
resultHandler();
yearHandler();
};
function yearHandler() {
var yearArray = [];
yearArray = <?php echo json_encode($years, JSON_PRETTY_PRINT); ?>;
document.getElementById('year').innerHTML = "";
var strOption = "";
yearArray.forEach(function(item){
strOption += "<option value='year'>" + item + "</option>";
});
selectYearOption = "<select name='year' style='background-color: #FFDDF4'>" + strOption + "/<select>";
console.log(selectYearOption);
document.getElementById('year').innerHTML = selectYearOption;
};
This select menu has name=‘year’. The controller file index.php should take this value from the newly generated select menu and put it into the database. But this isn’t happening.
if (isset($_POST['action']) and $_POST['action'] == 'hauptFormular')
{
include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/db.inc.php';
try
{
//echo("mjbfkjr");
$sql = 'INSERT INTO halbForm SET
exam = :exam,
year = :year,
first = :first';
$s = $pdo->prepare($sql);
$s->bindValue(':exam', $_POST['exam']);
$s->bindValue(':year', $_POST['year']);
$s->bindValue(':first', $_POST['first']);
$s->execute();
}
//exam subject year session first last
catch (PDOException $e)
{
$error = 'Error inserting into halbForm.';
include '/cuislegibney/includes/error.html.php';
exit();
}
header('Location: .?input');
exit();
}
I can’t seem to get this year data into the database. But the ‘exam’ and ‘first’ data go in fine. (‘first’ is the name of an input form not shown in the HTML above.)
I am not sure if the JS generated menu should contain something like,
$selected = ($exam == $key) ? 'selected' : '';
And this might be the problem.
Any help would be greatly appreciated,
Thanks,
Shane