Getting data into DB from JS generated select menu

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

Hi,
I changed the yearHandler() function so that it creates a string which is actually a php foreach() loop.
I know that there are issues with double and single quotes here.

I have changed the id of the original element on the TML page to id=‘yearid’

function yearHandler() {        
                            document.getElementById('yearid').innerHTML = "";
                            var strOption = "<?php foreach($years as $key=>$option):
                                                           $selected = ($year == $option) ? 'selected' : '';
                                                           echo "<option value='$option' $selected>$option</option>";
                                                      endforeach; ?>";
                            selectYearOption = "<select name='year' id='year' style='background-color: #FFDDF4'>" + strOption + "</select>";
                            console.log(selectYearOption);
                            document.getElementById('yearid').innerHTML = selectYearOption;
                            };

Despite the fact that the double quotes should give problems,
console.log(selectYearOption);
looks fine,

"<select name='year' id='year' style='background-color: #FFDDF4'><option value='2015' >2015</option><option value='2014' >2014</option><option value='2013' >2013</option><option value='2012' >2012</option><option value='2011' >2011</option><option value='2010' >2010</option><option value='2009' >2009</option><option value='2008' >2008</option></select>"

Thanks,
Shane

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.