How can I create a permalink for my form here so that 开发者_JAVA百科all the values of the form can be saved in the URL?
What you're asking for isn't really a "permalink" so much as it's just a way to provide someone with a link which automatically fills in form values. A "permalink" links to an existing record, whereas you're asking to create a new record (based on your comment).
If you want all the values in the URL, then what you can do is have a URL like this:
~/itemdb.html?field1=value1&field2=value2&field3=value3...
And so on. (Replace the fields with actual meaningful field names, of course.)
The page has an .html
extension. Is there any server-side logic here, or is it just a static page? If there's any server-side processing, then you can capture those values (GET values, since a link creates just a GET request) in your server-side code and pre-populate the fields.
If you don't have any server-side logic driving this, you can still do it with JavaScript. There are a number of methods to get URL parameters in JavaScript. What you decide to do is up to you. Once you have the values, you can just set the values of the corresponding form elements accordingly. For example (assuming jQuery):
var fieldValue = GetFieldValueSomehow('fieldName');
$('#elementName').val(fieldValue);
You'll want to do some basic input checking, of course.
If you want the pure JavaScript solution, then using #hash might be an easiest way to go:
http://ambo100.com/salmoneus/database/itemdb.html#{"name":"John","price":10}
In this case you are free in choosing the syntax (and not limited by the query string). This could be processed by the following simple JS code:
try {
var data = JSON.parse(location.hash.substr(1, location.hash.length));
$.each(data, function(key, value) { // assuming you're using jQuery
$(':input[name=' + key + ']').val(value);
});
}
except {
}
First you will need to save the values to some form of database.
You would then need to have a page which pulls the data out of the database querying against something passed in the URL. This could be anything but the simple solution would be to use an auto incrementing primary key column in the database.
To pass the unique value which matches to this column either add the code in a querystring or better still use routing.
There are many more options on how to create the link. Consider the codes produced by URL shorteners. Also with routing some websites produce a friendly link with a name based on what is entered in the form and then pin the database ID at the end which is the actual bit which points to the right data.
Another solution would be to actually create a flat HTML page on the server with the data in the form and give the user back the link to that page created.
精彩评论