简体   繁体   中英

how to link php, sql, and Javascript together,

I'm doing a log in page, i have javascript doing validations ( checking if field is blank) sql storing the data and php doing what php does (idk).... anyway when I press submit it tells me Cannot POST /login.php

is there away to test it on a website and see if it actually works or is the code completely wrong.

<?php

$server = 'localhost';
$username = 'root';
$passowrd = 'cosc_453';
$dbname = 'login'


if(!empty($_POST['user']))


{ $query = mysql_query("SELECT * FROM UserName where userName ='$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error()); 

$row = mysql_fetch_array($query) or die(mysql_error());


{ $_SESSION['userName'] = $row['pass']; echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE..."; } 



else { echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY..."; 
    }
  }
 }

if(isset($_POST['submit'])) 


{ SignIn();

 } ?>

php external

     function validate(){

   if ( document.getElementById (user).value=="")
      {
       alert ("Please enter your user name");
   }
   else if ( document.getElementById(pass).value=="")
alert("Please enter you password");
 else {
alert("Processing Login........");
}
      }

javscript external

  CREATE TABLE UserName ( 
 UserNameID int(9) NOT NULL auto_increment,
userName VARCHAR(40) NOT NULL, 
 pass VARCHAR(40) NOT NULL, 
PRIMARY KEY(UserNameID) );

 INSERT INTO 
UserName (userName, pass) 
VALUES
 ("cosc" , "453");

sql external

<!DOCTYPE HTML> 
 <html>
<head>
<title>Sign-In</title>
<link rel="stylesheet" type="text/css" href="home.css">
<script src ="login.js"></script> 
</head> 
<body id="body-color"> 

<div id="Sign-In">
<fieldset style="width:30%">
<legend>LOG-IN HERE</legend>

<form method="Post" action="login.php" submit =" validate()"> 

User:<br><input type="text" name="user" size="40"><br> 

Password:<br><input type="password" name="pass" size="40"><br>

<input id="button" type="submit" name="submit" value="Log-In">

   </form> 
  < /fieldset>
   </div> 
  </body> 
        </html> 

Your mysql do not have a connection to database. And please stop using mysql, use mysqli instead

<?php

    $con = mysqli_connect("localhost","root","cosc_453","login");

    // Check connection
    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql = "SELECT * FROM UserName WHERE userName ='".$_POST[user]."' AND pass = '".$_POST[pass]."'";
    $result = mysqli_query($conn,$sql);
    $count_result = mysqli_num_rows($result);

    // Login Success URL

    if($count_result  == 1)
    {
        // If you validate the user you may set the user cookies/sessions here
        #setcookie("logged_in", "user_id");
        #$_SESSION["logged_user"] = "user_id";

        $_SESSION["secret_id"] = $row['secret_id'];

        if($row['level'] == 1)
        {
            // Set the redirect url after successful login for admin
            $resp['redirect_url'] = 'admin/';
        }

        else if($row['level'] == 2)
        {
            // Set the redirect url after successful login for user
            $resp['redirect_url'] = 'user/';
        }

    }
    else
    {
         echo "Invalid username or pass";
    }


?>

To add onto what Eh Ezani stated, you have an issue in your HTML. Your form attribute reads submit when I believe what you meant is onsubmit. Might want to try something like.

<form method="Post" action="login.php" onsubmit ="return validate()"> 

     User:<br><input type="text" name="user" size="40"><br> 

     Password:<br><input type="password" name="pass" size="40"><br>

     <input id="button" type="submit" name="submit" value="Log-In">

</form> 

Also, "Use MySQLi over the older MySQL functions. The "i" stands for "improved". The list of improvements can be found in the docs .

-credited to

Difference between mysqli and mysql?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM