简体   繁体   English

Javascript / JQuery变量或数组到PHP变量或数组

[英]Javascript/JQuery variable or array to PHP variable or array

I'm trying to figure out how to "send" a Javascript/JQuery variable to a PHP variable. 我试图找出如何将“Javascript / JQuery”变量“发送”到PHP变量。 I found this post on here, and tried following some of the stuff there, with no luck. 我在这里找到了这篇文章 ,并尝试了一些其中的东西,没有运气。 I tried to make the simplest code that I could for this, just as a way to test it. 我试图为此制作最简单的代码,就像测试它一样。 Here it is. 这里是。

thefilename.php thefilename.php

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<?php
$test = $_POST['test'];
echo $test;
echo "Test Ran";
?>

<input type="button" id="rawr" value="Test"/>

<script>
var somestring = "some string";

//Audio test
$("#rawr").click( function() {
    alert("BUTTON PRESSED");
    $.post("thefilename.php", {test : somestring});
});

</script>

This, in my mind, is a simple way to test it. 在我看来,这是一种测试它的简单方法。 Maybe I have something wrong... not sure... thats why I'm here. 也许我有些不对劲......不确定......这就是为什么我在这里。 Just looking for the simplest way to pass a variable from Javascript to PHP. 只是寻找将变量从Javascript传递给PHP的最简单方法。 Eventually I would like to pass an array as well. 最后我想传递一个数组。 Any help with that would be greatly appreciated also. 任何帮助也将非常感激。

I think you are fundamentally confused about the page lifecycle of PHP versus JavaScript. 我认为你对PHP与JavaScript的页面生命周期有着根本的混淆。 PHP is a server-side scripting language. PHP是一种服务器端脚本语言。 When you request a page from your server (by navigating to it, or initiating an AJAX call), the PHP script runs from start to finish and your server returns its output (which in this case is an HTML page). 当您从服务器请求页面时(通过导航到它或启动AJAX调用),PHP脚本从头到尾运行,服务器返回其输出(在这种情况下是HTML页面)。 That's it. 而已。 PHP is done when the page source is delivered to your browser. 当页面源传递到您的浏览器时,PHP就完成了。

JavaScript, on the other hand, is a client-side scripting language. 另一方面,JavaScript是一种客户端脚本语言。 JavaScript operates inside your browser and can make changes to a page once the page is loaded (most notably by altering the DOM). JavaScript在您的浏览器中运行,并且可以在页面加载后对页面进行更改(最明显的是通过更改DOM)。

AJAX provides a somewhat tenuous link between the two worlds. AJAX在两个世界之间提供了一些有点脆弱的联系。 Javascript functions like the .post() in jQuery send an asynchronous request back to the server and tell the server to retrieve a certain file. JQuery函数(如jQuery中的.post())将异步请求发送回服务器并告诉服务器检索某个文件。 If that file is PHP, the server typically executes it. 如果该文件是PHP,则服务器通常执行它。

So in your example here's what happens: 所以在你的例子中,这是发生了什么:

  1. You type the URL for thefilename.php into your browser and hit enter 您可以在浏览器中键入filenamename.php的URL,然后按Enter键
  2. Browser sends a request to the server for thefilename.php 浏览器向服务器发送文件名为.name的请求
  3. Server recognizes thefilename.php as a PHP file and tells PHP to execute the script 服务器将filename.php识别为PHP文件,并告诉PHP执行该脚本
  4. The PHP script runs. PHP脚本运行。 Its output is determined by the data contained in the request body ($_POST in PHP). 它的输出由请求体中包含的数据确定(PHP中的$ _POST)。 It outputs some HTML code. 它输出一些HTML代码。
  5. The server responds to your request for thefilename.php with the HTML code produced by executing it. 服务器使用执行它生成的HTML代码响应您对filename.php的请求。
  6. Your browser renders the HTML code for you. 您的浏览器会为您呈现HTML代码。
  7. You click the button 单击按钮
  8. JavaScript in your browser sends a request to thefilename.php on your server 浏览器中的JavaScript会向您的服务器上的filenamename.php发送请求
  9. Your server recognizes thefilename.php as a PHP file and tells PHP to execute the script 您的服务器将filenamename.php识别为PHP文件,并告诉PHP执行该脚本
  10. The PHP script runs. PHP脚本运行。 Its output is determined by the posted data. 其输出由发布的数据确定。 It produces some HTML code. 它产生一些HTML代码。
  11. The server responds to your request for thefilename.php with the HTML code produced by executing it. 服务器使用执行它生成的HTML代码响应您对filename.php的请求。
  12. The .post function receives the HTML response and then renders it inside an alert. .post函数接收HTML响应,然后将其呈现在警报内。

I hope that helps explain what's going on. 我希望这有助于解释发生了什么。 Most likely you want a separate PHP file for your asynchronous call. 您很可能需要一个单独的PHP文件来进行异步调用。

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

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