首页 > 解决方案 > 如何在wordpress中使用ajax在数据库中插入图像

问题描述

我正在尝试使用 Javascript 和 ajax 将图像添加到我的数据库中,并且我的代码运行良好,无需在数据库中插入图像,但是当我尝试插入图像时,代码根本不起作用所以这是我的代码

  1. 注册.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, 
                                   ));
                               }

标签: javascriptphpajaxwordpress

解决方案


推荐阅读