在与查询不同的位置显示查询结果


Displaying query results in a different place from the query

基本上我有一个HTML表单,链接到一个php文件在不同的位置为它的行动,目前我正在使用的形式来更新用户配置文件,然后将它们发送回editprofile.php。基本上在editprofile.php的顶部,如果他们已经提交了查询,我想显示"配置文件更新"或"更新失败"的结果,问题是我不能锻炼如何显示查询结果时,查询是在不同的文件。

I tried to do this;

<?php
   if(!$query)
   {
     echo '<div class="editfail">Profile failed to update!</div>';
   } 
   else 
   {
     echo '<div class="editsuccess">Profile successfully updated!</div>';
   }
?>

除了这个问题,查询还没有在这个页面上运行,它是从另一个页面运行,然后使用标题重定向回editprofile页面,所以我怎么能显示与上面相同的结果,当查询正在从另一个位置执行?

可以在重定向文件时发送参数。

例子

如果(mysql_query (update_query美元)){

     header('location:editprofile.php?msg="success to save"');
}
else
{
    header('location:editprofile.php?msg="failed to save"');
} 

或者你也可以发送flag

if(mysql_query($update_query))
{
 header('location:editprofile.php?flag=0');
}else
{
header('location:editprofile.php?flag=1');
}

检查editprofile.php文件中flag的值,以显示正确的消息

你不应该乱动标题fxn,除非你需要-根据output_buffer设置等,它们可能是一个痛苦:

你可以做你想做的一切-在一个单一的页面:

所以像这样的东西-作为一个共同的惯例,并在一定程度上安全,你应该发布表单本身-你可以从其他页面集成任何其他到通过/失败配置文件逻辑块:

  <?php
   $query = htmlentities($_POST['profiletext']); #sanitize avec tu code du jour
   if(!$query || $query != 'someacceptablevalue))
   {
     #If it's not posted, or its not a good value, tell them it failed
     # and redisplay the form to try again
     $query_msg = '<div class="editfail">Profile failed to update!</div>';
     $profile_form = "<div_class='profile_rest_of_page stuff'>
           <form action='#' method='post'>
             <input type='text' id='profiletext' name='profiletext/>
           </form>
         </div>";
   } 
   else 
   {
     # They did it - Success, and link to next step
     $query_msg = '<div class="editsuccess">Profile successfully updated!</div>';
     $profile_form = 'No form needed - you did it';
   }
   #One block below handles all in 1 page with above logic:
   echo "<body>
          <div class='profile_message_container'>
             $query_msg
          </div>
         <div_class='profile_rest_of_page stuff'>
             $profile_redo<br/> You did it <a href='next'>next</a>
         </div>
        </body>
        ";       
  ?>

有两种方法:

  1. 在链接中像GET一样发送查询结果,可以被篡改

  2. 在与表单相同的页面中处理表单,如下所示

if(isset($_POST['some_name'])) {
    // Process form
} else {
   // Display form
}