dpf25323 2012-03-11 17:59
浏览 31
已采纳

困惑为什么代码只能用于ob_start();

I have added some server side validation to a dropdown box on one of my pages and when I did the page wouldn't work anymore. The code I added is as follows:

$show_form = true;        
if (isset($_POST['submit'])) {
    //All of the server side validations   
      $validator = new FormValidator();
    $validator->addValidation("hospital_name","dontselect=000","No facility was chosen");

    if ($validator->ValidateForm()) {
    // All the variables from the submission form   
    $userid = $_SESSION['user_id'];
    $hosp = $_POST['hospital_name'];
   header('Location: ../site_hospital' . $hosp . '/hospital_submitform.php?usr=' . $userid . '&&hosp=' . $hosp);
    exit;
    $show_form = false;
    } else {
    echo "<B style='color:red;'>The following errors occurred:</B>";
    $error_hash = $validator->GetErrors();
    foreach ($error_hash as $inpname => $inp_err) {
        echo "<p style='color:red;'>$inp_err</p>
";
    }
}}        
if (true == $show_form) {

Through pure chance I added ob_start(); as part of my debugging to the beginning of the page and suddenly my code worked properly but I have no idea why and I was hoping the community could throw out an educated guess as to why. When the code stopped working it would not execute my header command above, the page would simply refresh and not change location, when I added ob_start(); to the top of the page the page redirected as planned. So the overall question is why would the page not direct using the header command without ob_start? I'm sure alot more detail and code is necessary for a definitive answer but I'm hoping someone has run into this before or has an educated guess that may lead me to my own answers. Thanks for any insight.

  • 写回答

1条回答 默认 最新

  • doutuan4361 2012-03-11 18:02
    关注

    it's because you were writing to the output stream and preventing the header from working properly. once you started buffering other outputs, you removed the obstacle to the header's operation.

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

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么在R语言里下载Git上的miceco这个包吗
  • ¥15 GPT写作提示指令词
  • ¥20 如何在cst中建立这种螺旋扇叶结构
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥20 关于DAC输出1.000V对分辨率和精度的要求
  • ¥20 想写一个文件管理器,加载全部子文件夹后,要一级一级返回
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题
  • ¥15 哪位能做百度地图导航触点播报?
  • ¥15 请问GPT语言模型怎么训练?
  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角