使用ajax和javascript登录表单

时间:2017-02-10 06:41:57

标签: javascript php ajax

如果用户名或密码不正确,我尝试使用ajax显示错误消息,如果正确,则转到tabeeb.html或secc.php。但是,不是转到正确的页面,而是在对话框中显示该页面,如img。怎么了?

这是表格

 <form method="post"   class="bounceInDown animated">
 <input type="text" name="u" id="u" placeholder="User Name" class="bbb"><br>
 <input type="password" name="p" id="p" placeholder="Password" class="bbb">     
 <input type="button" onClick="myFunction()"  name="loginbtn" class=" btn 
 btn-primary " value="Log in"> 
 </form>

这是myfunction()函数

function myFunction(){

var u = document.getElementById("u").value;
var p = document.getElementById("p").value;
var dataString = 'u1=' + u + '&p1=' + p ;

 if (u == '' || p == '') {
alert("Please Fill All Fieldsssssss");} 


else {
// AJAX code to submit form.
$.ajax({
type: "POST",
url: "ajaxjss.php",
data: dataString,
cache: false,
success: function(html) {

alert(html);
}
});
}
return false;

这是ajaxjss.php

<?php
// Fetching Values From URL
$u2 = $_POST['u1'];
$p2 = $_POST['p1'];

$connection = mysql_connect("localhost", "root", "password"); 
//   Establishing Connection with Server..
$db = mysql_select_db("project", $connection); // Selecting Database

if (isset($_POST['u1'])) {


// من تحت هو التجربة
$result = mysql_query("select * from test where student_name = '$u2' ") or 
 die("faild");

$row = mysql_fetch_array($result);


if($row['student_name'] == $u2 && $row['student_password']==$p2)
{ $NoOFLetter = strlen($u2);

$result = substr($u2, $NoOFLetter-3,  $NoOFLetter);
if($result == "doc"){header('Location: tabeeb.html'); exit; }
if($result == "sec"){header('Location: secc.php'); exit;}

 exit; }

else{ 
$error_message = "Error in username or password!";}

// كل اللى فوق هو التجربة

if(!$query){

echo "error";

}else{echo "Form Submitted succesfully";}}
mysql_close($connection); // Connection Closed
?>

1 个答案:

答案 0 :(得分:0)

现在检查

&#13;
&#13;
function myFunction(){

var u = document.getElementById("u").value;
var p = document.getElementById("p").value;
var dataString = 'u1=' + u + '&p1=' + p ;

 if (u == '' || p == '') {
alert("Please Fill All Fieldsssssss");} 


else {
// AJAX code to submit form.
$.ajax({
type: "POST",
url: "ajaxjss.php",
data: dataString,
cache: false,
success: function(html) {
  if(html=="error"){
    alert(html);
  }
  else{
    window.location.href="secc.php";
  }

}
});
}
return false;
&#13;
&#13;
&#13;