doushou1298 2015-04-08 01:32
浏览 125
已采纳

Javascript重定向不使用IFrame,没有IFrame的Works

I am using a PHP form to collect information. I want it to redirect to a page on the same domain when the submit button is pushed. It works fine if you go to the php form, but not the iframed php form. The code is below.

define( 'PHPFMG_REDIRECT', 'http://propertytaxdfw.com/thank-you-sign-up.html' );
function phpfmg_thankyou(){
// move the redirect to phpfmg_thankyou() to get around the redirection within an iframe problem
        /*
        $redirect = PHPFMG_REDIRECT;
        if( strlen(trim($redirect)) ):
            header( "Location: $redirect" );
            exit;
        endif;
        */
    phpfmg_redirect_js();
?>

<!-- [Your confirmation message goes here] -->
    <br>

    <b>It Worked!</b>
    <br><br>

<?php

} // end of function phpfmg_thankyou()

I think that is where the problem is. I can provide additional code if needed. I appreciate your help in advance. I have read a lot of things on here trying to find an answer, but I feel like I am stuck.

The form: http://propertytaxdfw.com/SignUpFormFiles/form.php Page where iframed: http://propertytaxdfw.com/sign-up.html

function phpfmg_redirect_js(){
    if( defined('PHPFMG_REDIRECT') && '' != PHPFMG_REDIRECT ){
        echo "<script type='text/javascript'>
            function phpfmg_redirect(){
                var redirect = '" . addslashes(PHPFMG_REDIRECT) . "';
                try{
                    if( parent ) parent.location.href = redirect;
                }catch(e){
                    location.href = redirect;
                };
            }

            phpfmg_redirect();
        </script>";
    };
  • 写回答

1条回答 默认 最新

  • dqsvnsad79721 2015-04-08 02:04
    关注

    Update 6 error:

    The 2nd and 3rd brackets must be removed.

       this.submitOK = function(){
      window.top.location.href = redirect; 
    }
        }
    
    
    }
    

    Update 6

    Delete

        if( redirect == '' ){
            showThankYouMessage();
            return;
        };
    
    try{
        if( parent ) {alert('parent');parent.location.href = redirect;}
    
    }catch(e){  alert('frame');
        location.href = redirect;
    };
    

    And replace with this one line: window.top.location.href = redirect;.

    this.submitOK = function(){
      window.top.location.href = redirect; 
    }
    

    Update 5

    Remove previous alert and add two others a little further down, parent and frame.

    this.submitOK = function(){
        if( redirect == '' ){
            showThankYouMessage();
            return;
        };
    
    try{
        if( parent ) {alert('parent');parent.location.href = redirect;}
    
    }catch(e){  alert('frame');
        location.href = redirect;
    };
    

    }

    Update 4

    At form.lib.php line 2484 (may be different due to previous edits)

    Add the line alert('OK');

    Then when the form is submitted, a message box 'OK' should pop up.

       this.submitOK = function(){
       alert('OK');
    
            if( redirect == '' ){
                showThankYouMessage();
                return;
            };
    
            try{
                if( parent ) parent.location.href = redirect;
            }catch(e){
                location.href = redirect;
            };
        }
    

    end of update 4


    Update 3

    In form.lib.php on Line 11

    define( 'PHPFMG_REDIRECT', '' );
    

    Must be:

    define( 'PHPFMG_REDIRECT', 'http://propertytaxdfw.com/thank-you-sign-up.html' );
    

    end of update 3

    Update 2

    If you can find where this code is added.

    function PHPFMG( formID ){
        var frmID = formID;
        var exts = {
            'upload_control' : '',
            'harmful_exts'  : '.php, .html, .css, .js, .exe, .com, .bat, .vb, .vbs, scr, .inf, .reg, .lnk, .pif, .ade, .adp, .app, .bas, .chm, .cmd, .cpl, .crt, .csh, .fxp, .hlp, .hta, .ins, .isp, .jse, .ksh, .Lnk, .mda, .mdb, .mde, .mdt, .mdw, .mdz, .msc, .msi, .msp, .mst, .ops, .pcd, .prf, .prg, .pst, .scf, .scr, .sct, .shb, .shs, .url, .vbe, .wsc, .wsf, .wsh',
            'harmful_errmsg': "File is potential harmful. Upload is not allowed.",
            'allow_exts'  : '.jpg, .gif, .png, .bmp',
            'allow_errmsg': "Upload is not allowed. Please check your file type."
        };
        var redirect = "http://propertytaxdfw.com/thank-you-sign-up.html";
    

    Move var redirect = "http://propertytaxdfw.com/thank-you-sign-up.html";

    to above the function

    var redirect = "http://propertytaxdfw.com/thank-you-sign-up.html";
    function PHPFMG( formID ){
        var frmID = formID;
        ...
    

    Or replace redirect with the actual URL in theis function:
    Delete if( redirect == '' ){

    this.submitOK = function(){
            if( redirect == '' ){
                showThankYouMessage();
                return;
            };
    
            try{
                if( parent ) parent.location.href = redirect;
            }catch(e){
                location.href = redirect;
            };
        }
    

    Change to:

    this.submitOK = function(){
            try{
                if( parent ) parent.location.href = 'http://propertytaxdfw.com/thank-you-sign-up.html';
            }catch(e){
                location.href = redirect;
            };
        }
    

    end of update 2


    I am not a big fan of the redirect.

    Most times when a redirect is used a simple include() or readfile().

    The problem with a redirect is it requires the Browser to do and additional (and unnecessary) HTTP request.

    Why do a redirect when it is not necessary?

    In your case it will probably work where a redirect will not.

    Either of these should work fine.

    if( strlen(trim($redirect))){
      readfile($redirect);
      exit;
    }
    if( strlen(trim($redirect))){
      include($redirect);
      exit;
    }
    if( strlen(trim($redirect))){
      echo file_get_contents($redirect);
      exit;
    }
    

    UPDATE 1

    The required redirect JS is not in the iFrame: https://propertytaxdfw.com/SignUpFormFiles/form.php.

    Either function phpfmg_redirect_js() is not being executed in the PHP or defined('PHPFMG_REDIRECT') is not defined, or PHPFMG_REDIRECT is an empty string('' != PHPFMG_REDIRECT).

    Two things to try:
    Add this to the PHP:

    define("PHPFMG_REDIRECT", "http://propertytaxdfw.com/thank-you-sign-up.html");
    

    Or add this to the PHP:

        echo "<script type='text/javascript'>
        function phpfmg_redirect(){
          var redirect = '" . addslashes(PHPFMG_REDIRECT) . "';
          try{
          if( parent ) parent.location.href = redirect;
          }catch(e){
            location.href = redirect;
          };
        }
        phpfmg_redirect();
    </script>";
    

    When the page is created by the PHP if the above JS is found withing the existing
    <script type='text/javascript'> </script>
    then change the code to:

        echo "
    function phpfmg_redirect(){
          var redirect = '" . addslashes(PHPFMG_REDIRECT) . "';
          try{
          if( parent ) parent.location.href = redirect;
          }catch(e){
            location.href = redirect;
          };
        }
        phpfmg_redirect();
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择