简体   繁体   English

如何使用 AJAX 将 HTML 变量上传到 PHP 文件

[英]How to upload a HTML variable to a PHP file using AJAX

On my website I am trying to basically generate a random code (which I will set up later) and then pass that code into a PHP file to later retrieve it when the client needs it.在我的网站上,我试图基本上生成一个随机代码(我稍后会设置),然后将该代码传递到一个 PHP 文件中,以便稍后在客户端需要时检索它。 But my code just isn't working.但我的代码不起作用。

Here is the code:这是代码:

Javascript/HTML: Javascript/HTML:

function init() {    
    var code = "12345";
        $.ajax({
            type: 'POST',
            url: 'codes.php',
            data: { code: code},
            success: function(response) {
                $('#result').html(response);
            }
        });
    }

PHP: PHP:

 <?php 
     $code = $_POST['code'];
     echo $code
 ?>

So what I understand that is supposed to happen is that the code is uploaded or 'posted' to the php file and then the #result is the echo $code.所以我理解应该发生的是代码被上传或“发布”到php文件,然后#result是echo $code。 None of that happens and I have no idea.这些都没有发生,我不知道。

Your code working perfect with some basic changes.您的代码在进行一些基本更改后可以完美运行。 You need a html element with id 'result'.您需要一个 ID 为“result”的 html 元素。 And then you need to call your init() as per requirement.然后你需要根据要求调用你的 init() 。

<div id="result"></div>
<script>
function init() {    
    var code = "12345";
        $.ajax({
            type: 'POST',
            url: 'codes.php',
            data: { code: code},
            success: function(response) {
                $('#result').html(response);
            }
        });
    }
init();
</script>

You 100% have something of the likes of <div id="result"></div> ? 你100%有像<div id="result"></div>吗?
You 100% have jQuery? 你100%有jQuery?
You 100% have the path to the php file correct? 你100%有正确的PHP文件的路径?
You 100% are calling init() on window load? 你100%在窗口加载时调用init()?

<?php 
$code = $_POST['code'];
echo $code;
?>

I tried this on my server in the head of my document, and it worked :) I used on complete instead of on success.我在我的文档头部的服务器上尝试了这个,并且它起作用了:) 我使用的是 complete 而不是成功。

<script type="text/javascript" src="https://code.jquery.com/jquery.min.js"></script>
<script>
function init() {    
    $.ajax({
        type: "POST",
        url: "codes.php",
        data: {  
                'code': '12345'
            },

        complete: function(data){
            document.getElementById("result").innerHTML = data.responseText
        },
    });
}
init();
</script>

with codes.php the same as you have :)与codes.php一样,你有:)

just a few notes:只是一些注意事项:

  1. Make sure you point your url to the correct file.确保您将 url 指向正确的文件。 You can check it by using the console network.您可以使用控制台网络进行检查。 Or you can simply print anything out, not just the $_POST data.或者您可以简单地打印任何内容,而不仅仅是 $_POST 数据。 eg:例如:

     echo 'Test info';
  2. Open browser developer panel, to see if is there any client code issue.打开浏览器开发者面板,查看是否有客户端代码问题。 For example, document with id 'result' existed, or you have not included jquery in. The developer console will tell you everything on the client side.例如,id 为'result'的文档存在,或者你没有包含jquery。开发者控制台会告诉你客户端的一切。 For Chrome, check it out here https://developer.chrome.com/devtools对于 Chrome,请在此处查看https://developer.chrome.com/devtools

  3. Have you actually called init() ?你真的叫过 init() 吗?

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

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