PHP基础知识 - 重定向页面中的Echo消息

时间:2014-03-16 19:29:52

标签: php html redirect echo

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

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

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

这给我留下了一个白色的屏幕,上面有几行信息。它非常有用,但是从漂亮的popup div注册中非常难看。

有没有办法,在PHP结束时它可以重定向到另一个页面,假设回显信息可以在其中有一个空白div,我可以用HTML5和CSS为剩余的页面添加爵士乐。

如果是这样,我如何将回音信息传入此div?

由于

请看下面的代码片段(工作正常) - 但是对我来说很简单,因为它只需要几周的学习时间。

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>";
}

1 个答案:

答案 0 :(得分:1)

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

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

这项工作的好处在于,即使没有重定向,它也能正常工作,因为你可以清楚地分开添加/存储和显示消息。

希望这有帮助