supergrover1981 Posted November 28, 2006 Share Posted November 28, 2006 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=bIdeally, 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 More sharing options...
printf Posted November 28, 2006 Share Posted November 28, 2006 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 Link to comment https://forums.phpfreaks.com/topic/28683-reading-form-select-multiple-values/#findComment-131308 Share on other sites More sharing options...
PHPSpirit Posted November 28, 2006 Share Posted November 28, 2006 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.htmhere http://www.codingforums.com/showthread.php?t=65531here http://www.onlinetools.org/articles/unobtrusivejavascript/chapter2.htmland here http://javascript.internet.com/buttons/check-all.html Link to comment https://forums.phpfreaks.com/topic/28683-reading-form-select-multiple-values/#findComment-131370 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.