1

I would like to create a landing page consisting only of links. The links should make a post request to a server side script which will set some cookies and redirect the browser to another page. The reason for doing this is that I am bolting on a new locale selection page to an existing site which already has the locale logic built in - it sets a cookie containing a 2 char locale string.

Jquery is available on my page so I was considering using $.post() but this only makes XHR requests. Is it possible to do what I want using jquery? Or do I need my 'links' to be buttons within a form?

The following code makes an XHR request

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="/_scripts/jquery-1.6.1.min.js"></script>

    </head>
    <body>
        <a id="test" href="">link</a>

            <script type="text/javascript">
                $(function(){
                    $('#test').bind('click', function(){
                    $.post('/select-locale/processing',
                    {'locale':'NZ',
                        'returnPage':'/'

                    });
                }); 

                })

            </script>
    </body>
</html>

I guess I can just return a boolean value from my php script and then do window.location.href on success but wondered if there was an alternative.

codecowboy
  • 8,963
  • 17
  • 73
  • 129
  • 2
    You can create a form using jQuery, with the parameters you require on the document and [`.submit()`](http://api.jquery.com/submit/) it. – Orbling Mar 01 '12 at 17:13
  • ok - I misread this. I need to create a form then the link is clicked and then submit that. – codecowboy Mar 01 '12 at 17:38

1 Answers1

5

.post() is part of the Ajax functionality of jQuery. If you don't want to use this then fall back to regular old .submit(). This will submit a form, resulting in a new page being served up. Alternately you can submit the form to a hidden Iframe.

Diodeus - James MacFarlane
  • 107,156
  • 31
  • 147
  • 171