<%# language = "VBScript"%>
<!DOCTYPE html>
<html>
<body>
<form method="post" action = "">
<input type = "submit" onclick = "check()" value = "Submit"/>
</form>
<%
sub check()
Response.write("TEST")
end sub
%>
</body>
</html>
When I click on the submit button, this doesn't print "TEST" for some reason.
Classic ASP runs on the server. Click events happen on the client. There is no persistent connection between the two. Web programming is not the same as desktop app programming.
At a basic level, your code needs to follow this pattern:
BROWSER - click -> request to server -> server processes request -> serves new page -> BROWSER
An onclick event won't work server side for the reasons Diodeus explains. .You need to use the Request object to collect form data. This should do what I think you're trying to achieve.
<%#language="VBScript"%>
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type = "submit" name="mybutton" value = "Submit"/>
</form>
<%
If Request.Form("mybutton") <> "" then
Response.write("TEST")
End If
%>
</body>
</html>
Related
First off, let me beg forgiveness for being such a noob at web development. I have looked everywhere for how to do this and can't find anything that tells me specifically.
Here is my link:
<td><a style="color:red;text-align:left" href=/sponsor/manageuser.htm?user="
+ target=_self"</a></td>
I need the parameter "user" to be appended and I also need the exact same value as user as the link. Like this:
<td><a style="color:red;text-align:left"
href=/sponsor/manageuser.htm?user="12345" 12345</a></td>
The problem is that I have to put the param and the link inside the tag. At least I think that's the problem So you click on 12345 link, it appends 12345 to the URL and passes it as a GET method. My form is a GET:
<form class="frm" method="get" name="currentusers" id="currentusers"
action="currentusers.htm">
And then here's what my URL looks like and of course I get a 404 error:
http://localhost:8080/sponsor/manageuser.htm?user=target=
Any advice is appreciated in advance.
jQuery way:
What you can do is have each link call a function and pass the value of the link into the function. The function append() will then add a hidden input value with the number you clicked on. Finally, the function will submit the form to the manageuser.htm page with the "?user=1234" as you requested.
You can see how the code works by copying and pasting this code and opening it on your computer.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form id = "form1" method="get" action="sponsor/manageuser.htm">
<table>
<tr>
<td><button id="1543" onclick="append(1543)">1543</button></td>
<td><button id="3515" onclick="append(3515)">3515</button></td>
<td><button id="1115" onclick="append(1115)">1115</button></td>
</tr>
</table>
</form>
<script>
function append(user_number)
{
$('<input />').attr('type', 'hidden').attr('name', "user").attr('value', user_number).appendTo('#form1');
$('#form1').submit();
}
</script>
</body>
</html>
I want to store rating value in jsp session variable inside the java script function for using in testOpeartion page
I tried this way but not storing any value "UserRating" session variable
<html>
<head>
<title>Registration Page</title>
<script type="text/javascript">
function SetRating()
{
var rating = document.getElementById("ratingValue").value;
<%
request.setAttribute("UsaerRating",rating);
%>
}
}
</script>
</head>
<body>
<form method="post" action="testOperation">
<input type="text" id="ratingValue" onclick="doDisplay(this);" />
<input type="Button" onclick="SetRating();" value="Set Rating"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
From this link
It's not possible to set any session variables directly from
javascript as it is purely a client side technology. You can use AJAX
though to asyncronously send a request to a servlet running on the
server and then add the data to the session from within the servlet.
So you wouldn't be using javascript to do the actual setting of
session variables but it would look like it is.
You can also get more details from THIS LINK
I have the following code in my SampleController;
#Controller
public class SampleController {
#RequestMapping("home")
public String loadHomePage(Model m) {
m.addAttribute("name", "CodeTutr");
return "home";
}
#RequestMapping(value="/test", method=RequestMethod.GET)
public String handlePost(#RequestParam String action, Model m) {
if( action.equals("save") ){
//handle save
}
else if( action.equals("renew") ){
//handle renew
}
m.addAttribute("name", "change");
return "home";
}
}
on page load the attribute I set it successful shown on the web page. I am trying to get my head around button clicks on spring mvc below is my jsp code;
<!DOCTYPE HTML>
<html>
<head>
<title>Sample Application</title>
</head>
<body>
<h1>Hello, ${name}!</h1>
<input type="submit" name="action" value="save" />
</body>
</html>
My input does not do anything, the method handlePost is never hit. I was trying to change the attribute "name" to the word "change", I am not sure what I am doing incorrectly.
Your issue isn't with Spring, it's with HTML. You cannot submit a button. You can only submit a <form>.
Wrap your <input> element in a <form>
<form action="<c:url value="/test" />" method="GET">
<input type="submit" name="action" value="save" />
</form>
Where <c:url> is the url tag of the core taglib. Now, when you click the button, the browser will serialize your <input> elements as url-encoded form parameters and send them. They will appear as request parameters to your server/web application. Spring will deserialize them by name and inject them as arguments where you have a #RequestParam method parameter.
There needs to be a form encapsulating your input.
General FYI: Your "save" and "renew" use cases should be separate controller actions.
Also consider removing "POST" from your action name. Seeing as the action is decorated with GET, and the html is saying its GET
I want to pass the data from page to another using POST and display first name & last name on my second page. But instead of names it does display the actual code. PLease see the code below:
<html>
<head>
</head>
<body>
<FORM action="RetrieveData_Post.asp" id=form1 method=post name=form1>
First Name:
<br>
<INPUT id="txtFirstName" name="txtFirstName" >
<br>
Last Name:
<br>
<INPUT id="txtLastName" name="txtLastName" >
<br>
<INPUT type="submit" value="Submit">
</FORM>
</body>
</html>
This is the first page and the second page is:
<%# Language=VBScript %>
<html>
<head>
</head>
<body>
<%
Response.Write("First Name: " & Request.Form("txtFirstName") & "<br>")
Response.Write("Last Name: " & Request.Form("txtLastName") & "<br>")
%>
</body>
</html>
Create an html page Gatherdata_post.htm
<html>
<head>
</head>
<body>
<FORM action="RetrieveData_Post.asp" id=form1 method=post name=form1>
First Name:
<br>
<INPUT id="txtFirstName" name="txtFirstName" >
<br>
Last Name:
<br>
<INPUT id="txtLastName" name="txtLastName" >
<br>
<INPUT type="submit" value="Submit">
</FORM>
</body>
</html>
Save the page
Create an page Retrievedata_post.asp
<%# Language=VBScript %>
<html>
<head>
</head>
<body>
<%
Response.Write("First Name: " & Request.Form("txtFirstName") & "<br>")
Response.Write("Last Name: " & Request.Form("txtLastName") & "<br>")
%>
</body>
</html>
Run Gatherdata_post.htm by right-clicking the page and selecting View in browser.
In this way you can pass values
First, this assumes that you are running this on a server running IIS and not just double-clicking the HTML file. You need a server to process the actual code, and IIS is the most common server that will process Classic ASP.
Second, you need to make sure that ASP is enabled. Classic ASP is not enabled by default on IIS6+ (Technet)
In IIS Manager, expand the local computer, and then click Web Service Extensions.
In the details pane, click Active Server Pages, and then click Allow.
It may be a little different depending on the version of IIS you are running.
Source Page
The Source Page has an HTML Button with a jQuery Click event handler. When the Button is clicked, an HTML Form is created and appended to the BODY Tag of the page. The action is set to the Destination page (Page2.aspx). Using the AddParameter function values of the Name TextBox and the Technology DropDownList is appended to the Form as Hidden Fields and then the Form is submitted
<input type="button" id="btnPost" value="Send" />
<script type="text/javascript">
$(function () {
$("#btnPost").bind("click", function () {
//Create a Form
var $form = $("<form/>").attr("id", "data_form")
.attr("action", "Page2.aspx")
.attr("method", "post");
$("body").append($form);
//Append the values to be send
AddParameter($form, "name", $("#txtName").val());
AddParameter($form, "technology", $("#ddlTechnolgy").val());
//Send the Form
$form[0].submit();
});
});
function AddParameter(form, name, value) {
var $input = $("<input />").attr("type", "hidden")
.attr("name", name)
.attr("value", value);
form.append($input);
}
</script>
Destination Page
On the Destination page (Page2.aspx), in the Page Load event of the ASP.Net Page the values of the two posted fields are fetched and are printed on the page. The below code has been done in C# but similar can be done for other technologies.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (!string.IsNullOrEmpty(Request.Form["name"]) && !string.IsNullOrEmpty(Request.Form["technology"]))
{
Response.Write("<u>Values using Form Post</u><br /><br />");
Response.Write("<b>Name:</b> " + Request.Form["name"] + " <b>Technology:</b> " + Request.Form["technology"]);
}
}
}
Pros:
Best in class as there is 100% guarantee of data being send that too in hidden form.
Pros: Best in class as there is 100% guarantee of data being send that too in hidden form.
Cons:
Requires Server side technology for fetching posted data.Requires Server side technology for fetching posted data.
I have two html files namely "file1.html" and "file2.html". File1 is supposed to encode a string written in an input file and send it to file2 via URL so that it could be decoded there.
My problem is an embarrassing one as I'm having trouble while passing from file1 to file2. Here is my code:
<html>
<head>
<script type="text/javascript">
function encode()
{
var encodeThis = document.getElementById("exampleText").value;
document.getElementById("exampleText").value = escape(escape(encodeThis));
}
</script>
</head>
<body>
<form name="input" action="file2.html" method="get">
<input id="exampleText" type="text" name="example" value="Not strongly encoded" />
<input type="button" value="Encode and Submit!" onclick="encode()" />
</form>
</body>
</html>
When I click to submit button, I expect to send the encoded string via URL and pass to file2.html where I can process it, however I remain at my first page: "file1.html". Which fundamental knowledge do I lack here ? Thanks in advance for your time.
Because there is no submit. Either you give the input-tag the type submit instead of button or you make a form.submit () via JS.
document.input.submit(); should do this.
BTW... why double-escape?
The submit button should be like this:
<input type="submit" value="Encode and Submit!" onclick="encode()" />