Retain dropdown select search selection onChange

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 23, 2019   (RSS)

By Zicky - January 18, 2019

Howdy folks,

I'm stumped on a dropdown selector feature. I have a form that has three fields.

  • One for a "text" keyword search.
  • Second for a dropdown "select" document_category search match.
  • and a third for a dropdown "select" year search match.

These work individually, however they do not to together because of the onChange="this.form.submit();" feature. So I can search using each field, but I can not drill down with more in-depth searches, because each time the page loads the selection for onChange="this.form.submit();" the previous selection made is lost.

I would like for each selection to be retained once the page has refresh using onChange="this.form.submit();" so that I can begin searching by keyword and continue drilling down for records by selecting a category, and if needed drill even further down I can using the year dropdown selector.

I really don't know how to accomplish this, but below is my code with an example written as IF SELECTED <?php echo "selected"; ?> END IF that shows what I'm trying to do. I'm sure there are better ways to accomplish the same results... so I'm open to any suggestions.

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" >

<input name="title,content_keyword" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['title,content_keyword']); ?>" placeholder="Keyword" />
  			<select name="document_category_match" onChange="this.form.submit();">
     			<option value="">Categories</option>
      				<?php foreach ($documentCatArray as $value => $label): ?> 
      			<option value="<?php echo $value; ?>" <?php selectedIf($value, @$_REQUEST['document_category']) ?> IF SELECTED <?php echo "selected"; ?> END IF ><?php echo $label; ?></option>
      				<?php endforeach ?>
 			<select name="date_year" onChange="this.form.submit();" >
      			<option value="">Year</option>
      				<?php foreach($years as $year):?> 
     					<option value="<?php echo $year;?>" IF SELECTED <?php echo "selected"; ?> END IF ><?php echo $year;?></option> 
    				<?php endforeach ?>
			<input type="submit" name="submit2" value="Search"/>

Thanks, Zick

By daniel - January 23, 2019

Hi Zicky,

When you select either of the dropdowns, does it clear both of the other fields? I can't currently see anything wrong with the text field, so if that isn't retaining its value on submit then there might be something at play earlier in the code. 

For the dropdowns, you should be able to use the selectedIf() function that's already present in the first one. The format for this is "selectedIf( optionValue, selectedValue )". The existing one is close; you should just need to change "document_category" to "document_category_match" so that it's the same as the select's name, like this:

<option value="<?php echo $value; ?>" <?php selectedIf($value, @$_REQUEST['document_category_match']) ?>><?php echo $label; ?></option>

The same can be added to the second dropdown's options, with "date_year" used instead of "document_category_match".

Let me know if this helps with any of your issues, or if you have any questions!


Technical Lead

By Zicky - January 23, 2019

Thanks for your guidance on this Daniel!

Your suggestions/solution got me headed in the right direction and everything works like a charm now!

Cheers, Zicky