如何将数据从 JavaScript 发送到 PHP,反之亦然?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1968296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:10:02  来源:igfitidea点击:

How to I send data from JavaScript to PHP and vice versa?

phpjavascripthtmlprotocols

提问by chustar

How do I go about passing data from JavaScript code to PHP and back. Right now, I do this is a round about way:
PHP to JavaScript: I would simply use the inline echo to send the data:

我如何将数据从 JavaScript 代码传递到 PHP 并返回。现在,我这样做是一种方式:
PHP 到 JavaScript:我将简单地使用内联回显来发送数据:

<script type="text/javascript">
    var data = <? echo $target_variable ?>
</script>

OR

或者

<script type="text/javascript">
    var data = <?= $target_variable ?>
</script>

JavaScript to PHP: I would create a form element in the JavaScript that would sumbit the data for me to the php file:

JavaScript 到 PHP:我将在 JavaScript 中创建一个表单元素,它将我的数据汇总到 php 文件中:

<script type="text/javascript">
    var data = targetData;
    document.write("
        <form method=\"post\">
            <input type=\"hidden\" value=\""+target_value+\""></input>
        </form>
    ");
</script>
</script>

Are there any better ways to do this? Best practices sort of thing.

有没有更好的方法来做到这一点?最佳实践之类的。

回答by Sampson

If you wish to submit this data via a form, you don't need to create the form with Javascript. Simply create an invisible form with HTML, populate the hidden field with Javascript, and automatically submit whenever you're ready.

如果您希望通过表单提交此数据,则无需使用 Javascript 创建表单。只需使用 HTML 创建一个不可见的表单,使用 Javascript 填充隐藏字段,并在您准备好时自动提交。

  <form method="post" action="process.php">
    <input type="hidden" name="data" id="data" />
  </form>

  document.getElementById("data").value = "foo";

If you want to send this in an ajax-style fashion, I would suggest implementing jQuery, which makes this extremely easy. Note the previous case converted to a jQuery solution:

如果你想以 ajax 风格的方式发送它,我建议实现 jQuery,这使得这非常容易。请注意前面的案例转换为 jQuery 解决方案:

$.post("process.php", {data:"foo"}, function(results){
  // the output of the response is now handled via a variable call 'results'
  alert(results);
});

回答by Priyank

Following code shows a ajax request which submits a form from your page in background using jquery ajax. I hope it makes sense.

以下代码显示了一个 ajax 请求,它使用 jquery ajax 在后台从您的页面提交表单。我希望这是有道理的。

<html>
<head><title>Sample</title></head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script language="javascript">
    function submitMe()
    {
       variableString = 'name='+$("#name").val()+'&location='+$("#location").val();
       jQuery.ajax({
       type: "POST",
       url: "http://google.com/some.php",
       data: variableString,
       success: function(msg){
         alert( "Data Saved: " + msg );
       }
     });
    }
</script>
<body>
    <form id="xyzForm">
       <input type="text" id="name" name="name"/>
       <input type="text" id="location" name="location"/>
       <input type="button" value="submit" onClick="submitMe();"/>
    </form>
</body>
</html>

回答by marvin

First, you need to understand that php and javascript are running on different computers.

首先,您需要了解 php 和 javascript 在不同的计算机上运行。

Php runs on the server whereas javascript runs on the client computer. That makes things a little bit nondirect. I mean, php and javascript cannot change variables directly, because they dont run at the same time. First php runs at the server, prepares the HTML and js and gives it to the client, then at clients computer javascript runs, if javascript needs something from server, it calls the server again (either post, get or ajax doesnt matter, it is still a http request).

Php 在服务器上运行,而 javascript 在客户端计算机上运行。这让事情变得有点不直接。我的意思是,php 和 javascript 不能直接更改变量,因为它们不会同时运行。首先php在服务器上运行,准备HTML和js并将其提供给客户端,然后在客户端计算机上运行javascript,如果javascript需要来自服务器的某些东西,它会再次调用服务器(无论是post,get还是ajax都无所谓,它是仍然是一个http请求)。

So from php to javascript,you can pass the variable while preparing the javascript, at which pont only php runs and javascript not, and from javascript to php, you can pass the variable by making a request from clients computer to your php server with some variables.

因此,从 php 到 javascript,您可以在准备 javascript 时传递变量,此时仅 php 运行而 javascript 不运行,从 javascript 到 php,您可以通过从客户端计算机向您的 php 服务器发出请求来传递变量变量。

回答by nks14

you can use GET or POST to pass the data from one to another.

您可以使用 GET 或 POST 将数据从一个传递到另一个。

回答by Jeff Beck

The best thing you can do with the pair of PHP and JavaScript is to start using the json_encode() and json_decode() functions in PHP.

使用 PHP 和 JavaScript 可以做的最好的事情是开始使用 PHP 中的 json_encode() 和 json_decode() 函数。

This will get the data into JSON which is the JavaScript Object Notation allowing you to pass objects between the two languages. You can do an eval once you get it in JS to get the object.

这会将数据转换为 JSON,这是 JavaScript 对象表示法,允许您在两种语言之间传递对象。一旦你在 JS 中得到它,你就可以做一个 eval 来获取对象。

Actually moving the data should depend on what you are doing exactly but many times I use Ajax and it would be a good place to start looking at.

实际上移动数据应该取决于你在做什么,但很多时候我使用 Ajax,这将是一个开始查看的好地方。

回答by Mike

Well, if you want asynchronous interaction, you can use XMLHTTPRequest to perform an async POST request to the server, to send data.

好吧,如果你想要异步交互,你可以使用 XMLHTTPRequest 向服务器执行异步 POST 请求,以发送数据。