Reputation: 13
I have two separate files, one HTML, the other ASP. Ive looked at numerous guides, and i still cant fix it. When I save these files and test them out, the message is always "null".Why doesnt this work?
First file, html:
<!DOCTYPE html>
<html>
<body>
<form name="formName" action="html_form_action.asp" method="get">
<input type="text" id="name">
<input type="submit" value="Enter">
</form>
</body>
</html>
Second, asp file:
<!DOCTYPE html>
<html>
<body>
<script>
var x=document.getElementById("name");
alert(x);
</script>
</body>
</html>
Upvotes: 1
Views: 127
Reputation:
When you use a form in html, you should use a name=""
...
Your html code for the html file should be:
<html>
<body>
<form name="formName" action="html_form_action.asp" method="get">
<input type="text" id="name" name="TheNameOfTheInput">
<input type="submit" value="Enter">
</form>
</body>
</html>
If you have an asp file, you should use asp... replace this:
<script>
var x=document.getElementById("name");
alert(x);
</script>
with(without <script>
and </script>
):
<%
response.write(request.querystring("TheNameOfTheInput"))
%>
Tutorial here: http://www.w3schools.com/asp/asp_inputforms.asp :)
Upvotes: 0
Reputation: 66389
To do it Classic ASP way, first give the input box a name: (can be same as the id)
<input type="text" id="name" name="name" />
Now its value will be sent when you click the submit button.
Next step is having such code in html_form_action.asp:
var x = '<%=Replace(Request("name"), "'", "\'")%>';
alert(x);
This will now show alert with whatever you typed in the previous page.
Upvotes: 0
Reputation: 21191
Assuming the first page posts to the second, document.getElementById("name");
will never work, since the input doesn't exist on that page - you're trying to access something that is a query-string value.
This answer to a related question seems to be the most solid method of getting query string parameters: https://stackoverflow.com/a/2880929/534109
Using that code, you replace what you're doing on the second page with:
alert(urlParams["name"]);
You would of course have to include the referenced code first.
Upvotes: 2
Reputation: 1585
Never touched asp but according to your code I think there are three problems.
Give the input an 'name' attribute e.g.
<input name="name" id="somethingElse">
Upvotes: -1