How to pass the value from the javascript to servlet?

I need help in Javascript?

  • i have input the javascript code .But not working .Please check wheather my code is right . <?php include "database_conn.php"; // Connect to database if(isset($_COOKIE['ID_my_site'])) //login cookie { $username = $_COOKIE['ID_my_site']; //after the user log in it will direct to the member area $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); // select from the table name user while($info = mysql_fetch_array( $check )) { if ($pass != $info['password']) //if password ok { } else { header("Location: members.php"); // go to the location member file } } } if (isset($_POST['submit'])) { //if the login is submit if(!$_POST['username'] | !$_POST['pass']) { // please make sure all field is fill die('<body bgcolor="#C6DEFF">You did not fill in the required field. <br><a href=login.php>Back</a></body></body>'); } // checks it against the database if (!get_magic_quotes_gpc()) { //show wheather the configuration code is on or off $_POST['email'] = addslashes($_POST['email']); } $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error()); $check2 = mysql_num_rows($check); //give error message if the user does nor exis if ($check2 == 0) { die('<body bgcolor="#C6DEFF">That user does not exist in the database. <br><a href=login.php>Back</a></body>'); } while($info = mysql_fetch_array( $check )) { $_POST['pass'] = stripslashes($_POST['pass']); $info['password'] = stripslashes($info['password']); $_POST['pass'] = md5($_POST['pass']); if ($_POST['pass'] != $info['password']) { //gives error if the password is incorrect die('<body bgcolor="#FFFFCC">Incorrect password, please try again. <br><a href=login.php>Back</a></body>'); } else { $_POST['username'] = stripslashes($_POST['username']);// if login is sucessfull then set the cookie $hour = time() + 3600; setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); header("Location: members.php"); //go back to the member area } } } else { // not logged in ?> <body bgcolor="#FFFFCC"> <p><img src="images/login.jpg" width="100"> <link rel="stylesheet" type="text/css" href="sidestyle.css" /> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <table class="one"> <tr><td colspan=2><h2>Login</h2></td></tr> <tr><td>Username:</td></tr> <tr><td><input type="text" name="username" maxlength="40" size="16"></td></tr> <tr><td>Password:</td></tr> <tr><td><input type="password" name="pass" maxlength="50" size="16"></td></tr> <tr><td colspan="2" align="right"><input type="submit" name="submit" value="Login"></td></tr> </table> </form> <script type=\"text/javascript\"> function loginCheck(){ var id = document.getElementById('Name'); var pass = document.getElementById('Password'); if(id.value.length != 0){ if(pass.value.length != 0){ return true; } else alert(\"Password Field is EMPTY\"); pass.focus(); return false; } else alert(\"Username Field is EMPTY\"); id.focus(); return false; } </script> </body> <?php } ?>

  • Answer:

    Your javascrpt loginCheck() is never called. Since you are using php, you should validate the input using php aswell.

Nur S at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Without going through it more thoroughly, I'd say the major problem is your "grammar". Things like this where a curly brace is opened and closed and then three are closed in a row: { if ($pass != $info['password']) //if password ok { } else { header("Location: members.php"); // go to the location member file } } } And here there are two body close tags: Back</a></body></body>'); And then to finish it, you have this: </script> </body> <?php } ?> which is opening <?php again and a curly brace after the close body tag! I'd suggest going through and carefully checking what you are opening and closing. That seems to be the first thing to deal with and after that, it might work. EDIT: Oh, yeah. I meant to mention that... the other person is right - this is PHP code and you have to use a web server that is enabled to run the PHP code. Presumably you meant PHP, not JavaScript.

L S

ok. I hav't even looked into the code above, but Im gonna guess here. This code above is NOT javascript. there is a little javascript in there ive noticed, But It is primarily PHP. PHP needs to be hosted on a php enabled web server. You cannot just "open" it. Nor can you just "upload" it to a web directory like you could with simple html with some javascript. I presume this is the mistake you have made since you thought the above was javascript. However you just may have been mixed up with your terminology.

Martin V

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.