【问题标题】:How can I change label's text of one page from another php page?如何从另一个 php 页面更改一个页面的标签文本?
【发布时间】:2013-04-09 01:41:42
【问题描述】:

问题是:我正在构建一个简单的登录php页面,用户应该输入用户名和密码,然后单击“登录”按钮。此按钮将输入的值提交到另一个处理数据库并确保用户已注册的 php 页面。现在如果他/她没有注册,则页面返回登录页面,但在此之前,它会更改一些标签的文本以通知用户输入的用户名或密码错误。

我的问题是:第二个 php 页面无权访问第一个的元素!

这是我到现在为止使用的代码! 第二个php页面:调用LoginSubmit.php:

if($row = mysqli_fetch_array($result))
{
    echo "<meta http-equiv='refresh' content='0;URL=../Home.php'>";
}
else
{
    echo"<script type='text/javascript'>";
echo"parent.document.getElementById('FailureText').innerHTML = 'Yours user name or password are wrong!!';";
echo "</script>";
echo "<meta http-equiv='refresh' content='0;URL=../Login.php'>";
}

在第一页(称为 Login.php),标签的定义格式如下:

<td align="center" class="LiteralProb" colspan="2" style="color:Red;">
    <label ID="FailureText"></label>
</td>

它是空的,似乎不存在标签,但是当出现登录错误时,应该显示给用户的消息!

任何帮助,请! :) ??

【问题讨论】:

  • 您确实想查看 AJAX。或者在一个脚本中处理所有内容并重新加载整个页面。

标签: php javascript html dom


【解决方案1】:

将值存储为会话中的某种“flash message”。在LoginSubmit.php 中使用:

// at the top of your script
session_start();

// when you know an error occurred
$_SESSION['failure_message'] = 'Yours user name or password are wrong!!';

在其他页面上使用:

// at the top of your script
session_start();

// in your HTML part
<label ID="FailureText">
    <?php print ( isset($_SESSION['failure_message']) ? $_SESSION['failure_message'] : '' ); ?>
</label>

// at the bottom of your script remove the value from the session again
// to avoid that it's displayed twice
unset($_SESSION['failure_message']);

【讨论】:

    【解决方案2】:

    您的登录页面绝非简单;-)

    这个怎么样?

    if (!$validLogin) {
        header('Location: http://www.example.com/login?err');
        exit;
    }
    

    ...和:

    <? if( isset($_GET['err']) ){ ?>
        <div>Invalid username/password</div>
    <? } ?>
    

    【讨论】:

      【解决方案3】:

      这可以通过一些不同的方式来完成:

      1- 您可以使用会话变量来存储要在 php 脚本之间共享的值:

      session_start();
      $_SESSION['val1']=$value1; //set the value
      

      你可以这样检索它:

      //receive on the other script
      session_start();
      $value1=$_SESSION['val1'];
      

      2- 在将用户发送到登录脚本时,您可以使用 GET(URL)传递变量。

      header("location: first_script_url?error=some_error_message");
      

      你在登录脚本上这样检索它:

      $err_msg=$_GET['error'];
      

      3- 您可以使用 AJAX 进行登录过程,因此您无需将用户从一个脚本重定向到另一个脚本,而是调用第二个脚本,并根据第二个脚本返回值告诉用户是否存在是否有任何错误:

      使用 Jquery,如果我们要传递用户信息,最好使用 POST,也最好使用 HTTPS(无论您选择哪种方法,都应该这样做),或者至少对密码使用加密功能(这个不是 100% 安全的):

      $.post("second_url.php", {user: username, pass: password}), 
      function(data){
           //data contains anything second_url.php echoed. So you want to echo a 1 for example if everything went ok.
           if(data == 1){
                 //OK
           }else{
                 //Something went wrong, show the user some error.
           }
      });
      

      用户永远不会离开第一个脚本,因此您的 javascript/Jquery 中拥有所有变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-17
        • 2018-08-11
        • 1970-01-01
        • 1970-01-01
        • 2023-01-19
        • 2014-10-05
        • 1970-01-01
        相关资源
        最近更新 更多