2016-11-06 40 views
0

我在HTML中使用簡寫形式。這是聊天窗口,我想通過那裏不僅用戶消息,但也有一些隱藏的數據文件「post.php」,這是在上層文件夾。使用AJAX提交隱藏數據

<form name="message" action=""> 
    <input name="usermsg" type="text" id="usermsg" size="63" /> 
    <input name="company" type="hidden" id="company" value="<?php echo $kawalki_adres[2]; ?>"/> 
    <input name="submitmsg" type="submit" id="submitmsg" value="Send" /> 
</form> 

這裏是AJAX

<script type="text/javascript"> 
     //If user submits the form 
$("#submitmsg").click(function(){ 
    var clientmsg = $("#usermsg").val(); 
    var company = $("#company").val(); 
    $.post("../post.php", {text: clientmsg, company});    
    $("#usermsg").attr("value", ""); 
    $("#company").attr("value", ""); 
    return false; 
}); 

我走用戶的信息正確 「post.php中」,但不知道如何領取 「公司」 變量。

+0

設定公司的價值你沒有設置你的'$。員額公司數據()',只有關鍵。將該行更改爲:$ .post(「../ post.php」,{text:clientmsg,company:company}); ' – pappfer

+1

您可以簡單地使用'var data = $('form')。serialize()'並將數據發送到$ .post內的服務器 –

回答

0

使用這樣

確保您通過PHP

$("#submitmsg").click(function(e){ 
    e.preventDefault(); //prevents form submit, otherwise pages gets reloaded 
    var clientmsg = $("#usermsg").val(); 
    var company = $("#company").val(); 
    $.post("../post.php", {text: clientmsg, company: company});    
    $("#usermsg").attr("value", ""); 
    $("#company").attr("value", ""); 
    return false; 
});