Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use JavaScript to modify the form based on which button is clicked, or you can check server side (i.e. using PHP) which button was clicked and act accordingly.</p> <p>A submit-button is a form-input just like any other, i.e. you can give it a name and a value, which you can check for server side.</p> <p>On client side (i.e. using JavaScript) you would bind a handler to the button's click-event, modify the form's action-attribute and submit it to the new address.</p> <p>Here's a client side example:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Form submit test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="baz.html" method="post"&gt; &lt;input id="bar" type="submit" class="button" value="bar.html" name="" /&gt; &lt;input id="foo" type="submit" class="button" value="foo.html" name="" /&gt; &lt;/form&gt; &lt;script&gt; // Find the two buttons from the DOM and assign them to separate variables var barBtn = document.getElementById('bar'), fooBtn = document.getElementById('foo'); // Click-handler for the buttons. // NB! For this code to work as intended, it needs to run // in the context of the button, otherwise, the this-keyword // will not resolve correctly and this will result in an error // NB2! This code also requires that a button's value will be // the desired action handler. Usually you would probably not // do this, but use the button's name/value to lookup the // correct form action. function modifyAction(e) { this.form.action = this.value; } // Bind an event handler to an object // NB! This is not code you should use in production function bindEvent(target, event, callback) { if (target.addEventListener) { target.addEventListener(event, callback, false); } else if (target.attachEvent) { target.attachEvent('on' + event, callback); } } // Delegate creates a wrapping closure which binds the // original function's context to an object, i.e. ensuring // the this-keyword always refers to the same object when // the returned function is invoked. function delegate(context, method) { return function () { return method.apply(context, arguments); } } // Bind the click-event of the barBtb, and handle it // with the modifyAction-function bound to the barBtn. // I.e. run the modifyAction function, with the this-keyword // bound to barBtn bindEvent(barBtn, 'click', delegate(barBtn, modifyAction)); // Same as above for fooBtn bindEvent(fooBtn, 'click', delegate(fooBtn, modifyAction)); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Just for sake of completeness, here's a jQuery-example of the same:</p> <pre><code>&lt;form action="baz.html" method="post"&gt; &lt;input id="bar" type="submit" class="button" value="bar.html" name="" /&gt; &lt;input id="foo" type="submit" class="button" value="foo.html" name="" /&gt; &lt;/form&gt; &lt;script&gt; // Jquery event-handlers are automatically bound to // the element selected, so using "this" is safe function modifyAction(e) { this.form.action = this.value; } // Bind the click-event on all input with type=submit $("input[type=submit]").click(modifyAction); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload