doujian3132 2019-07-17 09:10
浏览 99
已采纳

wordplay php在javascript工作后回调成功?

i use php code in javascript. However, there is an error.

I don't know what went wrong with my code. Can you take a look?

function wp_auth_javascript() {
  if (is_page('login')) {     
    ?>
        <script type="text/javascript">
          function auth_adult(){
                IMP.certification({ // param
                merchant_uid:'merchant_' + new Date().getTime(),
                min_age: 19
              }, function (rsp) { // callback
                    if (rsp.success) { 
                     alert('success');
                    <?php $_SESSION['adult'] = 'y'; wp_redirect(home_url()); exit; ?>

                    }else{
                     alert('fail');
                    }
              });
          }
        </script>
    <?php
  }
}
add_action('wp_head', 'wp_auth_javascript');

When I use this code, When I go to the login page, It is automatically redirected to home url. I thought I would be redirected when I succeed in the callback, but it doesn't work that way at all. What is the problem?

  • 写回答

3条回答 默认 最新

  • duan4739 2019-07-17 09:41
    关注

    Try to redirect by Javascript, not PHP.

    if (rsp.success) { 
        alert('success');
        window.location.href = '/'; // Or anything you want.
    }
    

    The PHP inside your if statement will run all the time you access the login page despite you authorize (by Javascript) success or fail.

    But that just the example code, you should call to admin-ajax.php or a php files like @May Weather VN's anwser to assign the session to them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)