首页 > 解决方案 > 如果用户已经存在于数据库中,我想通过 php get 方法发送错误消息,然后在没有 GET 变量的情况下重置 url

问题描述

通过 url 获取错误消息后,我通过 p 标签抛出错误消息

然后使用 window.location.pushstate({}, "Hide", " http://localhost/shravari/project1/register.html ") 方法获得一个干净的 url 而没有错误消息

这是我的 php 代码

header('Location:../register.html?error=1');

这是我的 JavaScript 代码

var currentUrl = $(location).attr('href');  
    if (currentUrl.match(/error=1/g)) {
       $('#erroru').html('User already exists, please <a href="login.html">login</a> to access the page');
        $('#erroru').css('color','red');
        $('#erroru').css('text-algin','center');
        // window.location.assign('http://localhost/shravari/project1/register.html'); 
        window.history.pushState({}, "Hide", "http://localhost/shravari/project1/register.html");

    }

window.location.assign(' http://localhost/shravari/project1/register.html '); 在 mac 系统上运行良好,错误显示在 p 标签中,并且 url 不包含 get 方法错误变量,但在 windows 上不起作用

和 window.history.pushState({}, "Hide", " http://localhost/shravari/project1/register.html "); 适用于windows系统,错误显示在p标签中,url不包含get方法错误变量,但在mac上不起作用并且在两个系统中我都使用了chrome

标签: javascriptphp

解决方案


使用 php 会话。

您可以将会话定义为 session_start();

然后您可以通过会话发送错误消息

例如:$_SESSION['error'] = "你的错误信息";


推荐阅读