2015-02-10 94 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