For the first, you could try to create an element using javascript when the input is correct
Code:
function validateForm() {
// Get the variables
var
name = document.getElementById("name").value, // Value of name field
place = document.getElementById("place").value, // Value of place field
linkURL = "LINK URL", // Where the link would take the user
linkText = "", // Text of created link
div = document.getElementById("event"), // Where the hyperlink will be placed under (divs usually)
nameValue = "predeterminedValue", // Value name will be to show the link
placeValue = "predeterminedValue"; // Value place will be to show the link
// If both fields meet the requirements you specified
if(name == nameValue && place == placeValue) {
var a = document.createElement("a"); // Create the ANCHOR tag
div.innerHTML = ""; // Clear the error message
div.appendChild(a); // Add it to the document
a.setAttribute("href",""+linkURL);
a.innerHTML = linkText;
}
else {
// Check whether the element has been created otherwise remove nothing
if(a != null)
div.removeChild(); // Remove the link from the page
div.innerHTML = "Invalid Input"; // Write an error message where the link would be
}
}
}
HTML Code:
<form action="javascript:void(0)" method="get" id="form">
<input type="text" name="name" id="name">
<input type="text" name="place" id="place">
<input type="submit" value="submit query." onclick="validateForm()">
</form>
<!-- Where the link appears -->
<div id="event">
</div>
That should work
As for the second, the "GET" method puts the value into the url. E.G. filename.html?name=value&&place=value
Note: If you do not want the user to see the link just put the whole script on an external script (call the file 'validate.js' and reference by: <script type="text/javascript" src="directory/validate.js"></script>).
If you have more than one for the answers you could make two arrays called "answers." Try this
Code:
function validateForm() {
// Get the variables
var
i,
j,
k,
l,
name = document.getElementById("name").value, // Value of name field
place = document.getElementById("place").value, // Value of place field
linkURL = "LINK URL", // Where the link would take the user
linkText = "This is the link text", // Text for the ilnk (REQUIRED)
div = document.getElementById("event"); // Where the hyperlink will be placed under (divs usually)
// Easily organized array of data (answers)
// Answers for "name" field
var nameAnswers = new Array();
nameAnswers[0]="value";
nameAnswers[1]="value2";
// etc.
// Answers for "place" field
var placeAnswers = new Array();
placeAnswers[0]="value";
placeAnswers[1]="value2";
var pAnswersLength = placeAnswers.length;
var nAnswersLength = nameAnswers.length;
// Check each whether it works
for(i in nameAnswers) {
// If name is not equal to any of the answers, keep trying
if(name != nameAnswers[i] && i != nAnswersLength) {
continue;
j = 0;
// If name equals one, it stops the loop and runs the code
} else {
j=1;
break;
}
}
for(l in placeAnswers) {
// If place is not equal to any of the answers, keep trying
if(place != placeAnswers[l] && l != pAnswersLength) {
continue;
k = 0;
} else {
// If place equals one, it stops the loop and runs the code
k = 1;
break;
}
}
if(j > 0 && k > 0) {
var a = document.createElement("a"); // Create the ANCHOR tag
div.innerHTML = ""; // Clear the error message
div.appendChild(a); // Add it to the document
a.setAttribute("href",""+linkURL);
a.innerHTML = "this is a test";
}
else {
// Check whether the element has been created otherwise remove nothing
if(a != null)
div.removeChild(); // Remove the link from the page
div.innerHTML = "Invalid Input"; // Write an error message where the link would be
}
}
Please note I used javascript:void(0) as the value for the action attribute of the form tag, that just means it does not go anywhere, which I assumed is what you are talking about because the link shows up on the same page.