dousi0144 2012-03-29 22:39
浏览 7

为什么这个表单导航我到错误页面?

I have this code below where what should happen is that if the $sessionMinus (which is a number) is not the same as ($_SESSION['initial_count']), then the form submits to itself which I want to happen BUT if $sessionMinus equals the same number as ($_SESSION['initial_count']) then I want the form to submit to the create_session2.php.

But the problem is that if I click on the submit button, no matter what happens, it keeps taking me to an error page, no matter if the numbers match or not. Why is this happening? (This happens if I use both == and === signs).

Below is code:

<?php
session_start();

    if ($sessionMinus == $_SESSION['initial_count']){

        $action = ($sessionMinus == $_SESSION['initial_count']) ? "create_session2.php" : $_SERVER['PHP_SELF'];

    }

?>


<body>    
        <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" onsubmit="return validateForm(this);" >

...

</body>

Solution is Below:

<?php

session_start();

if ($sessionMinus == $_SESSION['initial_count']){ 

    $action = 'create_session2.php'; 

}else if($sessionMinus != $_SESSION['initial_count']){ 

    $action = $_SERVER['PHP_SELF']; 

}

?>

<body>

            <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" onsubmit="return validateForm(this);" >



...
</body>
  • 写回答

1条回答 默认 最新

  • doutao4480 2012-03-29 22:55
    关注

    There are a few things this could be:

    1. Check your php_error.log file for more debug information, it will usually have some info about what is causing the error.

    2. It looks like your PHP statements and your HTML aren't separated properly (assuming these lines of code are one right after another). You need a ?> tag after the if block but before the <form> tag. So your code would be:

      <?php /* <- this should be above this line already in your code */
      
      if ($sessionMinus == $_SESSION['initial_count']) {
          $action = ($sessionMinus == $_SESSION['initial_count']) ? "create_session2.php" : $_SERVER['PHP_SELF'];
      }
      /* you need to add the tag below */
      ?>
      <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" onsubmit="return validateForm(this);" >
      
    3. Where you assign the $action variable, consider wrapping the expression in parentheses, like this:

      $action = (($sessionMinus == $_SESSION['initial_count']) ? "create_session2.php" : $_SERVER['PHP_SELF']);
      

    and also changing PHP_SELF to REQUEST_URI (as PHP_SELF gives the file path relative to the document root, not the full path including document root), and adding the full path to the create_session2.php file:

        $action = (($sessionMinus == $_SESSION['initial_count']) ? $_SERVER['SERVER_NAME'] . "/create_session2.php" : $_SERVER['REQUEST_URI']);
    

    You shouldn't have to add the SERVER_NAME bit, but I always prefer absolute paths in my links.

    If you can provide any more source code, it would be very helpful.

    -T

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题