tags:

views:

53

answers:

3

I have this form that comes with some parameters from another page. I want to preserve those values and add a sortby parameter, but every time I hit submit all the parameters disappear, but the new sortby parameter.

How can I preserve the parameters from the previous page and add or change just the orderby parameter.

<form name="formSearch" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>"  method="GET">
    <select name="order_by" id="order_by">
        <option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 1) { echo "selected"; } ?> value="1">Ultima Modificacion (Reciente)</option>
        <option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 2) { echo "selected"; } ?> value="2">Ultima Modificacion (Viejo)</option>
        <option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 3) { echo "selected"; } ?> value="3">Precio (Mayor to Menor)</option>
        <option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 4) { echo "selected"; } ?> value="4">Precio (Menor to Mayor)</option>
        <option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 5) { echo "selected"; } ?> value="5">Marca/Modelo (A to Z)</option>
        <option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 6) { echo "selected"; } ?> value="6">Marca/Modelo (Z to A)</option>
    </select>
    <input name="submit" type="submit" />
</form>
A: 

The simplest way to persist the data would be to make sure the fields responsible for populating the values in question are present on the current page. If they're not supposed to be visible to the user anymore on the page in question, you can set them to <input type="hidden" name=fieldname" value="value_set_on_previous_submit"/> . Or if they should still show, just ensure that their values are set to the submitted ones.

Ben
+1  A: 

Add

<INPUT type='hidden' name='OPT1' VALUE='<?php if( isset($_REQUEST['OPT1'])) { echo $_REQUEST['OPT1']; } else { echo "" } ?>

since you need to pass them as hidden input fields.

OPT1 is the name of the parameter to preserve - add as many of these as you have parameters

If you wish for the fields/values to actually show, then :

  • Drop type='hidden' to display them and be editable

  • Make them disabled input fields to display them and not be editable.

However those 2 options need to be done cleanly, e.g. if the old value came from radio button, you need to display and pre-populate same radio button setup, etc...

DVK
But why is the form ignoring the values $_SERVER['QUERY_STRING'] on "action"? Shouldn't the form action preserve the values set with $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'] and just add the value of "order_by" to the QUERY_STRING?
Dunewalker
A: 

It seem that is the way "form" work if you use the method "GET", So, if I need to preserve the values and need to use "GET" I would need to add a bunch of hidden fields like it was suggested. But I was trying to avoid that. If someone knows a better way, please let me know, if not this would be a solution:

" method="GET"> $v){ echo ''."\n"; } ?>

Now, If you don't need to use "GET" in the result form, just change the method to "POST" and it will work fine.

Note if you are coming from another form, that form needs to be "GET" for this to work.

Search Page -> Use Get Result Page w/ Sorting form -> Use Post

Dunewalker