Thursday 15 January 2015

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

hello

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

I am giving example of how pass value using javascript.

create .php file like demo.php write to code




<html>
<head>
    
    <script type="text/javascript">
        var price = 0;
        var item_name;
        var quantity;
        function vegetable(price,item){
            this.price=price;
            this.item_name=item;
            alert(price+" "+item);
        }
        function getPrice(){
            alert("This is the latest: "+this.price+" "+this.item_name);
        }
        function send(quantity){
             window.location.href = "get_price.php?price="+price+'&qunatity=' +quantity;
        }
    </script>
</head>
<body>
        <h2>MY Vegetables </h2>
    <ul>
        <li><script type="text/javascript">var price1=12, itm1="Amaranth";</script><a href="#" onclick="vegetable(price1, itm1);"><strong>Amaranth</strong> $10</a></li>
        <li><script type="text/javascript">var price2=1, itm2="Bitter Gourd";</script><a href="#" onclick="vegetable(price2, itm2);"><strong>Bitter Gourd</strong> $15</a></li>
        <li><script type="text/javascript">var price3=5, itm3="Broad beans";</script><a href="#" onclick="vegetable(price3, itm3);"><strong>Broad beans</strong> $16</a></li>
        <li><a href="#" onclick="getPrice();">click Get Vegetables </a></li>

        <li><script type="text/javascript">var quantity=10;</script><a href="#" onclick="send(quantity);">Submit</a></li>
      
    </ul>
</body>
</html>

after include another file get price. create get_price.php

<html>
<head>
<?php
    $price = $_GET['price'];
    $qunatity = $_GET['qunatity'];
?>
</head>
<body>
<?php
       echo "<h1>Price Value</h1>";
       echo $price;
       
       echo "<h1>Quanity </h1>";
       echo $qunatity;
       
?>
</body>
</html>

No comments:

Post a Comment

Thank You For Comment