/    Sign up×
Community /Pin to ProfileBookmark

Help with Simple IF

At least i hope its simple.
I’m not very used to JavaScript IFs.
Basically I need to know how to do IF {Condition} but ‘2’ conditions and im not sure of the syntax whether it is AND or not, so for example:

[CODE]if {
usernameParam = “Staff”) AND (passwordParam = “Invite”);
{
response.sendRedirect(“http://trentdev:8080/2009-sem2/cs205189/Final/login.html”);
}
else {
[/CODE]

Then after that I just want it to carry on to my code that checks the user and pass in the Database because im trying to do a Special Login for Staff users that takes them to their own page.

Which is :

[CODE]ResultSet columns = statement.executeQuery(“SELECT * FROM Users WHERE Username='” +
usernameParam + “‘ AND Pass='” + passwordParam + “‘;”);

String sUserName=null;
String sUserPassword;
if(columns.next())
{
sUserName = columns.getString(“Username”);
sUserPassword = columns.getString(“Pass”);
}[/CODE]

Thanks for the help! ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMar 27.2009 — [CODE]if (usernameParam == "Staff" && passwordParam == "Invite") {
response.sendRedirect("http://trentdev:8080/2009-sem2/cs205189/Final/login.html");
} else {
// Do something else
}[/CODE]

JavaScript, Java and C/C++ all share a similar syntax commonly referred to as C Syntax.
Copy linkTweet thisAlerts:
@dntelauthorMar 27.2009 — Only thing is, im now having trouble of where to put it, i thought i put it in the correct place but it doesnt work for me, ill show you the layout of the whole code its not much:

[CODE]<HTML>
<HEAD> <TITLE>Login Page</TITLE> </HEAD>

<BODY>
<!-- HTML form allowing user to enter data -->
<P>
<HR>

<!-- the % tag below is what is called a scriptlet tag - allows java to be embedded in the jsp -->
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<!-- the java util package is needed for the enumeration class methods - used to get parameters -->

<!-- the code below: element values placed in string variables and used to construct sql statement -->
<!-- Note the construction of the Select statement using the value of the nameParam variable -->
<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/2009-sem2/cs205189/Final/javaDatabase.accdb;");

Statement statement = conn.createStatement();

String usernameParam = request.getParameter("usernameField");
String passwordParam = request.getParameter("passwordField");

ResultSet columns = statement.executeQuery("SELECT * FROM Users WHERE Username='" +
usernameParam + "' AND Pass='" + passwordParam + "';");

String sUserName=null;
String sUserPassword;
if(columns.next())
{
sUserName = columns.getString("Username");
sUserPassword = columns.getString("Pass");
}


%>

<% if (sUserName != null) {
session.setAttribute("Successful login",sUserName); %>
<h3> Your login has succeeded </h3>
<% out.print(sUserName); %>
<% response.sendRedirect("http://trentdev:8080/2009-sem2/cs205189/Final/addQuestions.jsp"); %>


<% } else { %>

<h3> Login Failed </h3>
<P> Your password is not okay
<P> <A HREF="login.html"> Please try again </A>

<% } %>

</BODY>
</HTML>[/CODE]


If you look at the code: I put the if statement just below the variable definition:
[CODE]
String usernameParam = request.getParameter("usernameField");
String passwordParam = request.getParameter("passwordField");[/CODE]


Thinking that it could look for 'Staff' and 'Invite' and if not carry on with the usual way, except it just throws an error at me to do with some other part of the code, maybe ill have to find another way of having a special login for staff ?
Copy linkTweet thisAlerts:
@toicontienMar 27.2009 — Ok. Hm. It looks like you are writing purely Java code, not JavaScript. JavaScript != Java. Is it the Java code you need help with?
Copy linkTweet thisAlerts:
@dntelauthorMar 27.2009 — nah its ok now mate, it is purely Javascript, well some ofmy pages are and some of them have html in them as well.

what i've done now is left the one page that checks the database for passwords and added a link for staff to login on a different page, so they get taken to a different page where i just did this:

[CODE]<body>
<form>
<p>Username :
<input type="text" name="text2">
</p>
<p> Password :
<input type="password" name="text1">
<input type="button" value="Login" name="Submit" onclick=javascript:validate(text2.value,"staff",text1.value,"login") >
</p>

</form>
<script language = "javascript">



function validate(text1,text2,text3,text4)
{
if (text1==text2 && text3==text4)
load('Success.html');
else
{
load('failure.html');
}
}
function load(url)
{
location.href=url;
}
</script>


</body>[/CODE]


Worked out easier that way and it works ?

now i just got one more page to do, Updating deleting from database

how fun....

thanks for the help i learned how to do a proper if statement at least !
×

Success!

Help @dntel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...