繁体   English   中英

jQuery $ .post()和PHP问题

[英]jQuery $.post() and PHP issue

它没有更新,我想念我做错了什么。 使用HTML,jQuery和PHP。 所有代码都在下面发布。

我正在尝试做的是允许用户更改“客户端”种子,并在更改时对其进行更新。 在中显示。 所有要做的就是每隔100ms从回显它的文件中刷新一次。 那里没有问题。

PHP代码:

<?php
session_start();
include_once('db.php');

if(isset($_POST['action'])) {
    switch($_POST['action']) {

        case 'get_client':
            echo json_encode(array('result' =>  $_SESSION['client']));
        break;

        case 'modify_client':
            if(isset($_POST['client']) && strlen($_POST['client']) == 6 && is_numeric($_POST['client']))  {
                $_SESSION['client'] = $_POST['client'];
                echo json_encode(array('result' => true));

              $secret = 123;
                    $_SESSION['server'] = hash('sha512', $_SESSION['roll'] . $_SESSION['client'] . $secret );
            }

            else {
                echo json_encode(array('result' => false));
            }
        break;
    }
}
?>

Javascript / jQuery:

<script type="text/javascript">
        $.post('./php/show_client.php', { action: 'get_client' }, function(result) {
            var result = JSON.parse(result);
        })
    });
    $("#client_seed_modify").on("click", function() {
        $.post('./php/show_client.php', { action: 'modify_client', client: $("#client_seed").val() }, function(result) {
            var result = JSON.parse(result);
            if(result ) {

                if(result.result) {
                    alert('Your Client Seed has been changed.  This has also changed the server seed.  Please note that you have the ability to change your client seed freely, but regardless of whether or not you decide to, it does NOT stay the same every roll.');
                }
            }
        });


</script>

HTML:

  <a>Current Client Seed: <span class="clientShow" style=""> </span></a>
  <p class="field">
    <input type="text" placeholder="Change your Client Seed" name="client" id="cient_seed" class="client_seed">
  </p>
  <p class="field">
    <input type="submit" value="Change Client Seed" style="width: 360px;" name="client_seed_modify" id="client_seed_modify">
  </p>

您正在混淆服务器端代码和客户端代码。 PHP在服务器上执行,这意味着到资源的任何链接都应该是服务器上实际文件所在的文件路径。 Javascript / JQuery是客户端代码,这意味着它在用户的浏览器中运行,因此任何链接都应该是URL,而不是文件路径。

不再像现在那样在服务器上使用本地文件路径:

$.post('./php/show_client.php' ...

传递给$.post()的URL应该是访问该PHP脚本的URL。

$.post('mysite.com/directory/show_client.php' ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM