- This topic is empty.
-
AuthorPosts
-
November 16, 2010 at 4:58 pm #30708realphParticipant
Does anyone know of some PHP to remember a form’s dropdown selection on submit?
At the moment I’m using this to remember a selection:
But when you have 10+ options it can get a bit much, is there a simpler way of doing this?November 16, 2010 at 6:30 pm #75049TT_MarkMemberThere is no real need to post this twice, albeit with one word changed in the topic from PHP to JavaScript
You could use an array and a foreach statement to save yourself a lot of time
e.g.
$arOptions = array('100', '250', '500', '1000');
foreach ($arOptions as $strOption){
echo '
if ($strOption == $_GET) echo ' selected ';
echo '';
}
November 16, 2010 at 6:57 pm #75022realphParticipantSorry, I wasn’t sure what the topic fell underneath. Is there a way of making all dropdowns remember their selection on submit without repeating that ^.
November 16, 2010 at 8:03 pm #75025Chris CoyierKeymasterIf you don’t want to clutter the URL:
Set cookie on submit script
Read cookie on new page
Use similar code to Mark’s only you just already have a variable set from the Cookie read, you don’t need to $_GET it.November 17, 2010 at 6:45 am #74968realphParticipantI’ve found some script (below) which is working for one of the Select Lists (MinPrice). I also need to target the second Select List (MaxPrice), how would I do that?
var saveString = "";
function getCookie(isName){
var cookieStr = document.cookie;
var startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
var endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
var isData = cookieStr.substring(startSlice,endSlice)
var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}function saveForm(){
var saveString = "";
var nText = document.getElementsByTagName('input');
for (i=0; i{saveString += nText.value+":"}
var isIdx = document.forms[0].selectedIndex;
saveString = saveString+isIdx;
document.cookie = "formData="+saveString+"";
}function restoreValues(){
var nValues = getCookie('formData');
var n = 0;
if (nValues)
{
//alert(nValues)
var isIndex = nValues.substring(nValues.length-1,nValues.length);
document.forms[0].selectedIndex = isIndex;
var nForm = document.forms[0];
nValues = nValues.substring(0,nValues.length-1);
var isText = nValues.split(":");
for (i=0; i{
if (nForm.type == "text")
{nForm.value = isText[n++]}
}
}
}onload=restoreValues;
onbeforeunload=saveForm;
November 17, 2010 at 7:13 am #74780Rob MacKayParticipantJust to look at your original code – you can use a shorthand if/else like this…
and the second part – remember to close your first option tag – personally I like to build a var and echo it. You can also use number_format to set how you want your number to float.
$arOptions = array('100', '250', '500', '1000');
foreach ($arOptions as $value){
$e = "
$e .= ($value == $_GET) ? " selected " : "";
$e .= ">£" . number_format($value, 2, '.', ''); . "";
echo $e;
}
November 17, 2010 at 7:29 am #74782realphParticipantThe array works perfectly for one part of the form (Buying), but when you switch the radio button to Renting it calls its JavaScript and pulls its select options from that. I can’t seem to work out a way to use that array in the JavaScript (below):
document.SearchForm.MaxPrice.options[0]=new Option ("Price (Max)","9999999");
document.SearchForm.MaxPrice.options[1]=new Option("u00A3250","250");
document.SearchForm.MaxPrice.options[2]=new Option("u00A3500","500");
document.SearchForm.MaxPrice.options[3]=new Option("u00A3600","600");
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.