Jump to content

hw can i get drop down form to change sort by form asc or desc?


Lisa23

Recommended Posts

Hi i am trying to change the query to sort by desc or asc I am using the switch method but the form is not calling the switch statment can someone help me out please?

 

this is the swicth script

switch ($sortby) {
    case "ASC":
        	$args = array(
	's' => $_GET['s'],
	'post_type' => 'deals',
	'orderby' => 'title',
	'order' => 'ASC',
	'paged' => get_query_var('paged')
);
    break;
    case "DESC":
                	$args = array(
	's' => $_GET['s'],
	'post_type' => 'deals',
	'orderby' => 'title',
	'order' => 'DESC',
	'paged' => get_query_var('paged')
);
        break;
}

This is the form

<form name="myForm">
             <select id="sortby" > 
                 <option value="ASC">ASC</option>
                 <option value="DESC">DESC</option>
            </select>
         </form>

 

I intend to use a onchange so that when depending on the option they select it will change the order?

Link to comment
Share on other sites

You would have to actually submit the form and then use the value of $_POST['sortby'] (note you'll need to give your select a name attribute) for your switch statement.

 

If you want to use the onchange event or otherwise do it without actually submitting the form you will have to use Javascript.

Link to comment
Share on other sites

Hi thanks for getting back at me I managed to get it to work like this.

 

The script

switch ($_POST['sortby'])  { 
   case "ASC":
        	$args = array(
	's' => $_GET['s'],
	'post_type' => 'deals',
	'orderby' => 'title',
	'order' => 'ASC',
	'paged' => get_query_var('paged')
);
    break;
    case "DESC":
                	$args = array(
	's' => $_GET['s'],
	'post_type' => 'deals',
	'orderby' => 'title',
	'order' => 'DESC',
	'paged' => get_query_var('paged')
);
        break;


  }

 

The form

  <form method="post">
                                            <select name="sortby" onChange="this.submit()">
                                                <option value="">Sort By</option>
                                                <option value="DESC">DESC</option>
                                                <option value="ASC">ASC</option>
                                            </select>
                                            <input type="submit" value="submit">
                                        </form>

 

But now i am trying to change the form without the submit button just with the onchange. but is not working?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.