Re: parsing select multiple="multiple"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 18, 2013 at 7:28 PM, John Taylor-Johnston
<John.Taylor-Johnston@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
>>>          <select multiple="multiple" name="DPRtype" form="DPRform">
>>>              <option value="1. Crimes Against Persons">1. Crimes Against
>>> Persons</option>
>>>              <option value="2. Disturbances">2. Disturbances</option>
>>>              <option value="3. Assistance / Medical">3. Assistance /
>>> Medical</option>
>>>              <option value="4. Crimes Against Property">4. Crimes Against
>>> Property</option>
>>>              <option value="5. Accidents / Traffic Problems">5. Accidents
>>> /
>>> Traffic Problems</option>
>>>              <option value="6. Suspicious Circumstances">6. Suspicious
>>> Circumstances</option>
>>>              <option value="7. Morality / Drugs">7. Morality /
>>> Drugs</option>
>>>              <option value="8. Miscellaneous Service">8. Miscellaneous
>>> Service</option>
>>>              <option value="9. Alarms">9. Alarms</option>
>>>            </select>
>>>
>>>
>>>
>>
>>
>> Do test this, but I think all that's required is you make the name an
>> array:
>>
>> <select name="DPRpriority[]" form="DPRform">
>>
>
> Something like this?
>
> if $DPRpriority[0] == "7. Morality / Drugs" { echo "selected"; }
>
> I hate arrays. :D
>
> http://php.net/manual/en/language.types.array.php
>
> <?php
> $DPRpriority[]  = array(
>    1    => "a",
>    "1"  => "b",
>    1.5  => "c",
>    true => "d",
> );
> var_dump($array);
> ?>
>

Not exactly that -- I think I goofed up your variable names.
$DPRpriority is the set of values that can be selected, and DPRType
are the values returned from the form.

Here's a small snippet showing how it works, I hope:

<?php

// Initial value for demo
$DPRpriority = array(array('name' => "Crimes Against Persons",
'selected' => false),
		     array('name' => "Disturbances", 'selected' => false),
		     array('name' => "Assistance / Medical", 'selected' => false),
		     array('name' => "Crimes Against Property", 'selected' => false),
		     array('name' => "Accidents / Traffic Problems", 'selected' => false),
		     array('name' => "Suspicious Circumstances", 'selected' => false),
		     array('name' => "Morality / Drugs", 'selected' => false),
		     array('name' => "Miscellaneous Service", 'selected' => false),
		     array('name' =>"Alarms", 'selected' => false));

echo "<h1>Initial value of DPRpriority:</h1><ul>\n";
foreach ($DPRpriority as $item => $value) {
  echo "<li> ".$item.": ".$value['name']." selected:
".$value['selected']." </li>\n";
}
echo "</ul>\n";

if (count($_POST) > 0) {
  // something was posted:
  echo "<h1>\$_POST:</h1><pre><code>\n";
  var_dump($_POST);
  echo "</code></pre>\n";

  echo "<h2>Items selected:</h2><ul>\n";
  foreach ($_POST['DPRType'] as $item) {
    $DPRpriority[$item]['selected'] = true;
    echo "<li>".$item.": ".$DPRpriority[$item]['name']."</li>\n";
  }
  echo "</ul>\n";

  echo "<h1>Final value of DPRpriority:</h1><ul>\n";
  foreach ($DPRpriority as $item => $value) {
    echo "<li> ".$item.": ".$value['name']." selected:
".$value['selected']." </li>\n";
  }
  echo "</ul>\n";
}

?>

<form method="post">

<select name="DPRType[]" id="DPRType[]" multiple onchange=""
size="<?php echo count($DPRpriority) ?>">
  <?php foreach ($DPRpriority as $index => $value) { ?>
<option value="<?php echo $index; ?>"<?php if ($value['selected'])
{echo ' selected="selected"';} ?>><?php echo
$value['name'];?></option>
     <?php } ?>
</select>

<input type="submit" name="submit" value="submit" />

</form>


(gist link: https://gist.github.com/tamouse/4982630 )

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux