#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    103
    Rep Power
    13

    PHP and Javascript inq


    Hello everybody,
    I have a javascript in my code to check wheather the fields are empty or not, which works..the next step i like to incorporate is to include a php script to check the user entery agains a database.. I'm having problem incorporating javascript with php.. can someone help me with some examples and directiosions..
    with javascript being in the header area i'm not sure how to get the php to do its thing after javascript does it's job.. at the same time i like to use the alert message in the javascript to tell the user the account doesn't exist which i will expand on the err message displayed later..

    total lost on this...any help you can give me would be greatly appreciated..

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function validate()
    	{	x=document.myForm
    		txt=x.myInput.value
    		txt1=x.pwd.value
    
    		if (txt!="" && txt1!="")
    			{	alert("logged in")
    				return true
    			}
    		else
    			{	alert("UserId and Password must be entered to authenticate the account")
    			
    				return false
    			}
    	}
    
    function closewindow()
    {	window.close()
    }
    
    </script>
    </head>
    
    <body>
    <form name="myForm" >
    
    enter info:
    <br>
    <input type="text" name="myInput">
    <br>
    <input type="text" name="pwd">
    <br>
    <input type="submit" value="login"  onclick="validate()">
    <br>
    <input type="submit" value="Cancel"  onclick="closewindow()">
    </form>
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Bored
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    IL
    Posts
    36
    Rep Power
    12
    PHP is server-side and Javascript is client-side, so you can't mix them together per-se...I'm not entirely sure what you're doing, but it sounds like you should validate the fields with JS, then you'd have to submit the form to a PHP page and let it do the rest.
  4. #3
  5. No Profile Picture
    I AM A GOLDEN GOD
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Location
    Camarillo, California
    Posts
    5,932
    Rep Power
    1166

    Re: PHP and Javascript inq


    First off, I would change your form to use a php script for the db authentication you want to do. I'm not all that familiar with JS, so I leave that portion up to you to change as you see fit.

    Code:
    <form name="myForm" method="post" action="auth.php">
    Then the php script: (assumes you use mysql, replace the database server, user, password, database vars with real values)
    PHP Code:
    <?
    // auth.php
    if (($_POST['submit'] == "login") && (!empty($_POST['myInput']))) {
      
    $user trim($_POST['myInput']);
      
    $pass trim($_POST['pass']);
      
    $link = @mysql_connect("$servername""$dbusername""$password");
      
    $db = @mysql_select_db("$dbname");
      
    $sql_query "SELECT
                    id,user,password
                    FROM auth_table
                    WHERE user = '
    $user'
                    AND pass = '
    $pass'
                    LIMIT 1"
    ;
      
    $sql_result mysql_query($sql_query);
      if (
    mysql_num_rows($sql_result) == 1) {
        
    header("Location: authenticated.php");
      } else {
        
    header("Location: notauthenticated.php");
      }
    } else {
      echo 
    "There was an error logging you in, please try again.";
    }
    ?>
    Of course, this is only an example of how you might authenicate someone from a database, and then of course use sessions or some other control to keep track of your logged in user. You will probably also want to use an encryption method of some sort with the password so it's stored in the db as an encrypted nonsense string and then re-encrypt their password, compare it with the encrypted string already in the db.
    Hope this is helpful to you.
    Last edited by lnxgeek; May 3rd, 2003 at 11:53 AM.

IMN logo majestic logo threadwatch logo seochat tools logo