dongqin1167 2014-03-16 19:29
浏览 35
已采纳

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

I have a website, which you press a button and a popup DIV loads up.

On this DIV is a JQuery Validator form which submits to a separate PHP file.

The PHP logins to a database through MySQLi and adds a user. Whilst it does it, at each stage it does an echo message (the idea is that I know what its doing).

This leaves me with a white screen with several lines of information. Its fantastically useful but very ugly from the nice popup div registration.

Is there any way, at the end of the PHP it can redirect to another page assuming there was a blank div in it where the echo information can go, and I can jazz the remaining page up with HTML5 and CSS.

If so how do I get the echo messages into this div?

Thanks

Please see the snippet (which is working) below - but go easy on me as its only been a couple of weeks of learning.

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条回答 默认 最新

  • duanhe6799 2014-03-16 19:59
    关注

    If you really want a redirection, you will have to store your messages somewhere. I suggest you to save them in the user session. The workflow would be :

    • user do action (save form / get page : anything)
    • server treat the request and store a new "message" in a specific array in the user session (standard php $_SESSION) depending on the situation (error message ? success message ?). At this point you should store the message and its level (INFO/WARNING/ERROR/SUCCESS/etc)
    • server do a rediction (if needed)
    • create a method which :
      • retrieve all store message and delete them directly cause you want to display them only once
      • display them on your DIV
    • you're done

    The good thing with this worklow is that it will work even without a redirection as you separate clearly messages addition/storing and display.

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