0

我有两个页面,一个将值发送到 php 页面的 html,然后,php 页面在 html 页面中以 div 形式返回结果。

在第一个测试中它只有一条错误消息,并且在 else 子句中它必须重定向到另一个 html 页面,因此 php 页面中的脚本将以 html 显示。我用过javascript,但它不适合我。

这是我使用的所有代码:

if (mysqli_num_rows($result)==0)
{
    echo" Code erroné ";
}
else
{
    header('location:infosInt.html');
    //echo "<script> window.open('infosInt.html')</script>";
    //echo "<script> window.location = 'infosInt.html'</script>";
}

编辑

我的html页面:

<!DOCTYPE html>
<head>
  <script type="text/javascript" src="jquery.js"></script>
     <script src="jquery-ui.js" charset="ISO-8859-1"></script>
          <script src="jquery-1.9.1.js" charset="ISO-8859-1"></script>
<script  type="text/javascript" src="jquery.crypt.js" ></script>
   <LINK rel="stylesheet" type="text/css" href="android.css">

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Login Form</title>
  <script>

function connexion(evt)
{
var x=document.getElementById('code');
var cde = x.value;
 var str = $().crypt({
  method: "md5",
  source: cde
  });
if (cde=="")
  {
 // document.getElementById("txtHint").innerHTML="";
  return;
  } 
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)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  var url ="http://localhost/filename/page.php";
  var params="q="+ str;
        xmlhttp.open("POST",url,true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       xmlhttp.setRequestHeader("Content-length", params.length);
       xmlhttp.setRequestHeader("Connection", "close");

        xmlhttp.send(params);     

};
  </script>
</head>
<body>
  <div class="login">
      <p></p>
    <form method="" action="" id= "codeform"  onsubmit="connexion();return false;">
     <p> &nbsp &nbsp &nbsp &nbsp</p>

      <input type="password"   id="code" ></p>

      <p class="submit"><input type="submit"  id="connect" value="Connexion" ></p>

    </form>
  <div id ="txtHint"></div>
  </div>
</body>
</html>

这是page.php:

<?php

$host     = "localhost";
$port     = port;
$socket   = "";
$user     = "root";
$password = "";
$dbname   = "base";


$con = new mysqli($host, $user, $password, $dbname, $port, $socket);
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

$q =$con->real_escape_string($_POST['q']);

mysqli_select_db($con,"ajax_demo");

$sql="query where column ='".$q."'"; // ex
$result = mysqli_query($con,$sql);

if (mysqli_num_rows($result)==0)
  {
 echo" Code erron&eacute; ";
  }
else
{
echo"<head><meta http-equiv='refresh' content='5; URL=infosInt.html'></head>";}

mysqli_close($con);
?>
4

1 回答 1

0

问题是,您正在使用 ajax 调用您的 php 脚本,因此您不能使用header('Location: ...')或 html 重定向。您应该在 php 脚本中返回一个标志,并在使用 javascript 获得响应时对其进行测试:

PHP(在末尾page.php):

if (mysqli_num_rows($result)==0)
{
    echo "Code erron&eacute;";
}
else
{
    echo "redirect"; // the flag
}

Javascript(在xmlhttp.onreadystatechange活动中):

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
    if (xmlhttp.responseText == 'redirect') { // if flag is sent we redirect
        document.location.href = 'infosInt.html';
    } else {
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
    }
}
于 2013-09-18T21:03:58.090 回答