the submit button doesn't work - javascript

when i click on the submit button nothing happens , am not sure what is the problem.
this program just adds 2 numbers and the output should be an alerts message.
please help
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GlassFish JSP Page</title>
</head>
<body>
<%
int a = 6, b = 8, c;
c=a+b;
%>
<p>
Value 1 =
<%=a%></p>
<p>
Value 2 =
<%=b%></p>
<p>
Enter the value :<br /> <input type="text" id="c" /></br>
<input type="submit" value="Submit"
onclick="validate();" />
</p>
<script type="text/javascript">
function validate() {
var a = <%=a%>;
var b = <%=b%>;
var c = <%=c%>;
if (c!= parseInt(document.getElementById("c").value, 10))
alert("The values you entered is incorrect.");
} else {
alert("Correct!");
}
}
</script>
</body>
</html>

You are missing the opening bracket on the if statement
if (c!= parseInt(document.getElementById("c").value, 10))
should be
if (c!= parseInt(document.getElementById("c").value, 10)){

Related

logout when tab or browser window closed jsp

I am new #jsp, and would like to call the logout function when the tab or the browser window is closed. i've made some research and found out that i need to use window.onbeforeunload. But the thing is , i don't know where to put it in my jsp page. Here is it's code:
<%# page language="java" contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<%# taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%# taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%# taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>
<link rel="stylesheet" href="css/login.css">
<title>Insert title here</title>
<script type="text/javascript" language="javascript" runat="server">
var divSelected = null;
function switchURL()
{
var x = document.getElementById("femp").value;
<c:choose>
<c:when test="${puser.LOGIN==true}">
document.getElementById("temp").value = x;
// alert("${puser.LOGIN}");
</c:when>
<c:otherwise>
document.getElementById("temp").value = 0;
document.getElementById("femp").value = 0;
// alert("${puser.LOGIN}");
</c:otherwise>
</c:choose>
}
</script>
<script type="text/javascript" language="javascript" runat="server">
var divSelected = null;
function Logout()
{
document.getElementById("Form1").target ="";
document.getElementById("Form1").action = "/dcHR/login";
// window.location = "login";
}
</script>
</head>
<body>
<section class="container">
<div class="login">
<form id="Form1" action="http://localhost:80/reports/rwservlet" method="Post" target="_blank">
<input type="hidden" id="cmdkey" name="cmdkey" value="repserv">
<input type="hidden" id="temp" name="P_TO_EMP" value="0">
<input type="hidden" id="femp" name="P_FROM_EMP" value="${puser.USR_USER}" >
<input type="hidden" id="pcomp" name="P_COMP" value="${puser.USR_CP}">
<p> Years:<input type="text" name="P_YEAR"> </p>
<p> Month:<input type="text" name="P_PERIOD"> </p>
<input type="submit" value="Report" name="pbtn" onclick="switchURL()" />
<input type="submit" value="Logout" name="pbtn" onclick="Logout()"/>
</form>
</div>
</section>
</body>
</html>
can somebody help me plz.
Regards to all.
You have to add listener on window object as below
window.addEventListener("beforeunload", function(){
    //Ajax call to invoke logout action
});
Put this code in your jsp page.

i want div content disappears after i click on textbox

<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<style>
#myloc,#mypass{
color: red;
}
</style>
<script>
function validateform(){
var username=document.login.username.value;
var password=document.login.password.value;
if (username==null || username==""){
var data="username should not be blank.";
document.getElementById("myloc").innerHTML=data;
return false;
}else if(password.length<6){
var p="Password must be at least 6 characters long.";
document.getElementById("mypass").innerHTML=p;
return false;
}
}
</script>
<body>
<form action="/loginusingservlet/LoginSES" onsubmit="return validateform()" method="post" name="login">
<table>
<tr>
<td>username</td><td> <input type="text" name="username"></td></tr>
<tr><td></td><td><div id="myloc"></div></td>
</tr><tr><td>password</td><td> <input type="text" name="password"></td></tr>
<tr><td></td><td><div id="mypass"></div></td></tr>
<tr><td></td><td><input type="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>
this program works great...when i click submit buttons it displays messages like "username should not be blank " or "password atleast 6".after getting message..if i click on a textbox i want the message disappear untill i click "submit" button.
Here you go. New function in script (it checks argument and removes text from specific element:
function removeWarning(type)
{
(type == 1) ? document.getElementById('myloc').innerHTML = "" : document.getElementById('mypass').innerHTML = "";
}
And add onClick event in inputs.
<input type="text" name="username" onClick="removeWarning(1)">
<input type="text" name="password" onClick="removeWarning(2)">
To me it is working but it was a quick solution, it is possible to improve a little.

Checkbox (JSP PAGE) and insert on mysql database

i have a jsp page that user in form has to give a destination : city,country,url_of_city and 3 checkbox with the kind of the destination (summer,winter,christmas) , he can check max 2 boxes, so i need to take the results of this and insert them to a database in order to be able later to search for this destination , how can i do it this?
mysql code :
CREATE TABLE DEST_C(ID_DEST_C INT(5),CATEGORY VARCHAR(45))
CREATE TABLE DEST(ID1_DEST INT(6),ID2_DEST INT (6),COUNTRY VARCHAR(40),CITY VARCHAR(40),URL VARCHAR(5400));
jsp form code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>create dest</title>
</head>
<body>
<html>
<head>
<title>CREATE DESTINATION</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> CREATING DESTINATION </h1>
<form name="createdest" method="get" action="create dest code.jsp">
Country: <input type="text" required name="id8" /> <br>
City: <input type="text" required name="id9" /> <br>
URL Video: <input type="url" required name="id10" /> <br> <br>
<i><ins>Categorize the destination (max 2): </ins></i> <br> <br>
<input type="checkbox" name="id5" value="1" onClick="return KeepCount()" >Christmas<br>
<input type="checkbox" name="id6" value="2" onClick="return KeepCount()" >Winter <br>
<input type="checkbox" name="id7" value="3" onClick="return KeepCount()" >Summer <br> <br>
<input type="submit" value="CREATE DESTINATION" />
<br>
</form>
<SCRIPT LANGUAGE="javascript">
function KeepCount() {
var NewCount = 0
if (document.createdest.id5.checked)
{NewCount = NewCount + 1}
if (document.createdest.id6.checked)
{NewCount = NewCount + 1}
if (document.createdest.id7.checked)
{NewCount = NewCount + 1}
if (NewCount == 3)
{
alert('Pick Just Two Please')
document.createdest; return false;
}
}
</SCRIPT>
JSP CODE for inserts :
<%#page import="java.sql.*" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>create dest code</title>
</head>
<body>
<%
int m[] = new int[5000]; String s[] = new String[5000];
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
boolean check=null!=request.getParameter("id5");
boolean check1=null!=request.getParameter("id6");
boolean check2=null!=request.getParameter("id7");
String id8=request.getParameter("id8");
String id9=request.getParameter("id9");
String id10=request.getParameter("id10");
String sqlString = "INSERT INTO DEST(COUNTRY,CITY,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
myStatement.executeUpdate(sqlString);
myStatement.executeUpdate(sqlString1);
myStatement.close();
myConnection.close(); %>
</body>
<h1 style="color:blue;">Successful Registration </h1>
</html>
A very simple solution would be to assign a binary value to your checkboxes. So rather than 1,2,3 you could use 1,2,4. Then you would sum the values of the selected boxes and save this unique value.
As in your code:
`input type="checkbox" name="id5" value="1" (...)`
`input type="checkbox" name="id6" value="2" (...)`
`input type="checkbox" name="id7" value="4" (...)`
The truth table below shows you the different values.
id: ID5 ID6 ID7 Sum
value: 1 2 4
x 1
x 2
x x 3
x 4
x x 5
x x 6
The solution is not SQL related, but quite frankly, neither is the question...
Good luck!

How to get text from javascript to html

Hello guys i am new to javascript and having a problem.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Temp </title>
<script type="text/javascript">
window.onload = function() {
document.forms["EventConfirmRedirection"].submit();
}</script>
<script type="text/javascript">
var formData = {
name: alex
email: alex#example.com
}
</script>
</head>
<body>
<form name="EventConfirmRedirection" method="post" action="http://requestb.in/14ewbma1">
<input type="hidden" name="name" value="test1-suc"></input>
<input type="hidden" name="email" value="test2-suc"></input>
</form>
</body>
</html>
I want name and email in the formdata object and post it.
Any help will be appreciated.
Thanks in advance
You just need to fill the value attribute (I added id's to make things simpler but you can also do it with e.g.: document.getElementsByName("name")[0])
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Temp </title>
<script type="text/javascript">
window.onload = function() {
fillandsubmit();
};
var formData = {name: "alex", email: "alex#example.com"}
function fillandsubmit(){
var i1 = document.getElementById("name");
var i2 = document.getElementById("email");
i1.value = formData.name;
i2.value = formData.email;
document.forms["EventConfirmRedirection"].submit();
}
</script>
</head>
<body>
<form name="EventConfirmRedirection" method="post" action="http://requestb.in/14ewbma1">
<input type="hidden" id="name" name="name" value="test1-suc"></input>
<input type="hidden" id="email" name="email" value="test2-suc"></input>
</form>
</body>
</html>

object oriented javascript form registeration

i am new to the object oriented javascript :)
i have written this code to validate the first name of a registration form, but it is not working
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OOForm</title>
<meta name="author" content="engy" />
<!-- Date: 2015-02-20 -->
<script>
var Form = {
fname:{
minLength : 1,
maxLength : 15,
},
validateLength:function(formEl,type){
if(formEl.value.length>type.maxLength||formEl.value.length<type.minLength){
document.getElementById("firstNameSpan").innerHTML = "<font color='red'>Invalid User Name</font>";
return false;
}
else{
document.getElementById("firstNameSpan").innerHTML = "";
return true;
}
},
};
function checkForm(){
var ourForm = document.getElementById("ourForm");
var inputs = ourForm.getElementsByTagName("input");
if(Form.validateLength(inputs[0],Form.fname)){
document.getElementById("mySubmit").disabled = false;
return true;
}
document.getElementById("mySubmit").disabled = true;
return false;
}
</script>
</head>
<body>
<form id="ourForm">
<label>First Name</label>
<input type="text"/>
<span id="firstNameSpan"> </span>
<br/>
<input type="submit" value="submit" id="mySubmit" onclick="return checkForm()">
</form>
</body>
</html>
i don't know what is the problem with the code, but when i enter the firstname with invalid value and then press submit, it do nothing but clearing the text field
and the "invalid" span do not appear
I think it's because you need to do onsubmit on form element instead of onclick and you need to return false in onsubmit so browser won't try to send your form.

Categories