Form wrongly submitting when textarea is completed

Hi,
I have a form which adds a new textarea when the textarea has been filled. This was working fine until I added the form tags. Now when I complete filling in the textarea and hit enter, the form submits instead of adding a new textarea. I only want the form to submit when the submit button is pressed.
Any help with this would be very much appreciated,
Thanks,
Shane
Here is the HTML and the JavaScript.

<!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">
                                             </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).on("keypress", 'form', function (e) {
                        var code = e.keyCode || e.which;
                        if (code == 13) {
                            e.preventDefault();
                            return false;
                        }
                        });*/
                        
                    document.getElementById('exam').onchange = callFunctions;
                    document.getElementsByTagName("input")[1].onchange = classAdder;
                </script> 
          </body>
</html>

I suspect that this may need to prevent default behaviour or have something to do with bubbling or propagation. If so i will need to read up on these.

The enter key is the keyboard equivalent to clicking the submit button for those who don’t use a mouse. If you disable it then those people will be unable to submit the form at all.

Anyway, how do you determine that a textarea (which allows infinite input) is full?

The following code seems to be invalid when it comes to using them for a textarea field:

<td class="left">First name:</td><td><input type="textarea" name="first" value=""></td>

If you want the first name to be a textarea section that can happily accept multiline entries with the enter key, you will need to use a textarea element instead, for example:

<textarea name="first"></textarea>

If instead you intend them to just be text fields, it is type=“text” that you’ll be wanting there instead.

In regard to the enter key, if you are wanting to move from one field to the next then that is not the job of the Enter key. The Tab key moves you from one field to the next. As has already been mentioned, Enter submits the form.

Previously I did it by clicking enter and a new textarea appeared.

Ok maybe i should think about just having a button for new textareas. Also I wasn’t aware the type=“textarea” is completely wrong, show use textarea elements. Thanks for that.

I have a lot of work to do here, thanks for you’re help.
Will post my progress soon.
Thanks,
Shane

Hi,
So is it not possible to change the default behaviour of a form. In this case so that when enter is clicked ‘onchange’ comes into effect to create new forms.
Is it also not possible to have it so that clicking the ‘submit’ button is the only way to submit data to the database.
Thanks,
Shane
PS I changed the instead of type=“textarea”

How do you do that from the keyboard? Not everyone can use a mouse.

Anyway changing the meaning of ENTER (submit the form) would confuse your visitors.

Also what you are trying to do in creating a new text area when the prior one is full of a bazillion characters that someone has been typing in for a million times the age of the universe is unnecessary as they can easily fit a few more bazillion characters in the first textarea and still have filled less than 1% of the available space in the textarea.

Hi,
I know this site that i am trying to build will only used by four people. it is a results archive. And of those four only one possible two will ever input results. They will definitely have a mouse and not just a keyboard.
I just thought it would be easier to have forms be created as they are needed instead of have 10 forms by default many of which would not be used.
Could you suggest anyway in general to achieve what I am looking for.


Is a button for additional forms a good idea?Thanks,
Shane

I’m receiving updates about this thread—and I shouldn’t be. How can I unsubscribe from this?

Shane Goodman

First we need to gain a better understanding of what you mean by “forms”. Are you meaning a row with a persons name and result? Are you meaning the subject type? Or some other larger type of thing? According to standard nomenclature, form means the entire set of fields that will be submitted from the page.

If it’s the names and result that you are meaning, there is a tradeoff in regard to how many that you have. For example, when someone is entering the results of students, it can be annoying to break their flow to find and press a “new person” button.

Speaking of such things, when a select list has a limited range of options that will be frequently used, it’s easier for the user to click on a radio button (just one click) than to open the select box and choose one from the list of options.

Sorry for not being clear on this, but yes that is exactly what I mean.

Yes that’s true, I hadn’t thought about that.
The form is used to enter results for a particular class. The main issue is that I do not know how many students the user will be inputting. It could be just one or it could be 30. But the type of exam, year and subject will be common to all the students. Therefore this information should only be put in once for each group of students. Then I would like a method of adding the row which contains first name, last name and result. And I would like this method to be as automatic as possible.
I could avoid this headache by just having 40 rows of first, last and result but I do want to just push the idea and see if it is technically possible for me to do better than that and hopefully in the process learn a lot more.
I hope this explains better what i am trying to do and why.

