因此,我有以下内容来更新用户元:
<?php if ( is_user_logged_in() ) { ?>
<?php echo '<input class="input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_user -> rh_phone. '"/>' ;?>
<?php update_user_meta( $current_user->ID, 'rh_phone', esc_attr( $_POST['rhc_phone'] ) ); ?>
<?php }else{ ?>
<input class="input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value=""/>
<?php } ?>但是,当我单击submit时,没有值(甚至是以前的值)。
我做错了什么?
谢谢!
编辑:这是我的表单,通过ajax:发送
<?php
$current_user = wp_get_current_user();
$rhcs_close = $_REQUEST['rhc_post_id'];//same as post id
?>
<div class="rh_contact_form">
<div class="my_textfield rhc_phone_number_class">
<?php if ( is_user_logged_in() ) { ?>
<?php echo '<input class="my_textfield__input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value="' .$current_user -> billing_phone. '"/>' ;?>
<?php }else{ ?>
<input class="my_textfield__input" type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number" value=""/>
<?php } ?>
</div>
<div class="my_textfield rhc_ask_class">
<?php if ( is_user_logged_in() ) { ?>
<?php echo '<input class="my_textfield__input" type="text" id="rhc_ask" name="rhc_ask" placeholder="Ask me" value="' .$current_user -> my_question. '"/>' ;?>
<?php }else{ ?>
<input class="my_textfield__input" type="text" id="rhc_ask" name="rhc_ask" placeholder="Ask me" value=""/>
<?php } ?>
</div>
<div class="rh_contact_button">
<button class="rh_contact_me">
<i class="icons">done</i>
</button>
</div>
</div>
<script type="text/javascript" >
function my_form_validate(element) {
$('html, body').animate({scrollTop: $(element).offset().top-100}, 150);
element.effect("highlight", { color: "#F2DEDE" }, 1500);
element.parent().effect('shake');
}
jQuery(document).ready(function($) {
$('body').on('click', '.rh_contact_me', function() {
if($('#rhc_phone').val() === '') {
my_form_validate($('#rhc_phone'));
} else if($('#rhc_ask').val() === '') {
my_form_validate($('#rhc_ask'));
} else {
var data = {
'action': 'my_send_message',
'phone': $('#rhc_phone').val(),
'to_author': $('#rhc_ask').val()
};
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
$.post(ajaxurl, data, function(response) {
if(response === 'success'){
alert('Message Sent Successfully!');
$('.rhp_close').click();
}
});
}
});
});
</script>Functions.php
class MY_Posts {
public static function my_send_message() {
if (isset($_POST['message'])) {
$to = $_POST['to_author'];
$headers = 'From: My Page <' . $_POST['email'] . '>';
$subject = "Hi";
ob_start();
?>
<div>
Hi
</div>
<?php
$message = ob_get_contents();
ob_end_clean();
$mail = wp_mail($to, $subject, $message, $headers);
if($mail){
echo 'success';
}
}
exit();
}
public static function my_mail_content_type() {
return "text/html";
}
}因此,当用户在下面的表单中输入“电话号码”和“询问”字段时,它会向admin (页面作者)发送一个表单。但同时,我试图使它更新这两个字段(即用户元键)。
我不知道怎样才是最好的方法。
我猜有两种情况:(以“电话号码”字段为例)
当然,如果用户没有登录,那么就没有可以使用的值。
发布于 2015-10-05 20:44:47
不能向类发送AJAX请求。您需要做的是拥有一个处理程序文件,它将处理AJAX请求,并在收到数据时运行该函数。
handler.php
if (isset($_POST['someKeyBeingSentByAJAX'])) {
// Run your function here.
}不能直接向类发送AJAX请求。
真例
<?php
class Car {
public static function getCarType() {
return "Super Car";
}
}
?>然后在你的处理程序文件中
<?php
require_once 'Car.php';
if(isset($_POST['getCarType'])) {
$result = Car::getCarType();
echo $result;
}
?>您可以将AJAX请求发布到处理程序,可以为每个请求创建特定的处理程序,也可以有一个通用的AJAX处理程序,但是该文件可能会变得相当大,很难维护。
https://stackoverflow.com/questions/32957337
复制相似问题