我使用js通过此php运行ajax:
function ajax_Person() { ?>
<script type="text/javascript">
jQuery("#createCat").on("click", function(e){
e.preventDefault();
person();
});
function person(){
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_person', catName: jQuery('#newCat').val(), catDesc: jQuery('#descRiption').val() },
success: function(data) {
jQuery(".modal-body").html("Done!");
}
});
}
</script>
<?php }
然后我跑
function data_person(){
$catname = $_POST['catName'];
$catdesc = $_POST["catDesc"];
$cat_ID = get_cat_ID( sanitize_title_for_query($catname) );
// Check if category exists
if($cat_ID == 0) {
$cat_name = $catname;
$cat_desc = $catdesc;
$cat_slug = sanitize_title_with_dashes($cat_name);
$my_cat = array(
'cat_name' => $cat_name,
'category_description' => $cat_desc,
'category_nicename' => $cat_slug,
'category_parent' => 0
);
if( wp_insert_category( $my_cat ) ) {
echo 'Category added successfully';
} else {
echo 'Error while creating new category';
}
} else {
echo 'That category already exists';
}
}
在两种情况下,我都得到“完成!”作为回应。我需要根据以下内容设置回复:
if( wp_insert_category( $my_cat ) ) {
echo 'Category added successfully';
} else {
echo 'Error while creating new category';
}
} else {
echo 'That category already exists';
}
如果我将它们作为标准php运行,则上述php位有效。
答案 0 :(得分:0)
尝试:
在您的PHP上
if( wp_insert_category( $my_cat ) ) {
echo json_encode( 'Category added successfully');
} else {
echo json_encode( 'Error while creating new category');
}
} else {
echo json_encode( 'That category already exists');
}
在js上
$(".modal-body").html("Done!");
使用
$(".modal-body").html(data);