douyulv6921 2012-07-16 13:04
浏览 116
已采纳

php返回一个完整的html文件而不是字符串(ajax)[关闭]

I'm working on a website which uses AJAX and I've run into a dead end. My javascript has a function named validate which takes from the "name" and "password" fields and sends it over to php, waiting for a response.

function Validate() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            if (xmlhttp.responseText.value == 'Invalid'){

                $("#box").effect('shake',150);  

                document.getElementById("validationText").innerHTML = xmlhttp.responseText;
            }
            else if (xmlhttp.reponseText.value == 'Success!'){  
            console.log(xmlhttp.responseText.value)
                document.getElementById("validationText").innerHTML = xmlhttp.responseText;


            }
        }
    }

    var name = document.getElementById("name").value;
    var password = document.getElementById("password").value;

    var arg = "JqueryPhp.php?name=" + name + "&password=" + password;

    xmlhttp.open("GET", arg, true);
    xmlhttp.send();
} 

The php file should return either "Success!" or "Invalid" depending on the situation.

<?php

    $con = mysql_connect("localhost","root","root");

    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("collegedatabase", $con);

      $getUserName = $_GET["name"];
      $getPassword = $_GET["password"];

      $result = mysql_query("SELECT * FROM `admin`");
      $Success = false;

      /*    if the user entered does not exist, run the function which shakes the screen and add a bit of text to say that the username is invalid  */

        while ($row = mysql_fetch_array($result)){
            if ($row['userName'] == $getUserName && $row['password'] == $getPassword){
                echo "Success!";
                $Success = true;
                break;
            }
        }
        if($Success === false) {
            echo ('Invalid');
        }


    ?>

Instead, it returns a whole html file, with the string inside of a body tag. the response which I recorded from firebug is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>



<body>

Invalid</body>

</html>
  • 写回答

2条回答 默认 最新

  • douyun3799 2012-07-16 13:12
    关注

    Put an exit; at the end of your PHP code.

    You probably have something running at the end that is causing a full html page to render.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度