duangan4406 2012-11-04 16:10
浏览 17
已采纳

如何在脚本中间结束PHP脚本,但显示页面的其余部分

I have my long PHP script with some JavaScript at the end. In the middle of the script if an 'if' statement is true then is their a way to stop the rest of the PHP script and output the rest of the HTML part of the page. Here's a little snippet:

... more code ...
        case 2:
            $e=$_POST['email'];
            $n=$_POST['name'];
            mysql_connect($dbloc,$dbuser,$dbpass);
            mysql_select_db($dbname)or die(mysql_error());
                $query="SELECT * FROM users WHERE email='$e'";
            $re=mysql_query($query);
            $numm=mysql_numrows($re);
            if($numm>=1){
                $js="alert('Sorry but your email is currently in use. Please try again with a different email address.');";
                $url="./";
                exit();
            }
            else{
                $_SESSION['rEmail']=$e;
                $_SESSION['rName']=$n;
            }
        break;
... more code...

<html goes here />

I originally thought exit() would end just the PHP portion but still give back the rest of the page but that does not seem to be the case.

  • 写回答

1条回答 默认 最新

  • dongpan3001 2012-11-04 16:17
    关注

    exit() is performing what it was maid to do. It is exiting the your PHP code right from the point where it is called.

    Your concept that it'll print the remaining of HTML is wrong. The whole of PHP code is taken as a PHP script, with all your logic/PHP functions tagged inside <?php and ?> and the text outside is just like documentation, and is outputted as it is.

    With the exit inside the if statement, the code/text after it isn't processed at all. I'd suggest that you use a break statement instead.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应