简体   繁体   English

如何在第二页上将值从Javascript传递给php变量?

[英]How to pass value from Javascript to php variable on the second page?

I have two pages. 我有两页。 One that accepts input and stores it to a javascript variable. 接受输入并将其存储到javascript变量的接收器。 The other will receive the sent variable and store it to php variable so that i can forward it to the database. 另一个将接收发送的变量并将其存储到php变量,以便我可以将其转发到数据库。

here's the code of my first page: 这是我的第一页的代码:

<html>
<head>
    <script type="text/javascript">
        var pricee= 0;
        var quants;
        function queue(num,str){
            this.pricee=num;
            this.quants=str;
            alert(num+" "+str);
        }
        function alertCurrentNumandStr(){
            alert("This is the latest: "+this.pricee+" "+this.quants);
        }
        function send(num){
             this.pricee=num;
        }
    </script>
</head>
<body>
    <ul>
        <li><script type="text/javascript">var x=42, y="alive";</script><a href="#" onclick="queue(x, y);">42 Alive</a></li>
        <li><a href="#" onclick="alertCurrentNumandStr();">Get</a></li>

        <li><script type="text/javascript">var t=10;</script><a href="#" onclick="send(t);">Submit</a></li>
        <!--How to send this.pricee, this.quants to .php when clicking the Submit link(above)?-->
    </ul>
</body>
</html>

And code on my secondpage: 和我的第二页上的代码:

<html>
<head>
<?php
    $testNum= $_GET['pricee']
?>
</head>
<body>
<?php
       echo "<h1>The num value </h1>";
       echo $testNum;
?>
</body>
</html>

How can I pass the javascript variables to the second page when I click Submit? 单击“提交”后,如何将javascript变量传递到第二页?

You can also do this with simple GET Variable. 您也可以使用简单的GET变量执行此操作。

  1. <a href='yourphpfilename.php?price1=xxx&YOUR2ndvariable=xxx'>Click this</a>
  2. And by forwarding to the next php page, simple fetch this variables using 通过转发到下一个php页面,使用简单的获取此变量

    $_GET['price1']; $ _GET [ '价格1']; ... ...

change your send function to this 将您的发送功能更改为此

  function send(quants){
window.location.href = "yourPhpPageUrl.com?price"+pricee+'qunatity' +quants;
            }

there will be no change in your PHP file . 你的PHP file没有变化。 just give it a try 试一试吧

put your li tags in form tag like so: 把你的li标签放在form标签中,如下所示:

<form action="action_page.php" method="GET">
    <ul>
        <li><script type="text/javascript">var price1=12, itn1="Mar";</script><a href="#" onclick="queue(price1, itn1);">Marinated $12</a></li>
        <li><script type="text/javascript">var price2=1, itn2="Kalamay";</script><a href="#" onclick="queue(price2, itn2);">Kalamay $1</a></li>
        <li><script type="text/javascript">var price3=5, itn3="coach Yeng";</script><a href="#" onclick="queue(price3, itn3);">Coach Yeng $5</a></li>
        <li><a href="#" onclick="getPrice();">Get</a></li>

        <li><script type="text/javascript">var quantity=10;</script><a href="#" onclick="send(quantity);">Submit</a></li>
    </ul>
<input type="text" name="pricee" value="100">
<br>
Pricee:<br>
<input type="submit" value="Submit">
</form>

i have assumed here that your php page is action_page.php 我假设你的php页面是action_page.php

here method = "GET" tells the php page tha it has to receive the values as get requests. 这里method = "GET"告诉php页面它必须接收值为get request。

In addition to this, input type="submit" before the closing form tag tells that you are using a submit event to sent the values in the li tags. 除此之外,在结束form标记之前input type="submit"表示您正在使用submit事件来发送li标记中的值。

You have used a pricee tag in your php code ( $_GET("pricee") ). 您在PHP代码中使用了pricee标签( $_GET("pricee") )。 To match this request you must have an element with name="pricee" . 要匹配此请求,您必须拥有name="pricee"的元素。

I have added that tag above. 我在上面添加了这个标签。

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

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