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

    Join Date
    Sep 2002
    Rep Power

    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..

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

    Join Date
    Apr 2003
    Rep Power
    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
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Camarillo, California
    Rep Power

    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.

    <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
                    FROM auth_table
                    WHERE user = '
                    AND 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 {
    "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 10:53 AM.

IMN logo majestic logo threadwatch logo seochat tools logo