1

We have a form that gets filled out many times a day and would like to create some "templates"

The form is very simple. It has inputs for "Subject" and "Body"

Would it be possible for me to create another web page with buttons or links on it that auto insert data into these fields? For example; Create a button called "Firewall change" - Click on that button would take you to the page w/ the form and pre-populate the subject with "Firewall change #" and the body with "blah blah blah"

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
AaronJAnderson
  • 1,698
  • 4
  • 18
  • 25
  • What do you have access to? Can you modify the code on the target page? If not you will need to accomplish this using a plugin or extension for your browser. Any reason you want changes on a separate page to propagate to this page? It would be a lot easier if the buttons were on the same page. – mrtsherman Jan 06 '12 at 03:22
  • I have access to everything. The situation is that the page I want to auto-fill gets overwritten on a regular basis. It's an automatically generated page from a program called AppGini. – AaronJAnderson Jan 06 '12 at 04:24

4 Answers4

0
<input type="text" id="someid" />

$("#someid").val("whatever you want to set it to")

Done with jQuery, I hope you don't mind.

You'd of course put the Javascript into an onclick handler (or $.click()).

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
0

You can use javascript with jQuery to fill the default values on the input texts, textarea boxes, checkboxes, select, etc. and you can also fill them in dynamically, for instance with PHP, or you can use both approaches.

Here is an example of how to prefill data from the server via PHP & GET:

<input type="text" name="title" id="title" value="<?= $_GET['title']; ?>"/>

Here is an example of how to prefill data from the server via PHP & POST:

<input type="text" name="title" id="title" value="<?= $_POST['title']; ?>"/>

Or, if using jQuery, for the same input text default values:

jQuery(document).ready(function() { $("#title").val("<?= $_GET['title']; ?>"); }

Filling the form from the server might be more convenient since you have access to validations or database-like data that you might want to test the form against when the user submits it.

Javier Isaaí
  • 440
  • 5
  • 12
0

While it's certainly possible to craft a separate web page to do this, it sounds more like you're simply looking for a macro program of sorts to help you auto-fill in forms. There's plenty of software out there for that kind of thing (such as RoboForm and LastPass or even just every web browser's autofill features)

If your looking for something more home grown and programmable, a userscript sounds like it would be simple enough to write. Just create a javascript file named mytemplates.user.js and put something like this into it (solution to include jquery stolen from here):

// ==UserScript==
// @name Name your script here
// @namespace http://doesnthavetobearealwebsite.com/
// @include http://sitewhereyoufillinformsdomain.com/*
// @author Your name here
// @description Script which fills out our forms!
// ==/UserScript==

// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://code.jquery.com/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {
  $('body').append($('<button>My Template</button>')).click(function(){
      $('[name*=firstName]').val('Initial Value');
      $('[name*=lastName]').val('Another template value');
  });
}

// load jQuery and execute the main function
addJQuery(main);

This would add a button to the bottom of the page which, when clicked, would fill out the form using the code above.

Expand the main section out to include all your form elements.

In Chrome or Firefox (with the GreaseMonkey extension) just hit Ctrl+O to open the "open" dialog and then open the file. You'll be asked if you wish to install the extension.

Repeat each of the lines above for each query-able form element and you're good to go. If you don't know jQuery or JavaScript, go learn it!

Community
  • 1
  • 1
peabody
  • 553
  • 5
  • 11
0

You don't need jQuery to insert. Only use

document.getElementById('elementinsert').value += 'text to insert';
bobbymcr
  • 23,769
  • 3
  • 56
  • 67