www.webdeveloper.com
Results 1 to 3 of 3

Thread: Update Cookie value through JavaScript onChange

  1. #1
    Join Date
    Nov 2010
    Posts
    2

    Update Cookie value through JavaScript onChange

    Hi there...

    I have a search results page on my small website.
    I want customers to be able to alternate between search views: relevancy, price up or down and latest products in the search results.

    The search results are coming from a PHP class on page load and the PHP script checks what the customers preferred search order is by the cookie. The cookie is also initially set as "relevancy", so far so good...

    Here is the form that I have:
    Code:
    <form name="OrVw" onChange="jsFnc_OrderView()">
    <span>order by:
    	<select name="view">
    		<option value="relevance">relevancy</option>
    		<option value="pUp">price lowest</option>
    		<option value="pDown">price higest</option>
    		<option value="latest">newest</option>
    	</select>
    </span>
    </form>
    OnSubmit I want the cookie to change and although I haven't been so far, reload the page...
    Code:
    <script  type="text/javascript">
    	function jsFnc_OrderView() {
    		var orderView = document.OrVw.view;
    		var d = new Date();
    		d.setDate(d.getDate() + 14);
    		setCookie(srchOrder, orderView, d);
    		window.location.reload();
    	}
    </script>
    So as the view changes the script should be executed... I'm stuck however, as nothing happens.

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Not all browsers support onchange for the <form> tag.
    Apply the event to the <select> tag.
    setCookie should take a days duration parameter.
    Code:
    <select name="view" onChange="jsFnc_OrderView( this )">
    
    function jsFnc_OrderView( box ) 
    {
     setCookie(srchOrder, box.value, 14);
     window.location.reload( true );
    }

  3. #3
    Join Date
    Nov 2010
    Posts
    2

    No cigar...

    Sorry it took me so long to confirm. I was stuck with another thing I was working on.

    I tried your suggestion, but it didn't work unfortunately.

    Here are the two code snip-its, as per your suggestion:

    Code:
    <script  type="text/javascript">
    				function jsFnc_OrderView(box) { 
    					SetCookie(srchOrder, box.value, 14);
     					window.location.reload( true );
    				}
    	</script>
    Code:
    <select name="view" onChange="jsFnc_OrderView(this);">
    						<option value="relevance" >	relevancy</option>
    						<option value="pUp" >price lowest</option>
    						<option value="pDown" >price higest</option>
    						<option value="latest" >newest</option>
    					</select>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles