简体   繁体   中英

Javascript for a form on the server-side?

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?

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.

ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM