简体   繁体   English

我如何接收从 JAVASCRIPT 到 PHP 的数据变量

[英]how can i recieve data variable from JAVASCRIPT to PHP

im still a beginner in this field i wanted to send the information from the value that I created from javascript to the other file external php or i mean from client-side to server-side as an example i have loop and value stored in 'i' and i wanna send it to php how?我仍然是该领域的初学者我想将信息从我从 javascript 创建的值发送到其他文件外部 php 或者我的意思是从客户端到服务器端作为示例我有循环和值存储在'i ' 我想把它发送到 php 怎么办? i googled and see some ppl say's use ajax method i still don't know it anyway i tried as an experiment like this it actually worked it only prints a value for me in console but idk how to send it to php我用谷歌搜索了一些人说的使用 ajax 方法我仍然不知道它无论如何我尝试作为这样的实验它实际上工作它只在控制台中为我打印一个值但我知道如何将它发送到 php

 var myrequest = new XMLHttpRequest(); myrequest.open('POST','upload.php',true); xhr.setRequestHeader('Content-Type',"application/x-www-form-urlencoded"); myrequest.onreadystatechange = function() { myrequest.onload = function() { console.log(this.responseText); } let data = JSON.stringify({'test': 'test'}); myrequest.send(data); }

in php file i tried as an example to call variable from js $_post['test'] i dunno how but it didn't work在 php 文件中,我尝试作为示例从 js $_post['test']调用变量我不知道如何,但它没有用

First you need to convert variable "data" to text like this首先,您需要将变量“数据”转换为这样的文本

let data = JSON.stringify({test: 'test'});

In PHP you can use PHP input stream.在 PHP 中,您可以使用 PHP 输入流。 PHP input stream is basically way to receive raw data. PHP 输入流基本上是接收原始数据的方式。 You can access it this way您可以通过这种方式访问​​它

$data = file_get_contents("php://input");

Now we have stringified JSON in $data variable.现在我们在 $data 变量中对 JSON 进行了字符串化。 To access data that is in $data variable you need to use PHP's json_decode function.要访问 $data 变量中的数据,您需要使用 PHP 的 json_decode 函数。

$final_data = json_decode($data);
echo $final_data->test;

This might seem complicated thing to understand.这似乎很难理解。 You should read more about data types.您应该阅读有关数据类型的更多信息。 In that way you can understand this example so much better.这样你就可以更好地理解这个例子。 I will link you few things that might help you out in learning.我会给你链接一些可能有助于你学习的东西。

String(0) means that your PHP has not received your data from Javascript. String(0) 表示您的 PHP 没有从 Javascript 接收到您的数据。 Reason for that might be that your URL is not written in right way.原因可能是您的 URL 没有以正确的方式编写。 I recommend using full URL.我建议使用完整的 URL。 For example if you have web server set at localhost your URL should be "http://localhost/upload.php".例如,如果您在 localhost 设置了 Web 服务器,您的 URL 应该是“http://localhost/upload.php”。

I made you example code that you can take look at.我给你做了示例代码,你可以看看。 It has comments that explain you every step that is happening.它的注释可以向您解释正在发生的每一步。 It might be hard for you to read it and understand it completely but that is okey.您可能很难阅读并完全理解它,但这没关系。 I actually did this example for me aswell as I do need to remind myself how to do things.我实际上为我做了这个例子,因为我确实需要提醒自己如何做事。 I post you two links to Pastebin that you can try to use together.我向您发布了两个指向 Pastebin 的链接,您可以尝试将它们一起使用。 I post it in comments as I did not find out how to post content from Pastebin to answer.我将其发布在评论中,因为我没有找到如何从 Pastebin 发布内容来回答。

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

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