如何在相同的php弹出窗体中显示错误消息

时间:2012-02-03 08:54:49

标签: php html validation popup

我试图制作一个弹出式php表单并登录弹出页面,幸运的是我从网上获得了一些关于如何设计和创建我所做的事情的笔记和资源。现在唯一的问题是每当我提交表单或登录弹出页面时,错误消息会将页面从弹出窗口返回到原始页面,这不是我想要的。我相信有一种方法可以在同一个登录/表单页面中进行验证,这可以在同一个弹出窗口中进行。任何想法怎么做??

期待您的回音。

如果需要代码或任何相关内容,我会在此处发布并更新原始问题。

更新#1:这是代码。附:这段代码介于身体之间

<?php

if ($username && $userid){
    echo "You are already logged in as <b>$username</b>. <a href='./member.php'>Click here</a> to go to the member page...or <a href='./logout.php'>Logout</a>";
}
else{
    $form = "<form action='./login.php' method='post' id='loginform'>
    <table>
    <tr>
        <td>Username:</td>
        <td><input type='text' name='user' /></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><input type='password' name='password' /></td>
    </tr>
    <tr>
        <td></td>
        <td><input type='submit' name='loginbtn' value='Login' /></td>
    </tr>
    <tr>
        <td><a href='./register.php'>Register</a></td>
        <td><a href='./forgotpass.php'>Forgot your password?</a></td>
    </tr>
    </table>
    </form>";

    if ($_POST['loginbtn']){
        $user = $_POST['user'];
        $password = $_POST['password'];

        if ($user){
            if ($password){
                require("connect.php");

                $password = md5(md5("kj87fiJAR46ufj".$password."Fj754456fj"));
                // make sure login info correct
                echo"$password";
                $query = mysql_query("SELECT * FROM users WHERE username='$user'");
                $numrows = mysql_num_rows($query);
                if ($numrows == 1){
                    $row = mysql_fetch_assoc($query);
                    $dbid = $row['id'];
                    $dbuser = $row['username'];
                    $dbpass = $row['password'];
                    $dbactive = $row['active'];

                    if ($password == $dbpass){
                        if ($dbactive == 1){
                            // set session info
                            $_SESSION['id'] = $dbid;
                            $_SESSION['username'] = $dbuser;

                            echo "You have been logged in as <b>$dbuser</b>. <a href='./member.php'>Click here</a> to go to the member page.";

                        }
                        else
                            echo "You must activate your account to login. $form";
                    }
                    else
                        echo "You did not enter the correct password. $form";
                }
                else
                    echo "The username you entered was not found. $form";

                //mysql_close();
            }
            else
                echo "You must enter your password. $form";
        }
        else
            echo "You must enter your username. $form";
    }
    else
        echo $form;
}
?>

并且有弹出窗口的代码。

更新#2:如果找到类似的tut,展示了如何使用弹出窗口构建完整的登录页面,那么我将从那里开始:enter link description here

谢谢,

1 个答案:

答案 0 :(得分:2)

使用ajax来执行此操作。

对于登录示例:

使用凭据发送和ajax请求

通过javascript执行操作,具体取决于服务器收到的结果。例如,如果请求结果为false则显示错误,如果结果为true,则关闭弹出窗口并在需要时重定向父窗口。

只是一个例子(既没有测试也没有工作)来指导你这样做

1)在弹出窗口中包含的js代码中包含ajax请求

//create the request object
if (window.XMLHttpRequest) {
  request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
  request = new ActiveXObject("Microsoft.XMLHTTP");
}

//send the request (you can do it via post or get)
if (request) {
  request.onreadystatechange = checkAjaxResponse;
  request.open("POST", "your_server_file_that_generates_ajax_response.php", true);
  request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  request.send("post params you want to include like param1=value");
}

/**
* Ajax callback function, this should handle your ajax response
*/
function checkAjaxResponse() {

  //check what was de ajax response
  if (request.readyState == 4) {
    if (request.status == 200) {
      xml_response = request.responseXML;   //you can do this with xml, json, ...
    }
  }

  //manipulate your response as needed, and take actions depending on if result is correct or not
  if (manipulated_data_from_the_response !== 'the answer you specified as right') {
    close_popup();
  }
  else {
    show_wrong_credentials_message();
  }
}

2)实现您的php文件(或首选服务器端语言)以登录用户并生成http响应 //your_server_file_that_generates_ajax_response.php

<?php

  function authenticateUser($user, $password) 
  {
    $query = "SELECT user_id FROM users_table WHERE user = $user AND password = $password LIMIT 1";

    //connect to your database, then make the query 
    $result = mysql_query($query);
    $row = mysql_fetch_row($result);

    //return the response you want    
    if ($row['user_id']) {
      $res = true; 
    }
    else {
      $res = false;
    }

    return $res;
  }


  //main actions
  echo authenticateUser($_POST['user'], $_POST['password']);

?>