Jump to content

show/hide div based on form selection


thaidomizil

Recommended Posts

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?

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.