Derleek Posted May 27, 2008 Share Posted May 27, 2008 so i have a input form where a user gets to input 10 different things from a drop down menu. here is the page: www.the3guys.com/moto_game.php username:test password:4e138c I want to store all of the 10 inputs into an array so i can check to make sure each one is unique (so a person doesn't pick rider4 twice). now, i'm wondering if it would be better to store them straight into the MySQL database? Maybe have a bool that lets the script know if it is valid or not? anywho, i am basically searching for a nice way to validate the user input for the form above... Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/ Share on other sites More sharing options...
Caesar Posted May 27, 2008 Share Posted May 27, 2008 Once the form is submitted, it's already stored in an array, temporarily ($_POST). I assume you're storing accounts in a MySQL db. If you are, then you query for any matching accounts and compare for validation. of course you'll want to also check for invalid characters, string length, and make sure you escape and scrub the input before using it in your query. Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-550929 Share on other sites More sharing options...
Derleek Posted May 27, 2008 Author Share Posted May 27, 2008 well the way i am setting the DB up (yes its mysql), there's a drop down menu so everything is closed, don't have to do much scrubbing. i'm not being clear enough here. i need to store 10 sets of values, these will be the 10 different choices of a user. Along with the value of each selection, i need to store the user id, and the order in which it was chosen along with several other variables(which aren't really relevant). I was wondering how to go about storing these 10 choices (in order). I tried doing a for loop with the form below but... here is the form: <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <select name="choices1"> <option value="null">---</option> <option value="rider1">Rider1</option> <option value="rider2">Rider2</option> <option value="rider3">Rider3</option> <option value="rider4">Rider4</option> <option value="rider5">Rider5</option> <option value="rider6">Rider6</option> <option value="rider7">Rider7</option> <option value="rider8">Rider8</option> <option value="rider9">Rider9</option> <option value="rider10">Rider10</option> </select> <br> <br> <select name="choices2"> <option value="null">---</option> <option value="rider1">Rider1</option> <option value="rider2">Rider2</option> <option value="rider3">Rider3</option> <option value="rider4">Rider4</option> <option value="rider5">Rider5</option> <option value="rider6">Rider6</option> <option value="rider7">Rider7</option> <option value="rider8">Rider8</option> <option value="rider9">Rider9</option> <option value="rider10">Rider10</option> </select> <br> <br> now, i saw somewhere that you can do it like this... <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <select name="/*change here*/CHOICES[]"> <option value="null">---</option> <option value="rider1">Rider1</option> <option value="rider2">Rider2</option> <option value="rider3">Rider3</option> <option value="rider4">Rider4</option> <option value="rider5">Rider5</option> <option value="rider6">Rider6</option> <option value="rider7">Rider7</option> <option value="rider8">Rider8</option> <option value="rider9">Rider9</option> <option value="rider10">Rider10</option> </select> <br> <br> <select name="choices[]"> <option value="null">---</option> <option value="rider1">Rider1</option> <option value="rider2">Rider2</option> <option value="rider3">Rider3</option> <option value="rider4">Rider4</option> <option value="rider5">Rider5</option> <option value="rider6">Rider6</option> <option value="rider7">Rider7</option> <option value="rider8">Rider8</option> <option value="rider9">Rider9</option> <option value="rider10">Rider10</option> </select> <br> <br> but i can't figure out how to access the form using that method. when i do: for($i=1;$i<=10;$i++) { echo "CHOICE #$i". $_POST['choice$i']; } or for($i=1;$i<=10;$i++) { echo "CHOICE #$i". $_POST['choice[$i]']; } i get nothing out... i'm really frustrated grr Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-550968 Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 Name the select tags with "choices[]" (both lowercase, not CHOICES[] and choices[], that's two different things) and then access the choices this way: <?php $choices = $_POST['choices']; foreach ($choices as $n => $choice) { echo 'Choice #', ($n + 1), ': ', $choice, '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-550978 Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 To check if the choices are unique, you can use this: <?php $choices = $_POST['choices']; //remove possible "null" values foreach ($choices as $n => $choice) { if ($choice == 'null') { unset($choices[$n]); } } if (count(array_unique($choices)) == 10) { echo 'Unique riders, OK!'; } else { echo 'No go.'; } ?> Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-550989 Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 well the way i am setting the DB up (yes its mysql), there's a drop down menu so everything is closed, don't have to do much scrubbing. No, everything is not closed. Remember that users can send anything they want to your processing page, so always sanitize user input. Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-550998 Share on other sites More sharing options...
Derleek Posted May 27, 2008 Author Share Posted May 27, 2008 if the input is a drop down menu how can they send what they want? it seems pretty set and predictable... thanks for the solution by the way Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-551038 Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 I can write my own form, and then submit it to your processing page - voila! E.g. with my Firebug extension for Firefox, I can edit a form while browsing it. Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-551049 Share on other sites More sharing options...
Derleek Posted May 27, 2008 Author Share Posted May 27, 2008 wow really? interesting... didn't know you could do that! guess i'll have to add my sanity check script to it. could you show me how your 'firebug' script works? that intrigues me Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-551109 Share on other sites More sharing options...
Derleek Posted May 27, 2008 Author Share Posted May 27, 2008 hey, while i'm on the subject. Is there an easy one line command to store an array into a MySQL database w/o using a for loop? Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-551124 Share on other sites More sharing options...
thebadbad Posted May 27, 2008 Share Posted May 27, 2008 could you show me how your 'firebug' script works? that intrigues me It's just an extension/plugin for Firefox. It gives you the ability to edit any source code behind the website you're browsing, but only locally of course - nothing is touched on the server. Neat tool to see real time changes, when editing in the code. Visit http://www.getfirebug.com/ for more info Is there an easy one line command to store an array into a MySQL database w/o using a for loop? You can use serialize() and unserialize(), or implode the array on insert, and explode it when taking it back out (given that a character is reserved for use between the elements, for the latter method). Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-551135 Share on other sites More sharing options...
Derleek Posted May 27, 2008 Author Share Posted May 27, 2008 i read that the serialize function kindof defeats the purpose of using a database, seeing is how it hinders the ability to search and access the data directly. i need to learn more about implode i think. Link to comment https://forums.phpfreaks.com/topic/107480-solved-storing-a-form-in-an-array/#findComment-551153 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.