Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand your question right, and you want a button to populate the form fields before posting the form - just place your embedded code in a javascript function and set it as the click handler via the 'onclick' property of the button.</p> <p><strong>Ex:</strong></p> <pre><code>&lt;script&gt; function populate(){ document.getElementById('name').value='myname'; //set username document.getElementById('password').value='mypassword'; //setpassword document.forms["formID"].submit(); //submit 'formID' } &lt;/script&gt; ... &lt;form&gt; .... &lt;input type="button" name="test" value="Free User" onclick="populate()"&gt; &lt;/form&gt; &lt;!-- or as a link.. --&gt; &lt;a href="#" onclick="populate()" title="login as free user"&gt;Free User&lt;/a&gt; </code></pre> <p>Id question a design that means you need to populate the variables and not have them as defaults though! (Defaults can be set in the HTML) However, if that's the design spec then here's your code!</p> <p><strong>Edit</strong> To summarise all the comments below: this code wont work if you have an element in your form with the name 'submit' - ala most submit forms which get named submit! If you try and call this code the submit line will actually try and call the button; which is obviously not a function and you'll get this error here...</p> <pre><code>Uncaught TypeError: Property 'submit' of object &lt;#an HtmlFormElement&gt; is not a function. </code></pre> <p>[<a href="http://jsfiddle.net/KS5ZN/3/" rel="nofollow noreferrer">jsFiddle with the error</a>] [<a href="http://jsfiddle.net/KS5ZN/2/" rel="nofollow noreferrer">jsFiddle fixed</a>] - <strong>Note only the submit button name has changed</strong>.</p> <p>The explanation for this is <a href="https://stackoverflow.com/a/9993047/1475897">here</a> - well you learn something new everyday!</p> <p>So you can either set the name of your button to something else - i.e. sub; or you can use GitaarLAB's solution of calling the click event on the submit button programmatically. (See the comments section for this)</p>
    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. 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