2

I want to take advantage of some jquery functions for sending messages like having a modal dialog but I also want to include users not running javascript.

If I have a jquery function to handle the dialog but the user doesn't have javascript enabled whats the best way to redirect them to my PHP page for composing and sending a message?

I'm assuming the jquery code takes highest priority since it's client side but then do I wrap my php code in tags?

Is there a more elegant solution?

23
  • 4
    I think you're confusing server-side and client-side code. What you do is you have a normal HTML form. You have JS override the submit functionality, and do whatever. If JS is off, the form would submit normally. Commented Jan 16, 2012 at 19:09
  • 4
    Do you have users that actually disable javascript? Commented Jan 16, 2012 at 19:10
  • 2
    <noscript>Time to upgrade your browser</noscript> Commented Jan 16, 2012 at 19:10
  • 3
    is this really worth worrying about anymore? this is only 1% max of users developer.yahoo.com/blogs/ydn/posts/2010/10/… Commented Jan 16, 2012 at 19:10
  • 2
    Evan @AdamRackis jrummel can we stop trolling and actually write HTML pages that actually work. non-javascript support is still important, it's called not breaking the bloody web. Commented Jan 16, 2012 at 19:13

2 Answers 2

2

I think what I would do here is take advantage of the onclick event of the button. You can use javascript or jquery to return false. If the browser has javascript turned off, the form will then be submitted.

<input type="button" onclick="return btnClick()" value="submit">

and then in your javascript something like

function btnClick(){
    //do ajaxy stuff
    return cancelEvent()
}
function cancelEvent()
{
    if (window.event) window.event.cancelBubble = true;
    return false;
}   
Sign up to request clarification or add additional context in comments.

5 Comments

avoid in line DOM0 event handlers. Also please make you cancelEvent function work. you need to call event.stopPropagation() because return false doesn't do anything in DOM event handlers (it's a non-standard extension)
It does work, I pulled it from one of my working solutions. return false works everywhere that if (window.event) window.event.cancelBubble = true doesn't. Try it before suggesting that it doesn't work.
Where in DOM3 events or DOM4 does it say that return false from an event listener will invoke event.stopPropagation(). It's a non-standard extension which may be removed at any time.
Note that I meant event.preventDefault(); not event.stopPropagation();. My mistake.
That'll work perfectly. I'll handle my jquery modal dialog if enabled and if not I'll include a href tag. Thanks
2

What you want to do is make a normal HTML form:

<form action="submit.php" method="POST" id="sendMessage">
   <input id="msg" name="message" type="text" />
   <button type="submit">Submit</button>
</form>

And then use jQuery to override the submit feature:

$('#sendMessage').submit(function(e){
  e.preventDefault(); // Stops the browser from submitting the form normally
  // Do stuff...
});

So, if JavaScript is enabled, jQuery will handle form submission. It it's not enabled, the browser will POST the form normally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.