简体   繁体   English

jQuery $ .post()和PHP问题

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

It's not updating and I'm missing what I'm doing wrong. 它没有更新,我想念我做错了什么。 Uses HTML, jQuery, and PHP. 使用HTML,jQuery和PHP。 All code is posted below. 所有代码都在下面发布。

What I'm trying to do is allow a user to change a 'client' seed and when it' changed it's updated. 我正在尝试做的是允许用户更改“客户端”种子,并在更改时对其进行更新。 In the it's displayed. 在中显示。 All that does is refresh every 100ms from a file that's echoing it out. 所有要做的就是每隔100ms从回显它的文件中刷新一次。 There's no issue there. 那里没有问题。

PHP Code: 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: 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: 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>

You are confusing server side code and client side code. 您正在混淆服务器端代码和客户端代码。 PHP executes on the server, meaning that any links to resources should be file paths to where the actual files on the server are located. PHP在服务器上执行,这意味着到资源的任何链接都应该是服务器上实际文件所在的文件路径。 Javascript/JQuery is client side code, meaning that it runs in the user's browser, so any links should be urls not file paths. Javascript / JQuery是客户端代码,这意味着它在用户的浏览器中运行,因此任何链接都应该是URL,而不是文件路径。

Instead of using a local file path on the server like you are now with: 不再像现在那样在服务器上使用本地文件路径:

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

Your url passed to $.post() should be a URL which accesses that PHP script. 传递给$.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