简体   繁体   English

从Forms中的Functions.php执行函数

[英]Execute A Function from Functions.php in Form

I'm building a form which displays a certain piece of user_meta data and I want to be able to update this usermeta on form submit. 我正在构建一个显示某些user_meta数据的表单,我希望能够在表单提交时更新此usermeta。

This is what I have at the moment; 这就是我现在所拥有的;

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']) );
} 

But the thing is the php that updates the user meta does so when the page loads, I only want it to do it when the user pressess save. 但事情是在页面加载时更新用户元的php,我只希望它在用户预先存储时执行。

You need to use Ajax to update information on a page without refreshing it. 您需要使用Ajax更新页面上的信息而不刷新它。 Try jQuery! 试试jQuery! It is a realy simple way to use Ajax. 这是使用Ajax的非常简单的方法。 For example: 例如:

<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>

And in functions.php you may set: 在functions.php中你可以设置:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM