【问题标题】:PHP Basics - Echo messages in a redirected pagePHP 基础 - 在重定向页面中回显消息
【发布时间】:2014-03-16 19:29:51
【问题描述】:

我有一个网站,你按下一个按钮就会弹出一个 DIV 加载。

在这个 DIV 上是一个 JQuery Validator 表单,它提交到一个单独的 PHP 文件。

PHP 通过 MySQLi 登录数据库并添加用户。虽然它会这样做,但在每个阶段它都会发出一条回显消息(我的想法是我知道它在做什么)。

这给我留下了一个带有几行信息的白屏。它非常有用,但从漂亮的弹出 div 注册中非常难看。

有什么办法,在 PHP 结束时,它可以重定向到另一个页面,假设其中有一个空白 div,回显信息可以去,我可以用 HTML5 和 CSS 使剩余的页面变得活泼。

如果是这样,我如何将回显消息放入此 div?

谢谢

请看下面的 sn-p(它正在工作) - 但请放轻松,因为它只有几周的学习时间。

function webmailSignUp($db_connection,$db_con_table) //The function for the website REGISTER FORM
{
    $webmailFullName = $_POST['webmailFullName'];
    $webmailUserName = $_POST['webmailUserName'];
    $webmailExEmail = $_POST['webmailExEmail'];
    $webmailPhone = $_POST['webmailPhone'];
    $webmailDOB = $_POST['webmailDOB'];

    //Check that the fields are not empty
    if (checkBlankFieldsError($webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$webmailDOB) == false)
    {   
        echo "There are no empty Form Input Fields<br>";

        //Connecting to MySQL
        if (mysqli_connect_errno($db_connection))
        {
            echo "Failed to connect to MySQL database:" . mysqli_connect_error();
            echo "<br>";
        }
        else
        {
            echo "Connected to database<br>";

            //Check that there is no existing name in the table
            if (checkInField($webmailUserName,$db_connection,$db_con_table,"userName") == false)
            {   
                //Check DOB Field
                $dob = $webmailDOB; //or use for non-iso: convertDate($webmailDOB);
                echo "DOB is: $dob<br>";

                //Binding and Query to prevent SQL injection
                $query = "INSERT INTO $db_con_table(userFullName,userName,userExEmail,userPhone,userDOB) VALUES(?,?,?,?,?)";                    
                $requery = $db_connection->prepare($query);
                $requery->bind_param("sssss",$webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$dob);     

                if ($requery->execute()) 
                {
                    echo "$webmailUserName has been added to the Webmail Database<br>";
                }
                else 
                {
                    echo "bind failed on $webmailUserName <br>";
                }   

                //Close Database
                $db_connection->close();
                echo "Database is Closed.<br>";

            }
            else{echo "There is already a user registered with this username.  Please try a different one.<br>";}
        }       
    }
    else
    {
        echo "There is 1 or more empty input fields. Please try again.<br>";
    }
}

function checkInField($value,$db_connection,$db_con_table, $db_field)  // Checks a value is not within a database field
{       
    $query = "SELECT $db_field FROM $db_con_table WHERE $db_field='$value'";
    $result = $db_connection->query($query) or die($mysqli->error());

    // GOING THROUGH THE DATA
    if($result->num_rows > 0) 
    {
        while($row = $result->fetch_assoc()) 
        {
            echo "User $value found: '$row[$db_field]' in the table $db_con_table column $db_field<br>";
            return true;
        }
    }
    else
    {
        echo "User $value has not been found in the table $db_con_table column $db_field<br>";
        return false;
    }
}

function checkBlankFieldsError($field1,$field2,$field3,$field4,$field5) //Checks if form fields are blank
{
    $fields = array($field1,$field2,$field3,$field4,$field5);       
    $error = false;

    foreach($fields AS $fieldname)   //Loop trough each fieldname in the fields array
    {
        if(empty($fieldname)) 
        {
            $error = true;
        }
        else 
        {
        }
    }
    return $error;
}

function convertDate($aString) //Converts a String to a Date in Y-M-D format
{
    $date2 = DateTime::createFromFormat('m/d/Y', $aString);
    return $date2->format('Y-m-d');
}

//Main Code Sequence on form buttons
if(isset($_POST['webmailRegisterSubmit']))
{
    webmailSignUp($mysqli_db,$db_table);
    echo "End of Registration.<br>";
}
if(isset($_POST['webamilForgottenPWSubmit']))
{
    webmailForgottenPassword();
    echo "End of Password Reset Request.<br>";
}

【问题讨论】:

  • 我同意,是的,有,但是除了这些,没有什么可以说的了
  • “我的想法是我知道它在做什么”——所以这只是为了调试目的?由于这不会发生在您的实时站点上,因此“丑陋”不应成为问题。无论如何,您可以将该数据放入会话中,然后在其他地方显示它;或将调试输出写入文件。
  • 是的,我认为你是对的,我想我需要将它重定向到一个页面并将回声记录在一个文件中。给我两分钟,我会添加代码。
  • 要重定向,我是否使用:header("Location: ../webmailSuccess.php");
  • 我也可以简单地改变我所有的回声:

标签: php html redirect echo


【解决方案1】:

如果您真的想要重定向,则必须将您的消息存储在某个地方。我建议您将它们保存在用户会话中。工作流程是:

  • 用户执行操作(保存表单/获取页面:任何内容)
  • 服务器根据情况(错误消息?成功消息?)处理请求并在用户会话(标准 php $_SESSION)中的特定数组中存储新的“消息”。此时您应该存储消息及其级别(INFO/WARNING/ERROR/SUCCESS/etc)
  • 服务器执行重定向(如果需要)
  • 创建一个方法:
    • 检索所有商店消息并直接删除它们,因为您只想显示一次
    • 在您的 DIV 上显示它们
  • 大功告成

这个工作流的好处是即使没有重定向也可以工作,因为您可以清楚地分开消息添加/存储和显示。

希望对你有帮助

【讨论】:

  • 如果消息模板保持不变,我总是发现最好只在 URL 中传递一个参数以在下一页加载时加载消息并在需要时插入任何模板数据(用户名) .我被告知要避免在会话中存储类似的临时内容。
  • 我认为这是我将参数传递到另一个 php 文件的选项。因为我只需要:成功/用户已被使用/服务器错误。我将在调试日志 txt 中添加的细节
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
相关资源
最近更新 更多