[英]Execute A Function from Functions.php in Form
我正在构建一个显示某些user_meta数据的表单,我希望能够在表单提交时更新此usermeta。
这就是我现在所拥有的;
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']) );
}
但事情是在页面加载时更新用户元的php,我只希望它在用户预先存储时执行。
您需要使用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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.