How do you convert a form with method="get" to a mod_rewrite url? - mod-rewrite

I have a PHP MVC Web App and Apache mod_rewrite rules already working fine, but when I create forms using method="get", the submitted URL looks like
contact/submit?a=b&c=d
I would like my form to submit to
contact/submit/a/b/c/d
Both posting and getting the form work fine on the server side, but when using post method, the back button always asks for reposting the form values and furthermore I want the strings in the URL for SEO. I think JQuery might let me intercept the form submit event and refresh to the url dynamically, but it it seems there must be an easier way to do it that I am missing.

You could use the POST->REDIRECT->GET pattern that Spring Web Flow utilizes. This would allow you to post as you wish and then redirect to contact/submit/a/b/c/d. It would also solve the problem with the back button asking you if you want to resubmit your form data. See this related article.

The GET method uses standard query string arguments to pass form data via an HTTP GET request.
The HTTP GET request is not intended to modify any data on the server. POST is designed for modifying data on the server.
GET may be cached. POST will not.
/a/b/c/d is not a standard format (as in RFC) for passing data. However, for requesting data or URLs to post to, that has become popular.
So, if you are updating server data, just use a POST -> REDIRECT -> /a/b/c/d.
If you are just reading data from the server, then you will need to use a bit of Javascript to read your form values and construct a query string, and then go to it with window.location = ...
Have fun!

Related

Tomcat's form based authentication with ajax app

I use tomcat's form based authentication on a webapp which use most of the time ajax call.
The configuration of the realm is pretty well documented, and it's working.
My problem begin when the user's session is ended, for any reason.
The subsequent ajax call will have a 200 ok response, with the content being the login page.
I'm looking for a way to change the beavaior of tomcat, like sending a 401 instead of hiding the content, forcing a client redirect, or any other solution that let the JS script know that the session is over and authentication is required once again.
After reading this http://www.htmlcodetutorial.com/document/index_tagsupp_13.html, I tried to add some custom header with http-equiv meta tag in my html login page.
But it doesn't work as expected. It seams that tomcat doesn't read static content in order to add headers.
I should also mention this post:
http://blog.pengoworks.com/index.cfm/2007/10/9/Expiring-Session-via-AJAX-using-HTTP-Response-Headers
What i end up doing is, on the js side, scan start of every response using a short regexp like this:
/^<!DOCTYPE html>/.test(responseBody)
It works, but take extra time for every request.
It only work because i know that i never use html as answer from the server.

Using securesocial services without using its views

I started integrating SecureSocial in my play/scala app, but I don't really like all the redirects it does between it's different views.
example - try to login from it's default login page and if you put in a wrong pass you will be redirected to a different page (url) but with the same login form. the only thing that is different is that there is an error message...
I want a simple login form (user/password provider) at the corner of my main page that submits it's data using ajax, this data is validated on the server and a response is made to either display error message/s or change the window.location.
Next to this form I will put a link to go to a more advanced login page that adds the option to use other providers like fb/twitter etc..
But from that page I also want to use ajax to submit the details and get the response.
I tried to browse into the SecureSocial source but got a little lost in there.
Can any one give me an idea how to use SecureSocial's but without using any of it's views?
NOTE: I'm not interested in customizing their views, It's not just a CSS/design issue, I want to handle the login details Ajaxly and not with normal form submission followed by redirects...
After some more rummaging around in SecureSocial code I got a better understanding of how it operates.
You can use any of the providers you listed in the play.plugins file seperatly to authenthicate the user's info from your own login/auth code. just make sure you send the right parameters that the provider needs.
I liked the way SecureSocial's ProviderController class dynamically decided what provider to use, based on a parameter. But I didn't like the responses it made - redirect.. I wanted to respond to an ajax request with some data and let the client side js handle it.
This is my solution:
pretty much copy all of ProviderController code to my own Auth.scala file (a Controller).
Changed the redirects related to "case ex, case _", kept the redirect on successful auth as it adds the SecureSocial session key related to the user.
Removed all the SecureSocial related routes from my routes file.
Put an additional hidden field with the logintype (userpass/google/fb/etc...) and configured my login ajax post to sent this along with the post to my Auth controller.
If you need more info comment here and I'll edit the answer.

Spring with AJAX integration

So I'm able to successfully integrate AJAX requests with Spring MVC. However, I have a problem- if I click the "submit" button of my form, my #Controller class detects the url and returns a ModelAndView. However, what I want is that there be an AJAX check first, and if the form submission is not successful (e.g., blank fields), return an AJAX response. Otherwise, proceed as per normal and display a ModelAndView. However, I have no clue how to integrate both at the same time.
Any ideas or tutorials are appreciated.
Thanks!
You have several choices:
submit the form to a specific, different URL, when using AJAX
add a specific parameter to the request when posting using AJAX, and use this specific parameter to check if the request is n AJAX request
test if the X-Requested-With request header is present and contains XMLHttpRequest
I would go the PJAX route or what's also known as HiJax.
Basically you return a subset of the page if it's an AJAX request using headers. Most people than just use conditions in their view/template to decide to include the full or chrome-less HTML.

HTTP Post By AJAX

I want to post some ad to CraigList webiste using this URL. https://post.craigslist.org/nyc/S/fud/mnh/all I know AJAX is a solution which can perform where there is no same origin policy restriction.
The unique thing about this URL is that it modifies the action attribute of the form every time you refresh the page and I can't just post to a single static URL. I wonder is there a way I can automate this URL using AJAX?
I am using JQuery and know how to post a URL but this is headache.
You won't be able to use AJAX as it violates the same origin policy. You can use a regular POST, but you will have to parse the page for the hidden fields etc. to make it work.
Craigslist has gone into a lot of problem to make automated posting very difficult, so I wouldn't bother.

Appropriate redirection of forms that use AJAX

I have many forms that use AJAX (w/ jQuery) for validation and data submission. When a form is filled out correctly, I use window.location to redirect the page after I get an acceptable response from the PHP script. On the new page, I use a session variable (set after the AJAX calls) to display the appropriate content. Please tell me if this is standard practice or please give me some suggestions.
Thanks!
Is there a reason you would use a $_SESSION variable to store the post-submission content? Standard practice would be to validate the form via AJAX but submit it in the standard way (i.e. via $_GET or $_POST) after validation. This way you don't need to store anything to a session and you'll likely have less to debug as you'll be submitting the form and displaying its results in the most widely-accepted way.
The benefit of AJAX is typically so that you can submit the form without actually having to do the redirect/refresh. You could get the same functionality by simply having your form POST to the destination URL, redirect to the appropriate place from there or send them back to the form displaying any errors that may have occurred. You could use AJAX to validate the form before the submission to save your users a redirection back to the form to fix their errors, but this is really just a convenience for them. Also, you will have to validate any user data on the server side once it has been submitted, as you can't rely on client-side validation, so you might as well forget the AJAX validation.

Resources