Jump to content

[SOLVED] concatenate <select> values ONLY if they've been selected??


Recommended Posts

i have a form that has four select dropdowns that i want to implode it into an array but ONLY if the dropdown has a value other than SELECT.  I can get it to work as long as i populate all the menus but if i dont populate one 'SELECT' is posted to my array and i dont want that.  Can someone please help?!  point me in the right direction!!!!

 

<form id="search" name="search" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select class="small" name="dropdown[]">
  <option selected="selected">Select</option>
  <option value="option">option</option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
</select><br />
<select class="small" name="dropdown[]">
  <option selected="selected">Select</option>
  <option value="option">option</option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
</select><br />
<select class="small" name="dropdown[]">
  <option selected="selected">Select</option>
  <option value="option">option</option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
</select><br />
<select class="small" name="dropdown[]">
  <option selected="selected">Select</option>
  <option value="option">option</option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
</select><br />
<input id="search" name="search" type="submit" value="Search" />
</form>
<?php
if(isset($_POST['search']))
{

if(isset($_POST['dropdown'])){$dropdown = implode(', ', (isset($_POST['dropdown']) ? ($_POST['dropdown']) : 'Select'));}
echo $dropdown;
}
?>

 

<form id="search" name="search" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<select class="small" name="dropdown[]">

  <option selected="selected">Select</option>

  <option value="option">option</option>

  <option value="option1">option1</option>

  <option value="option2">option2</option>

</select><br />

<select class="small" name="dropdown[]">

  <option selected="selected">Select</option>

  <option value="option">option</option>

  <option value="option1">option1</option>

  <option value="option2">option2</option>

</select><br />

<select class="small" name="dropdown[]">

  <option selected="selected">Select</option>

  <option value="option">option</option>

  <option value="option1">option1</option>

  <option value="option2">option2</option>

</select><br />

<select class="small" name="dropdown[]">

  <option selected="selected">Select</option>

  <option value="option">option</option>

  <option value="option1">option1</option>

  <option value="option2">option2</option>

</select><br />

<input id="search" name="search" type="submit" value="Search" />

</form>

<?php

if(isset($_POST['search']))

{

$imploded_str = "";

foreach($_POST['dropdown'] as $key => $value){

if($_POST['dropdown'][$key] != "Select"){

$imploded_str .= $value.",";

}

}

$imploded_str = substr($imploded_str,0,strlen($imploded_str)-1);

echo $imploded_str;

}

?>

 

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.