Paul
Paul

Reputation: 11756

Can I do both javascript and a PHP form?

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?

Upvotes: 2

Views: 150

Answers (2)

gen_Eric
gen_Eric

Reputation: 227310

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.

Upvotes: 2

Jeff Lauder
Jeff Lauder

Reputation: 1247

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;
}   

Upvotes: 2

Related Questions