dongtuo0828 2017-08-18 05:17
浏览 37

会话仅在更改最后一个会话值之前存储数据一秒钟

When I click the Sort By Date Created button, the string date is passed to the function sort() and alert 1 (see code) prints the string date. The string date is stored in the session in the first if statement and the alert 2 prints date. The issue is date is only being stored temporarily and alert 3 always alerts service, no matter the type. If I change the order of the if statements, the last if statement's string somehow always is stored in the session.

<?php session_start(); ?>
<html>
<head>
  <script>

   function sort(type){

     alert('<?php echo $_SESSION['sort']; ?>');  ///ALERT 1

     if (type == 'date'){
       <?php $_SESSION['sort'] = 'date'; ?>
       alert('<?php echo $_SESSION['sort']; ?>'); ///ALERT 2
     }
     else if (type == 'cost'){
       <?php $_SESSION['sort'] = 'cost'; ?>
       alert('<?php echo $_SESSION['sort']; ?>');
     }
     else if (type == 'service'){
       <?php $_SESSION['sort'] = 'service'; ?>
       alert('<?php echo $_SESSION['sort']; ?>');
     }

     alert('<?php echo $_SESSION['sort']; ?>'); ///ALERT 3
  }

 </script>
</head>
<body>
  <input type="button" onclick="sort('date');" value="Sort By Date Created">
  <input type="button" onclick="sort('cost');" value="Sort By Cost">
  <input type="button" onclick="sort('service');" value="Sort By Service">
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dpfps86064 2017-08-18 05:27
    关注

    You are confused about the difference between client side and server side code. You have essentially written the following code:

    alert('service');
    <?php
        $_SESSION['sort'] = 'date';
        $_SESSION['sort'] = 'cost';
        $_SESSION['sort'] = 'service';
    ?>
    if (type == 'date'){
    
        alert('service'); ///ALERT 2
    }
    else if (type == 'cost'){
    
        alert('service');
    }
    else if (type == 'service'){
    
        alert('service');
    }
    

    PHP does not respect your javascript if statements. PHP will just compile into a static web page which is the latter part of the code I have written.

    I feel an answer trying to explain the difference between the client-side and server-side code is a little broad and long-winded for StackOverflow, but there are plenty resources out there on that topic.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度