Can't get data from pulldown menu into $_POST

Hi,
I have a table which contains several forms. I can’t get the selected data in the second pull-down menu, which has a name of ‘annual’, into $_POST. Other values go into $_POST fine, such as ‘exam’, ‘last’ and ‘first’.

<table>
                        <tr>    <td>
                              <form action="?<?php htmlout($action);?>" method="post">
                              <!-- <input type="hidden" name="action" value="<?php htmlout($id); ?>" /> -->
                              <input type="hidden" name="action" value="hauptFormular" />
                              <input type="submit" value="<?php htmlout($button); ?>" />
                            </td>
                        </tr>
                        
                        <tr>
                            <td>
                                <fieldset>
                                     <legend>By class</legend>
                                     <table class="inner" id="tableByclass">
                                         <tr class="topRow">
                                             <td>Exam type:</td>
                                             <td class="left">                         
                                                <select name="exam" id="exam" style="background-color: #FFDDF4">
                                                    <option></option>
                                                     <?php foreach($exams as $key=>$option):
                                                           echo "<option value='$key'>$option</option>";
                                                      endforeach; ?>
                                                </select></td>
                                             <td>Year:</td><td name="yearid" id="yearid" class="left">
                                                 <select name="annual" id="annual" 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>
                                             </td>
                                             <td>Subject:</td><td id="subject" class="left"><td>
                                             
                                         </tr>
                                         <tr>
                                             <td class="left">First name:</td><td><input type="textarea" name="first" value=""></td>
                                             <td class="left">Last name:</td><td><input id="add" type="textarea" name="last" value=""></td>
                                             <td class="left">Result:</td><td id="result" class="left"></td>
                                         </tr> 
                                     </table>
                                 </fieldset>
                             </td>
                        </tr>
                     </table>
              </div>
              </form>

Here is the controller file which at the moment only echoes $_POST.

if (isset($_POST['action']) and $_POST['action'] == 'hauptFormular')
{
  include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/db.inc.php';
 try
  {
      echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    /*$sql = 'INSERT INTO halbForm SET 
        exam = :exam,
        first = :first,
        last = :last';
                $s = $pdo->prepare($sql);
                $s->bindParam(':exam', $_POST['exam']);
                $s->bindParam(':first', $_POST['first']);
                $s->bindParam(':last', $_POST['last']);
                $s->execute();*/
  }
  //exam subject year session first last
  catch (PDOException $e)
  {
    $error = 'Error inserting into halbForm.';
    include 'error.html.php';
    exit();
  }
  //include 'input.html.php';
  //header('Location: .?input');
  exit();
}

The post output is as follows,

Array
(
    [action] => hauptFormular
    [exam] => KET
    [first] => kkeett
    [last] => keeeetttttt
)

The $_POST array contains values for ‘exam’, ‘first’ and ‘last’ but no value for ‘annual’.
I have been looking at this for a few days and just can’t see what’s wrong.
Any help here would be greatly appreciated.
Thanks,
Shane

I don’t see any mention of ‘annual’ in your controller script at all.

1 Like

Hi WebMachine,
Correct there is no mention of ‘annual’ but it should still go into $_Post like ‘exam’ ‘first’ and’last’. These are not really mentioned in index.php either because the line that mention them are in a commented out block.
I am obviously doing something really stupid but I can’t see it.
Thanks,

I’m having problems duplicating your form to try it out myself, because there is an issue with the nesting of elements

  • for example the <table> and <form> elements.
1 Like

Yes I see the problem there, the opening form tag is inside the table but closes outside the table tags. I know it is not ideal and I’ll fix it when I get home but does this actually effect fuctionality.
Sorry I can’t fix it now but thanks for looking at this.
Shane

I think many browsers are fairly good at “fixing” invalid mark-up eg. inserting missing closing tags, ignoring “extra” tags, etc.

But it can’t be relied upon that all browsers will fix the mark-up the way you would like them to.

In fact, I’d go as far to say that the vast majority of the time it is a pointless waste of time to try and debug code until the mark-up is valid.

1 Like

Ok thanks I need to stop being sloppy!
“In fact, I’d go as far to say that the vast majority of the time it is a pointless waste of time to try and debug code until the mark-up is valid.” Well then that’s the next step.
Thanks

lol

You don’t need to stop being sloppy, (I know I haven’t) only need to catch it and un-slop :wink:

1 Like

I tried fixing your html and replaced any dynamically generated values (ie the ‘exam’ dropdown values) with dummy data and did a var_dump on the $_POST variable.

This is what I got:

array(5) {
  ["action"]=>
  string(13) "hauptFormular"
  ["exam"]=>
  string(1) "A"
  ["annual"]=>
  string(4) "2013"
  ["first"]=>
  string(4) "John"
  ["last"]=>
  string(3) "Doe"
}

So I’m getting the value of your ‘annual’ okay.

2 Likes

Hi,
I fixed up the html and it is working now.
So that must have been it.

<!DOCTYPE html>
<?php     
    include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/func.inc.php'; 
    include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/helpers.inc.php'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/arraybuilder.inc.php'; 
?>
<html lang="en">
  <head>
    <link type="text/css" rel="stylesheet" href="/cuislegibney/css/stylesheet.css"/>
    <meta charset="utf-8">
    <title>Results Input</title>
  </head>
  <body>
  <link rel="icon" type="image/x-icon" href="http://www.cuislegibney.com/cuislegibney/images/favicon.ico"/>
  <header>
      <h1>Results archive</h1>
  </header>
            <div class="input">
                  <form action="?<?php htmlout($action);?>" method="post">
                  <input type="hidden" name="action" value="hauptFormular" />
                  <input type="submit" value="<?php htmlout($button); ?>" />
                  <table>
                        <tr>
                            <td>
                                <fieldset>
                                     <legend>By class</legend>
                                     <table class="inner" id="tableByclass">
                                         <tr class="topRow">
                                             <td>Exam type:</td>
                                             <td class="left">                         
                                                <select name="exam" id="exam" style="background-color: #FFDDF4">
                                                    <option></option>
                                                     <?php foreach($exams as $key=>$option):
                                                           echo "<option value='$key'>$option</option>";
                                                      endforeach; ?>
                                                </select></td>
                                             <td>Year:</td><td name="yearid" id="yearid" class="left">
                                                 <select name="annual" id="annual" 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>
                                             </td>
                                             <td>Subject:</td><td id="subject" class="left"><td>
                                             
                                         </tr>
                                         <tr>
                                             <td class="left">First name:</td><td><input type="textarea" name="first" value=""></td>
                                             <td class="left">Last name:</td><td><input id="add" type="textarea" name="last" value=""></td>
                                             <td class="left">Result:</td><td id="result" class="left"></td>
                                         </tr> 
                                     </table>
                                 </fieldset>
                             </td>
                        </tr>
                     </table>
                     </form>
              </div>
             <script>                
                    function examHandler() {
                        var subjectArray = [];
                        var a = true;
                        document.getElementById('subject').innerHTML = "";
                        if(exam.value=="AS"){
                            subjectArray = <?php echo json_encode($ASsubjects, JSON_PRETTY_PRINT); ?>;        
                        }else if(exam.value=="A2"){
                            subjectArray = <?php echo json_encode($A2subjects, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="AP"){
                            subjectArray = <?php echo json_encode($APsubjects, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="KET"||exam.value=="PET"||exam.value=="FCE"||exam.value=="IELTS"){
                            document.getElementById('subject').innerHTML = "English";
                            a = false;
                        }
                            if(a){
                                var strOption = "";
                                subjectArray.forEach(function(item){
                                    strOption += "<option value='test'>" + item + "</option>";
                                    });
                                selectOption = "<select style='background-color: #FFDDF4'>" + strOption + "</select>";
                                document.getElementById('subject').innerHTML = selectOption;
                            }
                        };
                        
                        function resultHandler() {
                        var resultArray = [];
                        if(exam.value=="AS"|| exam.value=="A2"){
                            resultArray = <?php echo json_encode($GCEresults, JSON_PRETTY_PRINT); ?>;        
                        }else if(exam.value=="AP"){
                            resultArray = <?php echo json_encode($APresults, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="KET"||exam.value=="PET"){
                            resultArray = <?php echo json_encode($KETPETresults, JSON_PRETTY_PRINT); ?>;/*KET and PET results are same*/
                        }else if(exam.value=="FCE"){
                            resultArray = <?php echo json_encode($FCEresults, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="IELTS"){
                            resultArray = <?php echo json_encode($IELTSresults, JSON_PRETTY_PRINT); ?>;
                        }
                            document.getElementById('result').innerHTML = "";
                            var strOption = "";
                            resultArray.forEach(function(item){    
                                strOption += "<option value='result'>" + item + "</option>";
                                });
                            selectResultOption = "<select style='background-color: #FFDDF4'>" + strOption + "</select>";
                            document.getElementById('result').innerHTML = selectResultOption;
                        };
                        
                        function yearHandler() {
                            var yearArray = [];
                            yearArray = <?php echo json_encode($years, JSON_PRETTY_PRINT); ?>;        
                            document.getElementById('yearid').innerHTML = "";
                            var strOption = "";
                            yearArray.forEach(function(item){    
                                strOption += "<option value='" + item + "'>" + item + "</option>";
                                });
                            selectYearOption = "<select name='year' style='background-color: #FFDDF4'>" + strOption + "</select>";
                            console.log(selectYearOption);
                            document.getElementById('yearid').innerHTML = selectYearOption;
                        }
                        
                        var i = 1;
                        function classAdder(){
                            var table = document.getElementById("tableByclass");
                            var row = document.createElement("tr");    
                            
                            var cell = document.createElement("td");
                            var empty = document.createTextNode("");
                            cell.appendChild(empty);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            var detail = document.createElement("input");
                            detail.type = "text";
                            detail.name = "firstname";
                            cell.appendChild(detail);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            var last = document.createTextNode("");
                            cell.appendChild(empty);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            var detail = document.createElement("input");
                            
                            /*var detail = document.getElementsByTagName("input")[1];*/
                            
                            detail.type = "text";
                            detail.name = "lastname";
                            cell.setAttribute("id", "idNumber");
                            cell.appendChild(detail);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            
                            /*console.log(selectResultOption);*/
                            
                            var empty = document.createTextNode("");
                            cell.appendChild(empty);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            cell.setAttribute("class", "left");
                            cell.innerHTML = selectResultOption;
                            row.appendChild(cell);
                            
                            table.appendChild(row); 
                            i+=2;
                            document.getElementsByTagName("input")[i].onchange = classAdder;
                        }

                        function callFunctions(){
                            examHandler();
                            /*resultHandler();*/
                            yearHandler();
                        };
                        
                    document.getElementById('exam').onchange = callFunctions;
                    document.getElementsByTagName("input")[1].onchange = classAdder;
                </script> 
          </body>
</html>

Now I have to try to get it to work where the select menu is dynamically created as a DOM element. But that’s a JavaScript problem, so I wil not discuss it here.
Thanks for your help,
Shane

1 Like

All working now, great and thanks. Here is the code in case it is of any use to anyone.
The HTML and JS script,

<!DOCTYPE html>
<?php     
    include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/func.inc.php'; 
    include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/helpers.inc.php'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/arraybuilder.inc.php'; 
?>
<html lang="en">
  <head>
    <link type="text/css" rel="stylesheet" href="/cuislegibney/css/stylesheet.css"/>
    <meta charset="utf-8">
    <title>Results Input</title>
  </head>
  <body>
  <link rel="icon" type="image/x-icon" href="http://www.cuislegibney.com/cuislegibney/images/favicon.ico"/>
  <header>
      <h1>Results archive</h1>
  </header>
            <div class="input">
                  <form action="?<?php htmlout($action);?>" method="post">
                  <input type="hidden" name="action" value="hauptFormular" />
                  <input type="submit" value="<?php htmlout($button); ?>" />
                  <table>
                        <tr>
                            <td>
                                <fieldset>
                                     <legend>By class</legend>
                                     <table class="inner" id="tableByclass">
                                         <tr class="topRow">
                                             <td>Exam type:</td>
                                             <td class="left">                         
                                                <select name="exam" id="exam" style="background-color: #FFDDF4">
                                                    <option></option>
                                                     <?php foreach($exams as $key=>$option):
                                                           echo "<option value='$key'>$option</option>";
                                                      endforeach; ?>
                                                </select></td>
                                             <td>Year:</td><td name="yearid" id="yearid" class="left">
                                                 <!-- <select name="annual" id="annual" 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> -->
                                             </td>
                                             <td>Subject:</td><td id="subject" class="left"><td>
                                             
                                         </tr>
                                         <tr>
                                             <td class="left">First name:</td><td><input type="textarea" name="first" value=""></td>
                                             <td class="left">Last name:</td><td><input id="add" type="textarea" name="last" value=""></td>
                                             <td class="left">Result:</td><td id="result" class="left"></td>
                                         </tr> 
                                     </table>
                                 </fieldset>
                             </td>
                        </tr>
                     </table>
                     </form>
              </div>
             <script>                
                    function examHandler() {
                        var subjectArray = [];
                        var a = true;
                        document.getElementById('subject').innerHTML = "";
                        if(exam.value=="AS"){
                            subjectArray = <?php echo json_encode($ASsubjects, JSON_PRETTY_PRINT); ?>;        
                        }else if(exam.value=="A2"){
                            subjectArray = <?php echo json_encode($A2subjects, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="AP"){
                            subjectArray = <?php echo json_encode($APsubjects, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="KET"||exam.value=="PET"||exam.value=="FCE"||exam.value=="IELTS"){
                            document.getElementById('subject').innerHTML = "English";
                            a = false;
                        }
                            if(a){
                                var strOption = "";
                                subjectArray.forEach(function(item){
                                    strOption += "<option value='test'>" + item + "</option>";
                                    });
                                selectOption = "<select style='background-color: #FFDDF4'>" + strOption + "</select>";
                                document.getElementById('subject').innerHTML = selectOption;
                            }
                        };
                        
                        function resultHandler() {
                        var resultArray = [];
                        if(exam.value=="AS"|| exam.value=="A2"){
                            resultArray = <?php echo json_encode($GCEresults, JSON_PRETTY_PRINT); ?>;        
                        }else if(exam.value=="AP"){
                            resultArray = <?php echo json_encode($APresults, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="KET"||exam.value=="PET"){
                            resultArray = <?php echo json_encode($KETPETresults, JSON_PRETTY_PRINT); ?>;/*KET and PET results are same*/
                        }else if(exam.value=="FCE"){
                            resultArray = <?php echo json_encode($FCEresults, JSON_PRETTY_PRINT); ?>;
                        }else if(exam.value=="IELTS"){
                            resultArray = <?php echo json_encode($IELTSresults, JSON_PRETTY_PRINT); ?>;
                        }
                            document.getElementById('result').innerHTML = "";
                            var strOption = "";
                            resultArray.forEach(function(item){    
                                strOption += "<option value='result'>" + item + "</option>";
                                });
                            selectResultOption = "<select style='background-color: #FFDDF4'>" + strOption + "</select>";
                            document.getElementById('result').innerHTML = selectResultOption;
                        };
                        
                        function yearHandler() {
                            var yearArray = [];
                            yearArray = <?php echo json_encode($years, JSON_PRETTY_PRINT); ?>;        
                            document.getElementById('yearid').innerHTML = "";
                            var strOption = "";
                            yearArray.forEach(function(item){    
                                strOption += "<option value='" + item + "'>" + item + "</option>";
                                });
                            selectYearOption = "<select name='annual' style='background-color: #FFDDF4'>" + strOption + "</select>";
                            console.log(selectYearOption);
                            document.getElementById('yearid').innerHTML = selectYearOption;
                        }
                        
                        var i = 1;
                        function classAdder(){
                            var table = document.getElementById("tableByclass");
                            var row = document.createElement("tr");    
                            
                            var cell = document.createElement("td");
                            var empty = document.createTextNode("");
                            cell.appendChild(empty);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            var detail = document.createElement("input");
                            detail.type = "text";
                            detail.name = "firstname";
                            cell.appendChild(detail);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            var last = document.createTextNode("");
                            cell.appendChild(empty);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            var detail = document.createElement("input");
                            
                            /*var detail = document.getElementsByTagName("input")[1];*/
                            
                            detail.type = "text";
                            detail.name = "lastname";
                            cell.setAttribute("id", "idNumber");
                            cell.appendChild(detail);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            
                            /*console.log(selectResultOption);*/
                            
                            var empty = document.createTextNode("");
                            cell.appendChild(empty);
                            row.appendChild(cell);
                            
                            cell = document.createElement("td");
                            cell.setAttribute("class", "left");
                            cell.innerHTML = selectResultOption;
                            row.appendChild(cell);
                            
                            table.appendChild(row); 
                            i+=2;
                            document.getElementsByTagName("input")[i].onchange = classAdder;
                        }

                        function callFunctions(){
                            examHandler();
                            resultHandler();
                            yearHandler();
                        };
                        
                    document.getElementById('exam').onchange = callFunctions;
                    document.getElementsByTagName("input")[1].onchange = classAdder;
                </script> 
          </body>
</html>

The index.php

if (isset($_POST['action']) and $_POST['action'] == 'hauptFormular')
{
  include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/db.inc.php';
 try
  {
      echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    $sql = 'INSERT INTO halbForm SET 
        exam = :exam,
        year = :year,
        first = :first,
        last = :last';
                $s = $pdo->prepare($sql);
                $s->bindParam(':exam', $_POST['exam']);
                $s->bindParam(':year', $_POST['annual']);
                $s->bindParam(':first', $_POST['first']);
                $s->bindParam(':last', $_POST['last']);
                $s->execute();
  }
  //exam subject year session first last
  catch (PDOException $e)
  {
    $error = 'Error inserting into halbForm.';
    include 'error.html.php';
    exit();
  }
  //include 'input.html.php';
  header('Location: .?input');
  exit();
}

Thanks again,
Shane

Great! I’m glad you got it sorted out.

1 Like

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