javascript - 如何在wordpress中使用ajax在数据库中插入图像
问题描述
我正在尝试使用 Javascript 和 ajax 将图像添加到我的数据库中,并且我的代码运行良好,无需在数据库中插入图像,但是当我尝试插入图像时,代码根本不起作用所以这是我的代码
- 注册.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,
));
}
解决方案
推荐阅读
- generics - 集合 make_from_separate 中功能调用中的不兼容实际参数
- email - 设置 | G Suite + Sendgrid 用于交易电子邮件
- javascript - 对象的 Javascript 数组对项目键的值求和,然后连续求和值
- c - fputs 或 fprintf 未写入字符串文件
- python - 如何使用 2 列索引取消堆叠数据框?
- c# - 使用 C# 中的自定义属性将字符串转换为大写
- javascript - Angular 先渲染组件,然后在 ng-If 中移除另一个组件
- java - 如何在 Java 中准确地将 Epoch 时间戳转换为 java.time.Instant 类型的对象?
- locust - 确定理想的工作人员数量和主服务器的 EC2 大小
- firewall - Power Query - 将文件路径动态传递给 File.Contents 函数