2016-01-02 146 views
0

我有這個隱藏的div從Javascript傳遞動態變量到PHP

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

我得到這個div值的JavaScript:

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

其實,發送任何數據到服務器端,你必須執行'form'提交或使'ajax'請求。在頁面加載時,需要Ajax req – RomanPerekhrest

+0

。 – devpro

+0

你能幫我嗎? – user4571629

回答

0

的等價形式提交不提交表單可能是:

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

它不工作,你能指導我嗎? – user4571629