我正在尝试使用Javascript和Ajax将图像添加到数据库中 而且我的代码可以很好地工作,而无需在数据库中插入图像,但是当我尝试插入图像时,代码根本无法工作,所以在这里 是我的代码
- register.php
<form id="registrationForm">
<input type="text" class="input" id="firstname" name="firstname" placeholder="First name" required>
<input type="text" class="input" id="lastname" name="lastname" placeholder="Last name" required>
<input type="email" class="input" name="email" id="mail" placeholder="E-mail" required>
<input type="file" class="input" id="profileimg" name="profileimg" placeholder="Profile image">
<input type="text" class="input" minlength="11" id="tel" name="tel" placeholder="Mobile " required>
<input type="submit" class="btnSubmit btn-Blue" value="Register">
</form>
<script>
var submit_button = $(".btnSubmit");
$(submit_button).click(function(e){
e.preventDefault();
var fname = $( "#firstname" ).val();
var lname = $( "#lastname" ).val();
var mail = $( "#mail" ).val();
var tel = $( "#tel" ).val();
var aFormData = new FormData();
aFormData.append("profileimg", $('#profileimg')[0].files[0]);
jQuery.ajax({
url : '<?php echo admin_url( 'admin-ajax.php'); ?>',
type : 'post',
processData: false,
contentType: false,
dataType: "json",
data : {
action : 'trip_register_ajax',
'f_name': fname ,
'l_name': lname ,
'e-mail': mail ,
'mob': tel ,
'profile' : aFormData
},
success : function( response ) {
alert(response)
}
});
});
</script>
这是我的functions.php
add_action( 'wp_ajax_nopriv_trip_register_ajax', 'trip_register_ajax' );
add_action( 'wp_ajax_trip_register_ajax', 'trip_register_ajax' );
function trip_register_ajax() {
$firstname=$_POST['f_name'];
$lastname=$_POST['l_name'];
$mail=$_POST['e-mail'];
$tel=$_POST['mob'];
$upload_image=$_FILES['profileimg'];
global $wpdb;
$wpdb->insert('wp_users' , array(
'first_name' => $firstname,
'last_name' => $lastname,
'user_email' => $mail,
'tel' => $tel,
'image' => $upload_image,
));
}