如何从另一个脚本返回值和调用函数?

时间:2013-01-04 01:31:49

标签: php jquery ajax

下面我试图创建一个功能,如果用户在当前页面上,阵列中的所有其他页面都被锁定(无法访问),则在用户完成当前页面之前无法访问其他页面他们所在的页面。

现在下面是处理6个php脚本的代码:

        <script type="text/javascript">

$(function() {
    var link = $("#createLink");

    link.click(function() {
        $.ajax({
           url: "removesession.php",
           async: false,
           type: "POST",
           success: function() {
                window.location.href = link.attr("href");
           }
         });

         //cancels the links true action of navigation
         return false;
    });
);
</script>

    <?php

    $steps = array('create_session.php', 'QandATable.php', 'individualmarks.php', 'penalty.php', 'penaltymarks', 'complete.php');

    // Track $latestStep in either a session variable
    // $currentStep will be dependent upon the page you're on

    $latestStep = $_SESSION['latestStep'];
    $currentStep = basename(__FILE__); 

    $currentIdx = array_search($currentStep, $steps);
    $latestIdx = array_search($latestStep, $steps);

    if ($currentIdx - $latestIdx > 1 ) {
        ?>
    <div class="boxed">
    <a href="<?= $pages[$currentPages+1] ?>">Continue</a>
    <br/><a href="create_session.php" id="createLink">Create New</a>
    </div>

    <?



    } else {
        // let the user do the step
    }

    ?>

以下是可能发生的事情的一个例子:

  1. 用户在penalty.php页面上,因此数组中的其他页面是 锁定
  2. 如果用户尝试访问其他页面,则会显示上面显示的div框:

    • 如果用户选择Continue链接,那么它应该将用户导航到他们应该在的页面penalty.php页面

    • 如果用户选择了Create New链接,那么它应该导航到create_session.php并执行jquery / ajax方法并导航到后台的removesession.php脚本

  3. 现在上面的代码在一个名为steps.php的php脚本中,这是外部化的,因此可以使用include(steps.php)访问它,这将包含在数组中的6个php脚本中,这样这些脚本可以访问这些页面。

    现在我的问题是我需要在这段代码中再做一件事,它处理上面代码中的if / else语句,包括其他脚本中的代码:

      

    使用返回值,如果用户可以执行该步骤,则返回1,如果可以,则返回0   不。然后在每个页面上,您将调用此函数,具体取决于   返回值,显示页面或显示错误

    我的问题是,我似乎不知道如何编码上述引文中提到的内容。有人可以显示if / else语句应该是什么样的,以及应该在6的一个php脚本中包含哪些代码,以便能够在该脚本中调用该函数?

    更新

    steps.php

    <?php
    
    function allowed_in($steps){
    
    $steps = array('create_session.php', 'QandATable.php', 'individualmarks.php', 'penalty.php', 'penaltymarks', 'complete.php');
    
    // Track $latestStep in either a session variable
    // $currentStep will be dependent upon the page you're on
    
    $latestStep = $_SESSION['latestStep'];
    $currentStep = basename(__FILE__); 
    
    $currentIdx = array_search($currentStep, $steps);
    $latestIdx = array_search($latestStep, $steps);
    
    if ($currentIdx - $latestIdx > 1 ) {
    
       return 1;
    
    } else {
    
        return 0;
    
    }
    
    }
    
    ?>
    

    create_session.php:

    if(allowed_in($steps)){
    
    //all code in the create_session.php
    
    }else{
    ?>
    
    <div class="boxed">
      <a href="<?= $pages[$currentPages+1] ?>">Continue</a>
    <br/>
    <a href="create_session.php" id="createLink">Create New</a>
    </div>
    
    <?php   
    
    }
    
    ?>
    

1 个答案:

答案 0 :(得分:1)

非常伪代码:

function allowed_in($pased_vars){

//your code

if($foo){
    return 1;
}else{
    return 0;
}

}

在包含的页面上

<?php
//include file with code

if(allowed_in($vars)){
//allowed
}else{
//not
}