impulse
impulse

Reputation: 206

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. The other will receive the sent variable and store it to php variable so that i can forward it to the database.

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?

Upvotes: 1

Views: 934

Answers (3)

SagarPPanchal
SagarPPanchal

Reputation: 10121

You can also do this with simple GET Variable.

  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

    $_GET['price1']; ...

Upvotes: 2

Kuldeep Dangi
Kuldeep Dangi

Reputation: 4432

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. just give it a try

Upvotes: 1

put your li tags in form tag like so:

<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

here method = "GET" tells the php page tha it has to receive the values as get requests.

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.

You have used a pricee tag in your php code ($_GET("pricee")). To match this request you must have an element with name="pricee".

I have added that tag above.

Upvotes: 0

Related Questions