thaidomizil Posted September 8, 2011 Share Posted September 8, 2011 Hello, i have a form with a dropdown selection with the values 1 to 8, on the submit page i have another form which has 8 divs, now i want to show only a number of those divs based on the selection of the dropdown from the previous page. example: 1st page dropdown value is 4, form gets submitted, on the next page show div 1 & 2 & 3 & 4, and so on. Is this possible? /Edit: i just thought about it again and i think i could use <?php $dropdown=$_POST['dropdown']; if($dropdown=="1") { ?> around the div tags, but then i would have to make 8 forms and take out 1 div one by one based on the dropdown selection, or is my thinking wrong? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted September 8, 2011 Share Posted September 8, 2011 Of course. Just use a simple loop i.e <?php /* num from previous page */ $limit = 4; /* loop out the divs */ for($x = 1; $x <= $limit; $x++) { echo "<div id=\"".$x."\">".$x."</div>"; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 8, 2011 Share Posted September 8, 2011 Yes, it is possible. When you say "show only a number of those divs based on the selection of the dropdown from the previous page", do you need the other divs in the page, but just not visible? This might be the case if you have javascript to show/hide the divs dynamically. Also, it would be helpful to see your current code for displaying the divs, but here goes $divsToDisplay = (int) $_POST['fieldFromFirstPage']; if($divsToDisplay<1 || $divsToDisplay> { //Invalid value - add error handling or set default value $divsToDisplay = 1; } if($divsToDisplay >= 2) { //Show div 2 } if($divsToDisplay >= 3) { //Show div 3 } if($divsToDisplay >= 4) { //Show div 4 } if($divsToDisplay >= 5) { //Show div 5 } if($divsToDisplay >= 6) { //Show div 6 } if($divsToDisplay >= 7) { //Show div 7 } if($divsToDisplay >= { //Show div 8 } Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted September 8, 2011 Share Posted September 8, 2011 Come on mjdamato that's god awful code. If they were to add more elements to the select list are you going to keep adding conditionals to the script? A simple loop will suffice and requires no modifications on change of the number of list elements. Quote Link to comment Share on other sites More sharing options...
thaidomizil Posted September 8, 2011 Author Share Posted September 8, 2011 mjdamato, i'm building up the page with the divs currently, so to make them show with your piece of code i would have to hide them first and afterwards bring them back to a visible state with your code , i guess? Thanks for your help, to both of you, it's very much appreciated. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 8, 2011 Share Posted September 8, 2011 Come on mjdamato that's god awful code. If they were to add more elements to the select list are you going to keep adding conditionals to the script? A simple loop will suffice and requires no modifications on change of the number of list elements. Neil, I did state Also, it would be helpful to see your current code for displaying the divs, but here goes Yes, I considered that, but since I don't know "how" those divs are created or what their content is, I don't know if a loop would be easily implemented into what he had. And, I wasn't going to go out of my way to rebuild his current code when he didn't take the time to provide it. But, sure you could do something like this: $divs = array(); $divs[1] = "<div>Content of div 1</div>"; $divs[2] = "<div>Content of div 2</div>"; $divs[3] = "<div>Content of div 3</div>"; // etc . . . $divsToDisplay = (int) $_POST['fieldFromFirstPage']; if($divsToDisplay<1 || $divsToDisplay> { //Invalid value - add error handling or set default value $divsToDisplay = 1; } for($divIdx=1; $divIdx<=$divsToDisplay; $divIdx++ { echo $divs[$divIdx]; } But, again, depending on what he is actually doing that may or may not be the best approach. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted September 8, 2011 Share Posted September 8, 2011 Redeemed. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 8, 2011 Share Posted September 8, 2011 Redeemed. I don't believe there was anything to be redeemed. I provided a response valid solution based upon the information (or lack thereof) given. Quote Link to comment Share on other sites More sharing options...
thaidomizil Posted September 8, 2011 Author Share Posted September 8, 2011 Sorry that i have to bother you again, i tried using the last piece of code you provided, i'm getting this error: Parse error: syntax error, unexpected ';', expecting ')' in /Applications/XAMPP/xamppfiles/htdocs/test.php on line 15 Edit: Hey, it works just the way i really needed it too ! Thanks to you guys ! Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted September 8, 2011 Share Posted September 8, 2011 Should be for($divIdx=1; $divIdx<=$divsToDisplay; $divIdx++) not for($divIdx=1; $divIdx<=$divsToDisplay; $divIdx++ Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 8, 2011 Share Posted September 8, 2011 From my sig I do not always test the code I provide, so there may be some syntax errors. 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.