Retain form values

2014-11-21 23:04

Keywords: form, field, value

Suppose that you create a complex search form (txp or not) on some page, and output the search results on the same page, like this:

<form method="get">
	Me and
	<select name="titre" required="required">
		<option value="">Please select</option>
		<option value="0">Mr</option>
		<option value="1">Mrs</option>
	</select>
	<input name="name" placeholder="Jones" required="required" />
	<input type="hidden" name="id" value="47" />
	<input type="submit" />
</form>

When the form is submitted, you would like to retain the search fields values, but it does not work, just try it yourself:

Me and

Of course, you could populate the form inputs manually with _GET parameters, but what if the form is generated via some <txp:form_tag /> and you don’t get hold on its elements? The solution consists to fill them with etc_query:

<txp:etc_query data='<txp:form_tag />'
	globals="_GET"
	replace="//select[@name='titre']/option[@value='{?titre}']@@selected=selected;
		//input[@name='name']@@value={?name||urldecode}" />

and here is the result:

Me and

File(s)