denoteone Posted November 5, 2009 Share Posted November 5, 2009 I have 43 input text boxes that I am going to name box0 box1, box2 and so on. is there a trick I can use to assign them once I submit the form. so that I don't have to do $box_1 = $_POST['box1']; some sort of loop and then I am going to have to insert the values into a mySQL database. Any help would be awesome. Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/ Share on other sites More sharing options...
abazoskib Posted November 5, 2009 Share Posted November 5, 2009 you could do 1 of two things: a) you can name then all "box[]" and then access them as an array, or you can create dynamic variables using ${} Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-951934 Share on other sites More sharing options...
denoteone Posted November 5, 2009 Author Share Posted November 5, 2009 OK I will do it as an array. so all the inputs should be named box[] ? do I have to create and array like flash? Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-951939 Share on other sites More sharing options...
denoteone Posted November 5, 2009 Author Share Posted November 5, 2009 so would my code look something like this if(isset($_POST['submit'])){ for($i = 0, $i <= 43,$i ++){ $box.$i = box[i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-951956 Share on other sites More sharing options...
mrMarcus Posted November 5, 2009 Share Posted November 5, 2009 form: <input type="text" name="box[]" value="box0" /> and so on for each <input>... now, $_POST['box'] is an array. use appropriate array functions to manipulate the values to suit your needs. Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-951958 Share on other sites More sharing options...
mikesta707 Posted November 5, 2009 Share Posted November 5, 2009 if box[] is your HTML array, you could access each individual element like so echo $_POST['box'][5];//the sixth text box you could loop through the whole array like so for ($i = 0; $i < count($_POST['box']); $i++){ echo $_POST['box'][$i]; } //OR foreach($_POST['box'] as $box){ echo $box; } Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-951961 Share on other sites More sharing options...
abazoskib Posted November 5, 2009 Share Posted November 5, 2009 so would my code look something like this if(isset($_POST['submit'])){ for($i = 0, $i <= 43,$i ++){ $box.$i = box[i]; } } Don't do that. What if one day you added another checkbox, or took one away? Chances are you won't find the error when you define 43 like that. Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-951975 Share on other sites More sharing options...
denoteone Posted November 5, 2009 Author Share Posted November 5, 2009 I am stuck in an infinite loop and I am getting an undefined error if(isset($_POST['submit'])){ for ($i = 0; $i < count($_POST['box']); $i++){ $box.$i = $_POST['box'][$i]; echo $box.$i; } Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-952041 Share on other sites More sharing options...
mikesta707 Posted November 5, 2009 Share Posted November 5, 2009 post the error? assuming your $_POST['box'] array isn't infintely long that for loop wouldn't cause an infinite loop Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-952045 Share on other sites More sharing options...
Alex Posted November 5, 2009 Share Posted November 5, 2009 Looks like you want to do something like this? if(isset($_POST['submit'])){ for ($i = 0; $i < count($_POST['box']); $i++){ ${'box_' . $i} = $_POST['box'][$i]; echo ${'box_' . $i}; } Variables will then be like $box_0, $box_1, etc.. Quote Link to comment https://forums.phpfreaks.com/topic/180441-stream-line-_post/#findComment-952056 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.