Jump to content

Reading Form "Select Multiple" values


supergrover1981

Recommended Posts

Hi PhpFreaks,

I'm currently trying to create a php form which includes a select box from which a user can select multiple values.

The only things is, I'm not really sure how HTML forms post multiple select values, or how to interpret them in PHP.

The good old:

[code]$multipleselect = ($_GET['selectbox'])

echo $multiple select;
[/code]

brings up no value. In the URL, it reads

http://www......../testingmultipleforminputs.php?multipleselect=a&multipleselect=b

Ideally, I'm hoping to separate each selected item by a comma, and then explode the resulting string into a php array.

Any ideas sincerely appreciated.

Cheers,
        - JB
Link to comment
https://forums.phpfreaks.com/topic/28683-reading-form-select-multiple-values/
Share on other sites

Your [b]multipleselect[/b], need to be written as [b]multipleselect[][/b], so PHP knows that they are an array, if there not, PHP will only see the last one! If you have no control over the query string (data coming from another scripting source), then you can just parse the raw $_SERVER['QUERY_STRING'], you can do it, using something like this..

[code]<?php

$out = array ();

if ( ! empty ( $_SERVER['QUERY_STRING'] ) )
{
$p = explode ( '&', $_SERVER['QUERY_STRING'] );

foreach ( $p AS $name_value_pair )
{
list ( $key, $value ) = explode ( '=', $name_value_pair );

$value = urldecode ( $value );

if ( isset ( $out[$key] ) )
{
if ( is_array ( $out[$key] ) )
{
$out[$key][] = $value;
}
else
{
$out[$key] = array ( $out[$key], $value );
}
}
else
{
$out[$key] = $value;
}
}
}

print_r ( $out );

?>[/code]

printf



Nice code printf.

There is other option with javascript:

index.php
[code]
<?php
  if( isset($_GET['var']) )
    print_r( split(",", $_GET['var']) );
?>
<html>
< script language="javascript">

function getInputsInOne(id)
{
  x = "";
  e = document.getElementById(id).getElementsByTagName('input');
  for(i in e)
    if(e[i].checked) x+= e[i].value+",";
  return x;
}

function getNewSubmitForm(){
  var submitForm = document.createElement("FORM");
  document.body.appendChild(submitForm);
  submitForm.method = "GET";
  return submitForm;
}

function createNewFormElement(inputForm, elementName, elementValue){
  var newElement = document.createElement("input");
  inputForm.appendChild(newElement);
  newElement.name = elementName;
  newElement.value = elementValue;
  return newElement;
}

function createFormAndSubmit(id){
  x = getInputsInOne(id);
  var submitForm = getNewSubmitForm();
  createNewFormElement(submitForm, "var", x);
  submitForm.action= "index.php";
  submitForm.submit();
}

</script>

<body>

<div id="options">
<input type="checkbox" value="a" id="opt_0">
<input type="checkbox" value="b" id="opt_1">
<input type="checkbox" value="c" id="opt_2">
</div>

<input type="button" value="Send" onclick="createFormAndSubmit('options')">

</body>
</html>
[/code]

I take some code from:

here http://experts.about.com/q/Javascript-1520/create-form-submit-fly.htm
here http://www.codingforums.com/showthread.php?t=65531
here http://www.onlinetools.org/articles/unobtrusivejavascript/chapter2.html
and here http://javascript.internet.com/buttons/check-all.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.