Sorry but I have no idea why you are subscribed to this thread. And probably the fact that you have posted now and that I have commented on you means you’ll be even more suck here than ever. In fact it is possible that you will never be able to escape this thread… But seriously could the fact that we both share the same first name be the issue?
Thanks,
Shane

Sorry—I accidentally click the wrong option in Preferences. My mistake.

Shane.

1 Like

Hi,
Despite the disruption to user flow I am trying to add a button which will add a row to the table for first name, last name and result inputs.

The markup is,

<input type="button" name="addStudent" value="Add new student"/>

and the script,

document.getElementsByTagName("addStudent").onclick = classAdder;

    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;
                            }

I know the classAdder() function works fine so the problem is just calling it.

Can’t see why this doesn’t work.
Thanks,
Shane

I am trying it a different way, but still no luck.
Here is the markup,

<input type="button" onclick="classAdder();" value="Add new student"/>

The script and the classAdder() function is the same. The following line is commented out,

/document.getElementsByTagName(“addStudent”).onclick = classAdder;/

Thanks,
Shane

This works, so why not post #13

   <input type="button" onclick="test();" value="A test"/>

Script,

function test(){
                            alert("hiya!");
                            }

Thanks,

Because getElementsByTagName grabs the HTML tag. There is no HTML tag called <addStudent>

Not to mention that would return a node list so you need to reference it via [0], [1], etc.

1 Like

Hi,
Thanks I see what you mean. getElementsByTagName uses the element name not the name of the element. But post #13 doesn’t use getElementsByTagName. Clicking the button should just call the function classAdder();.
Thanks

Worked for me.
http://codepen.io/ryanreese09/pen/dogjBz?editors=101

In that case @ofeyofey, can we see a more complete example of what wasn’t working that you were trying?

Yes certainly,

Line 20 is the button that I think should be working, but isn’t.

<!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">
            <!-- <input type="button" onclick="classAdder();" value="Add new student"/> -->
            <input type="button" onclick="classAdder();" value="Add new student"/>
            <input type="button" onclick="test();" value="A test"/>
                  <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">
                                             </td>
                                             <td>Subject:</td><td id="subject" class="left"><td>
                                         </tr>
                                         <tr>
                                             <td class="left">First name:</td><td><input type="text" name="first" value=""></td>
                                             <td class="left">Last name:</td><td><input id="add" type="text" 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;
                        }
                        
                        function test(){
                            alert("hiya!");
                            }
                            
                        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).on("keypress", 'form', function (e) {
                        var code = e.keyCode || e.which;
                        if (code == 13) {
                            e.preventDefault();
                            return false;
                        }
                        });*/
                        
                    document.getElementById('exam').onchange = callFunctions;
                    /*document.getElementsByTagName("addStudent").onclick = classAdder;*/
                </script> 
          </body>
</html>

The arraybuilder.inc.php

<?php
        $exams = ['AS' => 'AS', 'A2' => 'A2', 'AP' => 'AP', 'KET' => 'KET', 'PET' => 'PET', 'FCE' => 'FCE', 'IELTS' => 'IELTS']; 
          $years = array("2015","2014","2013","2012","2011","2010","2009","2008");
          $sessions = array("January","June");
          $GCEresults = array("A","B","C","D","E","U");//Will need these as UMS scores
          $GCSEresults = array("A","B","C","D","E","U");
          $GCSEsubjects = array("Mathematics","Biology");//Will need these as UMS scores
          $ASsubjects = array("Mathematics","Geography","Physics","Biology","Chemistry");
          $A2subjects = array("Pure Mathematics","Further Mathematics");
          $APsubjects = array("Calculus","Chemistry","Physics","Biology");
          $APresults = array("5","4","3","2","1");
          $KETPETresults = array("Pass","Merit","Distinction");
          $FCEresults = array("A","B","C","D");
          $IELTSresults = array("1","2","3","4"); 

Thanks,

Hi,
I have just figured out what the issue was. I must select a subject first and then I can add a row with a new student.