0

The question emerges from the discussion with Christoph in my last question 'HTML: target=”_blank” for a drop-down list' and the alternative method by Andrew.

Problem: to run code server-side because some users lack Javascript support. The situation is a form like here.

Some suggestions:

Christoph's recommendation:

<form action="path-to-redirection-script" method="GET" target="_blank"
 onsubmit="window.open(this.elements['foo'].value); return false;">
 <select name="foo" size="1">
  <option value="http://google.com">google</option>
 </select>
 <input type="submit" value="go">
</form>

Alternative method by Andrew:

<form onsubmit="this.js_enabled.value=1;return true;">
    <input type="hidden" name="js_enabled" value="0">
    <input type="submit" value="go">
</form>

Question: How would you do the server-side scripting in the case of form?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • Gab Royer: I will outline the suggestions. C's method relies totally on the server, fetching the script each time from the server. In contrast, A's method fetch the code only from the server if the user has no JS support. Otherwise, client runs the JS. – Léo Léopold Hertz 준영 Jul 13 '09 at 23:29
  • 2
    What kind of scripting languages are you permitted to use on your server? – Chris W. Rea Jul 13 '09 at 23:35
  • Sorry SimpleThings I deleted my comment because I was a bit ashamed not to understand the question as it didn't seem that hard for other people... But thanks a lot for sorting it out! – Gab Royer Jul 14 '09 at 00:00
  • heh gab, took me about three minutes to figure it out :) – Henrik P. Hessel Jul 14 '09 at 00:21
  • 1
    @SimpleThings: the server-side script only gets calles if scripting is disabled: see my answer for details – Christoph Jul 14 '09 at 09:07

2 Answers2

2

Actually, it depends on the server-side language you're going to use.

But, it's as simple as reading out the values in the POST and GET Values the Form delivers you.

i.e. PHP:

if($_POST["js_enabled"] == 0)  
{  
   doSomething(); // Redirecting   
}

But don't forget to validate all values.

good luck,
rAyt

Edit

There you go (pretty good answer though)

How are POST and GET variables handled in Python?

Community
  • 1
  • 1
Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
1

There's no need for any additional code or checking for scripting on the server side: Because of the return false in the onsubmit handler, the form won't be submitted if the handler is executed!

Returning false from event handlers supresses the default action associated with the event the same way as calling event.preventDefault() (event.returnValue = false in IE) does.

Christoph
  • 164,997
  • 36
  • 182
  • 240