我是新开发者。我已经为数据插入WP仪表板创建了一个插件。我试图在WordPress中使用Ajax形成提交。当我更改pic更新成功,但当我没有更改pic删除。为什么?有我的代码。
形式:
<form id="ajax_form_update" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data">
<table class='wp-list-table widefat fixed'>
<tr><th></th><td><input type="hidden" name="id" value="<?php echo $id; ?>"/></td></tr>
<tr><th>Name</th><td><input type="text" name="name" value="<?php echo $name; ?>"/></td></tr>
<tr><th>Code</th><td><input type="text" name="code" value="<?php echo $code; ?>"/></td></tr>
<tr><th>Image</th><td ><img id="blah" src="data:image/jpeg;base64, <?php echo $photo;?> " width="100" height="100" /> </td></tr>
<tr><th></th><td><input type="file" onchange="readURL(this);" name="photo" class="ss-field-width" /></td></tr>
</table>
<input type='submit' name="submit" value='Save' class='button'>
</form>
PHP:
<?php
function ajaxupdate() {
global $wpdb;
$id = $_POST["id"];
$code = $_POST["code"];
$name = $_POST["name"];
$imagesss = ($_FILES["photo"]["tmp_name"]);
$bin_string = file_get_contents("$imagesss");
$aencode1 = base64_encode($bin_string);
$table_name = $wpdb->prefix . "school";
//update
$result = $wpdb->update(
$table_name, //table
array('code' => $code, 'name' => $name,'image_name'=>$aencode1 ), //data
array('id' => $id) //where
);
}
add_action( 'wp_ajax_ajaxupdate', 'ajaxupdate' );
add_action('wp_ajax_nopriv_ajaxupdate', 'ajaxupdate');
AJAX
jQuery("#ajax_form_update").submit(function(event){
event.preventDefault(); //prevent default action
var post_url = jQuery(this).attr("action"); //get form action url
var request_method = jQuery(this).attr("method"); //get form GET/POST method
var form_data = new FormData(this); //Creates new FormData object
jQuery.ajax({
url: "/mywp/wp-admin/admin-ajax.php?action=ajaxupdate",
type: request_method,
data : form_data,
contentType: false,
cache: false,
processData:false,
success:function(data){
alert ('Data Updated');
//location.reload();
},
});
});
答案 0 :(得分:1)
这是你的代码:
if(!empty(imagesss)){
//update
$result = $wpdb->update(
$table_name, //table
array('code' => $code, 'name' => $name,'image_name'=>$aencode1 ), //data
array('id' => $id) //where
);
} else {
//update
$result = $wpdb->update(
$table_name, //table
array('code' => $code, 'name' => $name), //data
array('id' => $id) //where
);
}