从不同的位置/文件回显页面上的消息/错误

时间:2014-01-26 08:47:48

标签: php html

好的,我有一个名为profile.php的脚本,我希望用户从另一个html页面或php脚本(如user_settings.php)与profile.php“表单输入”进行交互。

我希望仅在profile.php中显示错误,以便在user_settings.php页面上显示/回显/打印。我试过以下

---- --- profile.php

 <?php ob_start(); require_once ("core/init.php"); ?>
 <?php require ("includes/overall/header.php"); ?>

 <?php 
      if(has_access($session_user_id, 1) === true){
            $message[] = '<p class="admin">  **Admin**</p>';
      }
 ?>

   <div  id="mainInfo">
   <p class="profile"> Hello <?php echo $user_data['first_name'], '!' .output_message($message)  ;?> </p><br /><br />
  <?php

        if(isset($_FILES['image']) === true)
        {
            if(empty($_FILES['image']['name']) === true)
            {
                $errors_image[] = 'Please choose a file';
            }
            else
            {
                $allowed = array('jpg', 'jpeg', 'gif', 'png');  

                $file_name = $_FILES['image']['name'];
                $file_extn = strtolower(end(explode('.', $file_name)));
                $file_temp = $_FILES['image']['tmp_name'];

                if(in_array($file_extn, $allowed) === true)
                {
                    change_profile_image($session_user_id, $file_temp, $file_extn);

                    header('Location: profile');
                    exit();
                }
                else
                {
                    $errors_image[] = 'Incorrect file type. Allow: ';
                    echo implode(', ', $allowed);
                }
            }
        }
        if(empty($user_data['image']) === false)
        {
           echo '<p class="photo"><image src="', $user_data['image'],'" alt="', $user_data['first_name'], '\'s Profile Image"></p> <br>';
        }
    ?>



    <p class="usr_sttngs"> <a href="usr_sttngs">Update Info!</a></p><br />


    </div>  
    <?php require ("includes/overall/footer.php"); ?>

-------- usr_sttngs.php ------------

   <?php ob_start(); 

  require_once ("core/init.php");
  ini_set('display_errors', 'On'); error_reporting(E_ALL); 
  protect_page(); ?>

   <?php 

if(empty($_POST) === false)
{
    $required_fields = array('current_passwd','passwd', 'passwd_again');
    foreach($_POST as $key => $value)
    {
    if (empty($value) && in_array($key, $required_fields) === true)
        {
            $errors[] = '<p class="postad_msg">Fields marked with * are required.</p>';
            break 1;
        }
    }


    if (sha1($_POST['current_passwd']) === $user_data['passwd'])
    {
        if(trim($_POST['passwd']) !== trim($_POST['passwd_again']))
        {
            $errors[] = '<p class="postad_msg">New passwords DO NOT match!</p>';
        }
        else if (strlen($_POST['passwd']) < 6)
        {
            $errors[] = '<p class="postad_msg">New passwords must be at least 6 characters.</p>';
        }
    }
    else
    {
        $errors[] = '<p class="postad_msg">Your current password is incorrect!</p>';
    }   
}
?>

  <?php require ("includes/overall/header.php"); ?>

    <div id="mainInfo">

                    <br /> <br /><p3>Change Password!</p3> <br /> <br />
                <?php 

                    if(isset($_GET['success']) && empty($_GET['success']))
                    {
                        $message[] = '<p class="postad_msg">Your password has been changed successfully.</p>';
                    }

                        if(empty($_POST) === false && empty($errors) === true)
                        {      
                            change_passwd($session_user_id, $_POST['passwd'] );
                            //redirect
                            header('Location: usr_sttngs?success');                                         
                            //exit
                            exit();                                                 
                        }
                        else if(empty($errors) === false)
                        {
                            output_errors($errors); 
                        }
                ?>     

        <form method="post" action="usr_sttngs">
            <fieldset>              
                <label for="currentpasswd">Current password* </label>
                <input placeholder="Type your current password" type="password" name="current_passwd" size="30" maxlength="50" value="<?php echo htmlentities($current_passwd); ?>" /><br /><br />
                <label for="last_name">New Password* </label>
                <input placeholder="New Password" type="password" name="passwd" size="30" maxlength="50" value="<?php echo htmlentities($new_passwd); ?>" /><br /><br />
                <label for="email">New Password Again* </label>
                <input placeholder="Retype New Password"  type="password" name="passwd_again" size="30" maxlength="50" value="<?php echo htmlentities($new_passwd_again); ?>" />
            </fieldset>

            <fieldset class="center">
                <input class="submit" type="submit" name="submit" value="Change Password" />
                 <?php echo output_errors($errors); ?>
                 <?php echo output_message($message);?>

            </fieldset>





        </form>

    <p4>Change profile photo!</p4> <br /> <br />

    <form action="profile" method="post" enctype="multipart/form-data">
        <fieldset class="center">
            <input type="file" name="image"> <input type="submit">

            <?php echo output_errors($errors_image); 
                  require_once ('$_SERVER["DOCUMENT_ROOT"]\profile.php');
            ?>

        </fieldset>          
    </form>



  <br /><br /><br /><p4>Upload your resume!</p4> <br /> 

    <form action="profile" method="post" enctype="multipart/form-data">
        <fieldset class="center">
            <input type="file" name="resume"> <input type="submit">
        </fieldset>          
  </form>

 </div>

 <?php require ("includes/overall/footer.php"); ?>

我尝试了以下帖子Want to echo a php variable from another page上读到的内容,但我没有得到预期的结果。所以我尝试包含required_once'profile.php';在user_setting.php文件的最顶部,这只是给我一个空白页面。任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

在显示任何HTML之前必须调用Require或Require_once。

尝试将require_once放在文件的开头。

<强> EDITED

尝试使用以下测试代码,看看是否可以使其正常工作。

profile.php应包含以下内容:

<?php

echo "profile.php is displaying correctly";

?>

user_settings.php应包含以下内容:

<?php

require_once("profile.php");

?>

看看你是否可以使用这个基本代码。发布您的结果

当我尝试上面的代码时,我收到了轰鸣声页面:

enter image description here