将动态变量从javascript传递给php

时间:2016-01-02 20:49:43

标签: javascript php

我有这个隐藏的div

<div class="amount" style="display:none;">$30.00</div>

我用Javascript获得这个div值:

var price;
window.onload = function(){  
   //Get the value from the amount div     
   price = document.getElementsByClassName('amount')[0].innerHTML; 

   //Set the hidden_price input value (on the form below) to the price
   document.getElementById("hidden_price").value = price; 
};   

现在我有一个隐藏输入的表单

<form action="phpfile.php" method="post">    
    <input type="hidden" name="hidden_price" id="hidden_price">    
    <input type="submit" />            
</form>

当我点击提交时,我在php中有变量

 $hidden_price = $_POST['hidden_price'];

我可以不提交表格吗?

1 个答案:

答案 0 :(得分:0)

提交表单的等效表单可以是:

$.post('phpfile.php', $("form").serialize(), .done(function( data ) {
   alert( "Data Loaded: " + data );
});