dongxing7318 2015-12-02 17:36
浏览 89
已采纳

如何通过OnClick返回带有新变量的switch语句?

I'm building an intake application for clubs. I'm having trouble getting the user from one page to another. Each page is a module, usually a form. And they can differ in position, as in the user can change the position a page has in the intake.

Here's how my database looks like approximately. A club can have 1 or more intakes. An intake consists of multiple modules. The database models look like this:

table: intake               
intake_id(P)   club_id
       12345     99999
       12346     99999
       12347     99998

table: intake_has_module
intake_id(P)   module_id   position
       12345           1          2
       12345           2          1
       12345           3          3
       12346           1          1
       12346           2          2
etc.

table: module
module_id(P)                   name
           1   Personal Information
           2   Terms and Conditions
           3          Subscriptions

(Dummy data)

Here I get the intake_id from the corresponding club

$intake_sql = $db->sql_query('SELECT intake_id FROM fit_vg_intake WHERE club_id = ' . (int)User::currentUser()->getClubID());
$intake_sql_2 = $db->sql_fetchrow($intake_sql);

Here I get the module_id that's in first position from the club's intake

$module_sql = $db->sql_query('SELECT module_id FROM fit_vg_intake_has_module WHERE position = 1 AND intake_id = ' . $intake_sql_2['intake_id']);
$module_sql_2 = $db->sql_fetchrow($module_sql);

Then I use this module_id in a switch that looks like this:

switch ($module_sql_2['module_id']) {
case 1:
    generate_page('intake/personalinformation.twig');
    break;
case 2:
    generate_page('intake/termsandconditions.twig');
    break;
case 3:
    generate_page('intake/subscriptions.twig');
    break;
default:
    generate_page('intake/main.twig');
    break;
}

So this part is working correctly for me. The user can switch the position of each module in a settings panel. Now for my problem & question:

Say a user is on the Terms and Conditions module. Which is module_id 2 and position 1. When he clicks a button which confirms that he has accepted the terms and conditions he should be moved to the module that has the position+1 of module_id=2.

What I've tried: I've tried adding an onClick to the button. When clicked on a javascript function gets called named next_module() with an action called accepted-terms. I pass this action to the php file in which I use another switch:

switch ($action) {
case 'accepted-terms':
    $position_sql = $db->sql_query('SELECT position FROM intake_has_module WHERE module_id = 2 AND intake_id = ' . $intake_sql_2['intake_id']);
    $position_sql_2 = $db->sql_fetchrow($position_sql);
    break;
default:
    break;
}

In this switch as you can see I get the position of the module with module_id=2. It returns 1 which is correct. Now I can get the module_id with a position of 1+1. This is how far I've come. Basically my question is: How do I return to the first switch with the new module_id when someone clicks a button?

  • 写回答

1条回答 默认 最新

  • dtncv04228 2015-12-03 10:53
    关注

    I fixed this by using the handler array and adding the position to the back of the url like /intake/1 , /intake/2 etc

    $handler_page = System::getHandlerArray()[0];
    if($handler_page == null){
    $handler_page = '1';
    }
    

    Here I check if the user is at /intake. If he is and thus System::getHandlerArray()[0] == 0, I assign 1, for the first position, to the variable.

    Then to use this variable in my template I do this:

    $template->assign_var('position', (int)$handler_page + 1); 
    

    I add 1 because I want to assign the current position + 1 to this variable because I will be using it to direct the page with an onClick to the next position as is seen here:

    <script>
    function next_module() {
        window.location.href = ('/intake/' + {{ position }});
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站