phatgreenbuds Posted October 7, 2008 Share Posted October 7, 2008 Picking up where I last left off...in the code below I have a simple form with 4 seperate selectable boxes. Each is numbered box1 thru box4 and are submitted as normal. <body> <form action="../esv/testy.php" method="POST"> <select name="box1"> <option value=""></option> <option value="a1">a1</option> <option value="a2">a2</option> <option value="a3">a3</option> </select> <select name="box2"> <option value=""></option> <option value="b1">b1</option> <option value="b2">b2</option> <option value="b3">b3</option> </select> <select name="box3"> <option value=""></option> <option value="c1">c1</option> <option value="c2">c2</option> <option value="c3">c3</option> </select> <select name="box4"> <option value=""></option> <option value="d1">d1</option> <option value="d2">d2</option> <option value="d3">d3</option> </select> <input name="Submit" type="submit" id="Submit" value="Submit"> </form> </body> Now I know that the $_POST[] is really just an array that the values being posted are stored in but when you are looking to pull them back out how would you do so with efficient means? Also how would you account for a box not be selected? I was thinking something like this: <body> <?php if (isset($_POST[box])) { for ($i = 1; $i <= 4; $i++) { echo $_POST[box][$i]; } } ?> </body> But of course that did not work so I am back to reading and asking for help. FYI I am still somewhat new to this and my course of learning is to try and reproduce parts of what I use at work and sites I may chance upon. Probably not the best way to learn but it has worked for me so far. Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/ Share on other sites More sharing options...
iversonm Posted October 7, 2008 Share Posted October 7, 2008 it will never work because $_POST[box] is not set your posts are box1, box2, box3, and box4 instead of labeling them box1, box2, box3, and box4 just use box[] for all for of them and it will set them automatically $_POST[box] will be an array also as a sidenote instead of using a for statement i would use a while statement while($box=$_POST['box']){ echo $box; } Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-659481 Share on other sites More sharing options...
trq Posted October 7, 2008 Share Posted October 7, 2008 also as a sidenote instead of using a for statement i would use a while statement while($box=$_POST['box']){ echo $box; } That will create an infinite loop. A foreach would be best. foreach ($_POST['box'] as $box) { echo $box; } Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-659485 Share on other sites More sharing options...
iversonm Posted October 8, 2008 Share Posted October 8, 2008 before my computer crashed i was just trying to post that while just makes a stupid loop that never stpos you have to do a foreach as thorpe if(isset($_POST['box'])){ foreach($_POST['box'] as $box){ echo $box; } } Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-659498 Share on other sites More sharing options...
phatgreenbuds Posted October 8, 2008 Author Share Posted October 8, 2008 ok cool...I did not know that you could do that with the labels. The reason I chose to use the "for" loop is to key the eventual entry into the database. So for instance if box 2 and 4 are passed with entries then I want the selection entered into a table with an ID of 2 and 4 repectively. I have yet to figure that part out...still taking baby steps. But thats the reason for my second issue if having to account for boxes that have nothing selected. Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-659501 Share on other sites More sharing options...
Barand Posted October 8, 2008 Share Posted October 8, 2008 foreach ($_POST['box'] as $id => $box) { if ($box != "") { // process id and box } } Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-659687 Share on other sites More sharing options...
nadeemshafi9 Posted October 8, 2008 Share Posted October 8, 2008 do a for each $_POST NOT for each $_POST[] oh sorry i thoght u meant teh whole array yes do a for each to get teh check boxes Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-659690 Share on other sites More sharing options...
phatgreenbuds Posted October 8, 2008 Author Share Posted October 8, 2008 Here is how I solved this...please feel free to critique. It does work as posted here but reading the above posts I see that I could have done this differently now. testx.php <body> <form action="../esv/testy.php" method="POST"> <select name="box[]"> <option value=""></option> <option value="a1">a1</option> <option value="a2">a2</option> <option value="a3">a3</option> </select> <select name="box[]"> <option value=""></option> <option value="b1">b1</option> <option value="b2">b2</option> <option value="b3">b3</option> </select> <select name="box[]"> <option value=""></option> <option value="c1">c1</option> <option value="c2">c2</option> <option value="c3">c3</option> </select> <select name="box[]"> <option value=""></option> <option value="d1">d1</option> <option value="d2">d2</option> <option value="d3">d3</option> </select> <input name="Submit" type="submit" id="Submit" value="Submit"> </form> </body> testy.php <body> <?php if (isset($_POST[box])) { for ($i = 0; $i < 4; $i++) { $out = $_POST[box][$i]; if ($_POST[box][$i] == "") { $out = "nothing"; } echo $i+1 . ": " . $out; echo "<br />"; } } ?> Ultimately this will allow me to do the DB insert as I want it to work. I am still going to play for a bit on this and try the recommendations above. Link to comment https://forums.phpfreaks.com/topic/127469-some-complicated-theorypart-two/#findComment-660003 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.