onlyican Posted February 5, 2007 Share Posted February 5, 2007 Hey guys I have a form, and when the user hits submit, I will check the form for certain things. Basic fields I can do easily by <input type='text' name='MyName' id='MyName' value='Jamie' /> <script type='text/javascript'> var MyName; MyName = document.getElementById('MyName').value; alert("MyName is " + MyName); </script> That works, I have a pop up with "Jamie" BUT I also have some text fields set out like this <input type='text' name='MyValues[]' value='1' /> <input type='text' name='MyValues[]' value='2' /> <input type='text' name='MyValues[]' value='3' /> The [] makes the Value go into an Array Is there anyway I can check this in JavaScript without a Page Refresh? Quote Link to comment Share on other sites More sharing options...
paul2463 Posted February 5, 2007 Share Posted February 5, 2007 you can write a javascript function to check the values which returns either true or false depending on the check and place this in the onSubmit function of the form <form name="myform1" id="myform1" type="post" action="nextform.php" onSubmit"return checkfunction();"> --> if checkfunction() returns a "false" then the page is not posted, it stops then put up --> an alert box saying this bit is wrong etc --> form bits <input type="submit" name="submit"> </form> Quote Link to comment Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 The [] makes the Value go into an Array Says who? They go into the same collection because they have the same name... that's it. Quote Link to comment Share on other sites More sharing options...
onlyican Posted February 5, 2007 Author Share Posted February 5, 2007 I mean the [] makes it an array for php I am a php guy so if I done this in php <input type='text' name='something' value='1' /> <input type='text' name='something' value='2' /> <input type='text' name='something' value='3' /> <?php print_r($_POST["something"]); //Output //3 ?> BUT if I did <input type='text' name='something[]' value='1' /> <input type='text' name='something[]' value='2' /> <input type='text' name='something[]' value='3' /> <?php print_r($_POST["something"]); //Output //Array ([0] => "1", [1] => "2", [2] => "3") if I done in javascript the following <script type='text/javascript'> function ShowValues(){ var MyValues; alert(document.getElementById('MyValues').value); } </script> <form method='post' action='' onClick="ShowValues();"> <input type='text' name='MyValues[]' id='MyValues' value='1' /> <input type='text' name='MyValues[]' id='MyValues' value='2' /> <input type='text' name='MyValues[]' id='MyValues' value='3' /> </form> I will get an Alert saying "1"; I want the array like Array ([0] => "1", [1] => "2", [2] => "3") Quote Link to comment Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 *shudder* You can't use the same ID more than once -- EVER. Second, if you gave the form a name, you could use the elements collection to get at this set of inputs... like document.forms[formName].elements['MyValues[]'] ), which is going to be an array in this case. Not that I think text inputs are the best choice. Quote Link to comment Share on other sites More sharing options...
onlyican Posted February 5, 2007 Author Share Posted February 5, 2007 i know I should not use the ID more than once, which is why I was checking if JavaScript can handle [] like PHP I have this then <script language='text/javascript' /> function FinishedEditing(){ alert(document.forms['MyForm'].elements['MyValues[]']); } </script> <form method='post' action='' name='MyForm'> <input type='text' name='MyValue[]' value='1' /> <input type='text' name='MyValue[]' value='2' /> <input type='text' name='MyValue[]' value='3' /> <input type='hidden' name='MyOrigValue[]' value='1' /> <input type='hidden' name='MyOrigValue[]' value='2' /> <input type='hidden' name='MyOrigValue[]' value='3' /> <input type="submit" onClick="FinishedEditing();" name="submit" class="loginbutton" value="Finished" /> </form> And the following pop up [object NodeList] Quote Link to comment Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 I don't know what a NodeList is.... but that object should have .length. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.