CodeIgniter Forums
Help with variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help with variables (/thread-45792.html)



Help with variables - El Forum - 10-05-2011

[eluser]Unknown[/eluser]
Hi everyone,

I have a view that has a form that allows a user to enter guest details. They submit the form and it needs to check to see if they have entered in all the fields. The problem I am having is that it is only evaluating the first guests details and not the rest. What I want to do is save all the text boxes in an array and then check to see if each one is empty, if it is, then I want to alert them. It doesn't seem to be working.

Here is my code:

This dynamically displays the amount of guests input boxes depending on the input of how many they want.

Code:
[removed]
                                    var mycounter = 1;
                                    var limit = <? echo $limitvar; ?>;
                    var allIDname = [];
                    var allIDemail = [];
                    var allIDphone = [];

                    function addInput(number){
                        mycounter = 1;
                        document.getElementById("dynamicInput")[removed] = "";
                        var num = parseInt(number);
                        var i;

                        for (i = 0 ;i <num; i++){
                            if (mycounter == limit)  {
                                alert("You have reached the limit of adding " + mycounter + " inputs");
                                break;
                            }
                            var newdiv = document.createElement('div');
                            newdiv[removed] = "   <tr><td>Guest " + mycounter + "<td></tr> <table><tr><td>Guest name</td><td>Guest email</td><td>Guest phone number</td></tr> <tr><td>&lt;input type='text' name='myGuestName[]' id='agname'&gt;&lt;/td><td>&lt;input type='text' name='myGuestEmail[]' id='agemail'&gt;&lt;/td><td>&lt;input type='text' name='myGuestPhone[]' id='agphone'&gt;&lt;/td></tr></table>";
                            document.getElementById("dynamicInput").appendChild(newdiv);
                            // Adding the input boxes to an array
                            allIDname = document.getElementById('agname');
                            allIDemail = document.getElementById('agemail');
                            allIDphone = document.getElementById('agphone');
                            mycounter++;
                        }
                    }

                    // Supposed to validate to see if there is any input in all the boxes when form is submitted
                    function validateGuest() {
                        var guestName = document.getElementById('agname');
                        var guestEmail = document.getElementById('agemail');
                        var guestPhone = document.getElementById('agphone');
                        // only alerting first guest's details
                        alert(allIDname.value);
                        alert(allIDemail.value);
                        alert(allIDphone.value);
                        if (guestName.value == "") {
                            alert("Please enter the guest's name");
                            return false;
                        } else if (guestEmail.value == "") {
                            alert("Please enter the guest's email");
                            return false;
                        } else if (guestPhone.value == "") {
                            alert("Please enter the guest's phone");
                            return false;
                        }

                        return true;
                    }
                [removed]