2015-02-10 62 views
0

我正在構建一個顯示某個user_meta數據的表單,並且希望能夠在表單提交時更新此usermeta。從函數.php中執行函數

這是我目前所擁有的;

function my_form_funnction() { 
global $current_user; 

$vat_no = get_user_meta(get_current_user_id(), 'vat_number', true); 

?> 
<form name="setPrices" action="" method="POST"> 

<label for="lowPrice">Vat Number: </label> 
<input type="text" id="vat_number" name="vat_number" value="<?php echo $vat_no ?>" /> 


<button type="submit">Save</button> 
</form> 

<?php update_user_meta(get_current_user_id(), 'vat_number',  esc_attr($_POST['vat_number'])); 
} 

但問題是,更新用戶元也因此,當頁面加載,我只希望它這樣做時,用戶pressess保存PHP。

回答

0

您需要使用Ajax更新頁面上的信息而不刷新它。試試jQuery!這是一個非常簡單的使用Ajax的方法。例如:

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<button onclick="javascript:la()">Save or do smth</button> 
<div id="a">Here will be included the loadable thing</div> 
<script> 
function la(){ 
$("#a").load("/functions.php?func=my_form_function"); 
} 
</script> 

而且在functions.php中你可以通過設置:

if(isset($_GET["func"]){ 
if($_GET["func"] == "my_form_function"){ 
my_form_funnction(); 
} 

}

+0

恐怕沒有工作 – 2015-02-11 18:13:25

+0

當我再補充一點確切的代碼添加到functions.php它給錯誤?任何想法 – 2015-02-16 14:30:18

+0

你可以粘貼嗎? – 2015-02-16 20:42:12