如何在表格中按单词提交后显示成功消息?此代码用于提交评论。提交评论后,我需要显示成功消息(您的评论已成功提交)。我的html代码:
<div class="container">
<div class="row">
<div class="reviews">
<?php
if (is_user_logged_in()) {
?>
<h2>REVIEWS ON <?php echo get_the_title(); ?> </h2>
<div class="message-box">
<form method="post">
<div class="rating">
<div class="rating-inner">
<div id="rateYo1"></div><p>Rate the store</p>
</div>
</div>
<textarea class="review_message" name="message_review" id="message" placeholder="Type your review here..." required></textarea>
<button class="review_submit_button" name="review_submit" value="Submit Review">Submit Review</button>
<input type="hidden" id="data-rating" name="data-rating" >
</form>
</div>
<?php }?>
</div>
</div>
</div>
<?php
$img = showImage();
$user_avatar=$img[0]->mem_photo;
if (isset($_POST['review_submit'])) {
$review_content = $_POST['message_review'];
$data_rating = $_POST['data-rating'];
$shop_id = get_the_ID();
add_user_review($review_content, $data_rating, $shop_id, $user_avatar);
}
?>
Function code:
function add_user_review($review,$data_rating,$shop_id,$user_avatar){
global $wpdb;
$current_user=wp_get_current_user();
$current_user_id=$current_user->ID;
$username=$current_user->display_name;
$user_email=$current_user->user_email;
$review_date= date("m/d/Y");
$in=$wpdb->insert("wp_user_reviews" ,array(
"review_content"=>$review,
"review_rating"=>$data_rating,
"wp_user_id"=>$current_user_id,
"user_name"=>$username,
"product_id"=>$shop_id,
"review_date"=>$review_date,
"user_email"=>$user_email,
"user_avatar"=>$user_avatar,
));
}
请帮助。谢谢:)
答案 0 :(得分:1)
将您的if条件更改为此
if (isset($_POST['review_submit'])) {
$review_content = $_POST['message_review'];
$data_rating = $_POST['data-rating'];
$shop_id = get_the_ID();
if(add_user_review($review_content, $data_rating, $shop_id, $user_avatar)){
echo "<script>alert('success')</script>";
} else {
echo "<script>alert('fail to insert')</script>";
}
}
或者您也可以在functions.php中编写它
<script>
$( ".review_submit_button" ).click(function() {
alert( "success" );
});
</script>
这始终适用于:)