doupijin0397 2015-07-08 22:53
浏览 217
已采纳

HTML表单post方法和自动刷新PHP页面

In my html page, I have a php iframe that gets its variables from the form using post method. This works fine.

My problem is this: I would like the php iframe to auto-refresh (which it does), but , understandably, I get undefined index errors from php the first time the iframe auto-refreshes.

Is there some way (preferably using only php/html) to allow the iframe to hold on to the variables from the html form after the first load of the php iframe? Isset doesn't seem to be the answer to my problem

  • 写回答

1条回答 默认 最新

  • dongliushui2001 2015-07-08 23:05
    关注

    Here's a very simple way of accomplishing what you're after:

    <?php
    
    // Starts a session
    session_start();
    
    // Gets a variable via $_POST if present and caches it in $_SESSION
    // If it doesn't exist in $_POST or $_SESSION, return null
    function get_variable( $name )
    {
        if( isset( $_POST[$name] ) )
        {
            $_SESSION['cache_'.$name] = $_POST[$name];
        }
        return ( isset( $_SESSION['cache_'.$name] ) ? $_SESSION['cache_'.$name] : null );
    }
    
    // Get these values from the form on the cache
    $value_a = get_variable('field_name_a');
    $value_b = get_variable('field_name_b');
    $value_c = get_variable('field_name_c');
    
    // One the values is missing
    if( is_null( $value_a ) || is_null( $value_b ) || is_null( $value_c ) )
    {
        // Display an error message or whatever else
    }
    // All values are present
    else
    {
        // Use the variables however you were before
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?