简体   繁体   English

通过 PHP Post 将 Javascript 变量传递到另一个页面

[英]Pass Javascript variable to another page via PHP Post

I am having two php pages:我有两个 php 页面:

page 1:第 1 页:

<form class="form-horizontal"  role="form" method="post" action="Page2.php">
      <button id="place-order" class="btn btn-lg btn-success">Place Order</button>
      <div id="ajax-loader" style="display:none;"><img src="images/ajax-loader.gif" /></div>
</form>
<script>
      var id = Math.random();
      $(document).ready(function() {
        $('#place-order').on('click', function() {
            $(this).hide();
            $('#ajax-loader').show();
        });
      });
</script>

As on form, it redirects to Page2.php, I want to pass the Javascript variable "id" from Page1 to receive it in Page2.在表单上,​​它重定向到 Page2.php,我想从 Page1 传递 Javascript 变量“id”以在 Page2 中接收它。 I have tried using cookies, but need an alternative approach.我曾尝试使用 cookie,但需要另一种方法。

I am not understanding the transistion from PHP to JS and vice-versa.我不理解从 PHP 到 JS 的转换,反之亦然。 Help is appreciated.帮助表示赞赏。 Thanks in advance提前致谢

You can use session storage or cookies.您可以使用会话存储或 cookie。 Example for session storage:会话存储示例:

// First web page:
sessionStorage.setItem("myVariable", "myValue");
 
// Second web page:
var favoriteMovie = sessionStorage.getItem('myVariable');

Dear you can do it very easily with ajax.亲爱的,你可以很容易地用 ajax 做到这一点。 Ajax has data attribute which helps you pass your data from javascript to another page. Ajax 具有 data 属性,可帮助您将数据从 javascript 传递到另一个页面。

This link will help you a lot https://api.jquery.com/jquery.ajax/此链接将帮助您很多https://api.jquery.com/jquery.ajax/

You could use a query string to pass the value to the next page.您可以使用查询字符串将该值传递到下一页。

  1. Add an ID to the form向表单添加 ID
<form class="form-horizontal"  role="form" method="post" action="Page2.php" id="order-form">
  1. Update the action of the form to add this query string from our JS variable更新表单的操作以从我们的 JS 变量中添加此查询字符串
var id = Math.random();
$('#order-form').attr('action', 'Page2.php?id=' + id);
  1. Get this variable in PHP (obviously you might wanna do more checks on it)在 PHP 中获取这个变量(显然你可能想对它做更多的检查)
<? $id = $_GET['id'] ?>

We can now use $id anywhere in our PHP and we'll be using the ID generated from JS.我们现在可以在 PHP 的任何地方使用$id并且我们将使用从 JS 生成的 ID。 Neat, right?整洁,对吧? What if we want it in JS again though?如果我们再次想要它在 JS 中怎么办? Simply add another script tag and echo it there!只需添加另一个脚本标签并在那里回显!

<script type="text/javascript">
var id = <? echo $id ?>;
</script>

EDIT : Updated to add a little about how it works as you said you're not too sure about the transition between PHP and JS.编辑:更新添加了一些关于它是如何工作的,因为你说你不太确定 PHP 和 JS 之间的转换。

PHP runs on the server. PHP在服务器上运行。 It doesn't know much about the browser, and certainly doesn't know about JS.它对浏览器了解不多,当然也不了解JS。 It runs everything and finishes executing before the web page is displayed.它运行所有内容并显示网页之前完成执行。 We can pass PHP variables to JS by creating script tags and creating a new javascript variable, echo ing the PHP value.我们可以通过创建脚本标签和创建一个新的 javascript 变量, echo显 PHP 值来将 PHP 变量传递给 JS。

JS (JavaScript) runs in the browser. JS (JavaScript) 在浏览器中运行。 It doesn't know about anything that happens on the server;它不知道服务器上发生的任何事情; all it knows about is the HTML file it is running in (hit CTRL+U to see raw HTML).它只知道它正在运行的 HTML 文件(按 CTRL+U 查看原始 HTML)。 As JS runs at a completely separate time to PHP there is no easy way to transfer variables (eg $phpVar = myJSVar ).由于 JS 与 PHP 在完全不同的时间运行,因此没有简单的方法来传输变量(例如$phpVar = myJSVar )。 So, we have to use server methods like POST or GET .因此,我们必须使用诸如POSTGET类的服务器方法。

We can create a GET or POST request in 2 main ways:我们可以通过两种主要方式创建GETPOST请求:

  1. Using a form使用表格
  2. Using an AJAX request使用 AJAX 请求

Forms work in the way I've outlined, or you can create a hidden field, set the value you want and then check for that.表单按照我概述的方式工作,或者您可以创建一个隐藏字段,设置您想要的值,然后检查它。 This involves redirecting to another page.这涉及重定向到另一个页面。

AJAX (Asynchronous Javascript And Xml) works slightly differently in that the user doesn't have to leave the page for the request to take place. AJAX(异步 Javascript 和 Xml)的工作方式略有不同,因为用户不必离开页面即可进行请求。 I'll leave it to you to research how to actually program it (jQuery has a nice easy API for it!), but it basically works as a background request - an example would be displaying a loading spinner whilst loading order details from another page.我会让你研究如何实际编程(jQuery 有一个很好的简单 API!),但它基本上作为后台请求工作 - 一个例子是显示加载微调器,同时从另一个页面加载订单详细信息.

Hope this helps, let me know if something's not clear!希望这会有所帮助,如果有不清楚的地方,请告诉我!

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

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