Dangling Cruze
Dangling Cruze

Reputation: 3433

With/Without Javascript form submission using ajax or normal form action

I am currently submitting a form using form action to a php script and i was going to implement ajax form submission also.

But a thought came to my mind that wouldn't it be nice to know if the client's browser has javascript enabled or disabled and submit the form accordingly as i don't want the no-javascript users to view a page that says "Enable Javascript or something"! as i have used both client and server side validations.

So, how should i know that a client has javascript and let him submit form by ajax and vice versa?

I found many QA's related to redirecting users to a page that says "Javascript disabled" and they mostly talked about tags.

I would appreciate hearing you guys and your suggestions. Thankyou :)

Upvotes: 0

Views: 193

Answers (2)

Antoine
Antoine

Reputation: 2807

Use a form with a submit button.

In your javascript script put an handler on it, and call the preventDefault function to let your ajax code do the job.

So if javascript is enabled it will submit with ajax. If not it will submit it the default way .

EDIT:

Here is a jsfiddle to illustrate it: http://jsfiddle.net/CTwx2/

Basically the default behavior of a submit button is to send the data to the address indicated in the form tag.

With the handler defined in javascript & jquery, you stop this behavior and send the data by yourself through an ajax request (http://api.jquery.com/event.preventDefault/)

Upvotes: 2

isJustMe
isJustMe

Reputation: 5470

You will have to have 2 versions of your site and redirect accordingly using :

<noscript>
  <meta http-equiv="refresh" content="0;url=noscript.html">
</noscript>

OR

Wrap your form in your <noscript> tags, and if javascript is enabled disable that form-

Upvotes: 1

Related Questions