weixin_33725272 2014-09-05 04:13 采纳率: 0%
浏览 53

Ajax responseText不起作用

In my ajax i have a var named ajax for some reason when I run ajax.respondText.

In the php code I ask it to return the username of the one log in. Well it is echoing out the name of the user but is also echoing of the header of my website in the mix as you can see here - Andrew at the bottom of the first set of code. That is the only thing that should be echoed out not the html that is ahead it.

<DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Better Gamerz United</title>
<link rel="icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<header>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a title="" class="navbar-brand " href="index.php">
        <img src="img/BGU Logo.png" />
        </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="index.php">Home</a></li>
        <li><a href="#">Recrutment</a></li>
        <li><a href="servers.php">Servers</a></li>
        <li><a href="forums.php">Forums</a></li>
        <li><a href="contactus.php">Contact Us</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a class="navbar-link" href="login.php">Login</a></li>
        <li><a class="navbar-link" class="navbar-link"class="navbar-link" href="signup.php">Register</a></li>

      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-->
</nav>
</header>

Andrew

What could cause my header to be included in to my response so I did went alittle deeper and ran a console.log just the var ajax. which provided me this

XMLHttpRequest {statusText: "OK", status: 200, responseURL: "http://www.bettergamerzunited.com/login.php", response: "<DOCTYPE html>↵<html>↵<head>↵   <meta charset="UTF-8…iv><!-- /.container-->↵    </nav>↵</header>↵↵ Andrew", responseType: ""…}onabort: nullonerror: nullonload: nullonloadend: nullonloadstart: nullonprogress: nullonreadystatechange: function () {ontimeout: nullreadyState: 4response: "<DOCTYPE html>↵<html>↵<head>↵ <meta charset="UTF-8">↵   <title>Better Gamerz United</title>↵  <link rel="icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵  <link rel="shortcut icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵ <link rel="stylesheet" href="css/bootstrap.css">↵ <link rel="stylesheet" href="css/main.css">↵</head>↵<body>↵   <header>↵ <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">↵     <div class="container">↵        <!-- Brand and toggle get grouped for better mobile display -->↵      <div class="navbar-header">↵        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">↵            <span class="sr-only">Toggle navigation</span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵         </button>↵        <a title="" class="navbar-brand " href="index.php">↵            <img src="img/BGU Logo.png" />↵           </a>↵     </div>↵↵        <!-- Collect the nav links, forms, and other content for toggling -->↵        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">↵       <ul class="nav navbar-nav">↵            <li class="active"><a href="index.php">Home</a></li>↵         <li><a href="#">Recrutment</a></li>↵          <li><a href="servers.php">Servers</a></li>↵           <li><a href="forums.php">Forums</a></li>↵         <li><a href="contactus.php">Contact Us</a></li>↵        </ul>↵        <ul class="nav navbar-nav navbar-right">↵           <li><a class="navbar-link" href="login.php">Login</a></li>↵<li><a class="navbar-link" class="navbar-link"class="navbar-link" href="signup.php">Register</a></li>↵↵          </ul>↵      </div><!-- /.navbar-collapse -->↵   </div><!-- /.container-->↵  </nav>↵</header>↵↵ Andrew"responseText: "<DOCTYPE html>↵<html>↵<head>↵  <meta charset="UTF-8">↵   <title>Better Gamerz United</title>↵  <link rel="icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵  <link rel="shortcut icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵ <link rel="stylesheet" href="css/bootstrap.css">↵ <link rel="stylesheet" href="css/main.css">↵</head>↵<body>↵   <header>↵ <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">↵     <div class="container">↵        <!-- Brand and toggle get grouped for better mobile display -->↵      <div class="navbar-header">↵        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">↵            <span class="sr-only">Toggle navigation</span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵         </button>↵        <a title="" class="navbar-brand " href="index.php">↵            <img src="img/BGU Logo.png" />↵           </a>↵     </div>↵↵        <!-- Collect the nav links, forms, and other content for toggling -->↵        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">↵       <ul class="nav navbar-nav">↵            <li class="active"><a href="index.php">Home</a></li>↵         <li><a href="#">Recrutment</a></li>↵          <li><a href="servers.php">Servers</a></li>↵           <li><a href="forums.php">Forums</a></li>↵         <li><a href="contactus.php">Contact Us</a></li>↵        </ul>↵        <ul class="nav navbar-nav navbar-right">↵           <li><a class="navbar-link" href="login.php">Login</a></li>↵<li><a class="navbar-link" class="navbar-link"class="navbar-link" href="signup.php">Register</a></li>↵↵          </ul>↵      </div><!-- /.navbar-collapse -->↵   </div><!-- /.container-->↵  </nav>↵</header>↵↵ Andrew"responseType: ""responseURL: "http://www.bettergamerzunited.com/login.php"responseXML: nullstatus: 200statusText: "OK"timeout: 0upload: XMLHttpRequestUploadwithCredentials: false__proto__: XMLHttpRequest

Which the response is my header.

What I am asking is to please view my code below and trying to help me understand why this is happening. I'm confused and I have tried everything that I can to fix it.

File WIth Problem

<?php 
include 'core/init.php'; 
include 'includes/overall/header.php'; 

// If user is already logged in, header that weenis away
if($user_ok == true){
header("location: user.php?u=".$_SESSION["username"]);
exit();
}

// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST["e"])){
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($mysqli, $_POST['e']);
$p = md5($_POST['p']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
    echo "login_failed";
exit();
} else {
// END FORM DATA ERROR HANDLING
    $sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_row($query);
    $db_id = $row[0];
    $db_username = $row[1];
$db_pass_str = $row[2];
    if($p != $db_pass_str){
        echo "login_failed";
  exit();
    } else {
        // CREATE THEIR SESSIONS AND COOKIES
        $_SESSION['userid'] = $db_id;
        $_SESSION['username'] = $db_username;
        $_SESSION['password'] = $db_pass_str;
        setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
        setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
    setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE); 
        // UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
        $sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
  $query = mysqli_query($mysqli, $sql);
        echo $db_username;
      exit();
    }
 }
 exit();
}
?>
<script>
function emptyElement(x){
_(x).innerHTML = "";
}
function login(){
var e = _("email").value;
var p = _("password").value;
var status = _("status");
if(e == "" || p == ""){
    status.innerHTML = "Fill out all of the form data";
} else {
    _("loginbtn").style.display = "none";
    status.innerHTML = 'please wait ...';
    var ajax = ajaxObj("POST", "login.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText == "login_failed"){
                            status.innerHTML = "Login unsuccessful, please try again.";
                            _("loginbtn").style.display = "block";
                        } else {
                            console.log(ajax.responseText);     //debugging use   
                            window.location = "user.php?u="+ajax.responseText; // This is where                      the header is being added. I need the word anderw here with out my header being included. 
                        }
        }
    }
    ajax.send("e="+e+"&p="+p);
}
}
</script>
<html>
<div class="container background">
<p class="heading-title">Log In</p>
<form id="loginform" class="form-horizontal" onsubmit="return false">
    <div class="form-group">
        <label for="email" class="col-sm-2 control-label">Email Address</label>
        <div class="col-sm-10">
            <input type="test"10 name="email" onfocus="emptyElement('status')" maxlength="88" class="form-control" id="email" value="" autocomplete="off" placeholder="Please enter your email">
        </div>
    </div>
    <div class="form-group">
        <label for="password" class="col-sm-2 control-label">Password</label>
        <div class="col-sm-10">
            <input type="password" name="password" onfocus="emptyElement('status')" maxlength="16" class="form-control" id="password" value="" autocomplete="off" placeholder="Please enter your password">
            <li class=""><a class="navbar-link" href="#">Forgot your password?</a></li>
    </div>
    </div>
    <div class="form-group">
    <div class="col-sm-offset-2 ">
        <span class='pull-right text-danger' id="status"></span>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" id="loginbtn" onclick="login()" class="btn btn-primary pull-right">Log In</button><br><br>
    </div>
  </div>
</form> 
</div>
</html>
<?php
include 'includes/overall/footer.php';
?>
  • 写回答

1条回答 默认 最新

  • weixin_33747129 2014-09-05 05:03
    关注

    This is coming from either of the following two includes at the top of your php script:

    include 'core/init.php'; include 'includes/overall/header.php';

    So restrict the unwanted output or file inclusion in time of an ajax call.

    评论

    报告相同问题?

    悬赏问题

    • ¥15 请教一下simulink中S函数相关问题
    • ¥15 Hadoop中eclipse运行问题
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
    • ¥40 基于51单片机实现球赛计分器功能
    • ¥15 cs2游戏画面卡住,应用程序sid与指挥者sid不匹配
    • ¥15 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.