【问题标题】:How to submit wordpress form (file & name) with ajax [duplicate]如何使用ajax提交wordpress表单(文件和名称)[重复]
【发布时间】:2016-12-25 06:08:24
【问题描述】:

我是一名新开发人员。我为数据插入 WP 仪表板创建了一个插件。我正在尝试在我的数据库中插入图像。我正在尝试在 WordPress 中使用 Ajax 提交表单。有我的代码。当我使用 input type=text 数据提交成功,但是当我使用 input type=file 时数据没有提交。我希望将所有数据插入到我的数据库中,并将文件保存在上传文件夹中。

 <form  id="ajax_form" method="" action="<?php echo $_SERVER['REQUEST_URI']; ?> " enctype="multipart/form-data">
            <table class='wp-list-table widefat fixed'>
                <tr>
                    <th class="ss-th-width">Code</th>
                    <td><input type="text" name="code" value="<?php echo $code; ?>" class="ss-field-width" /></td>
                </tr>
                <tr>
                    <th class="ss-th-width">School</th>
                    <td><input type="text" name="name" value="<?php echo $name; ?>" class="ss-field-width" /></td>
                </tr>
                <tr>
                    <th class="ss-th-width">Image</th>
                    <td><?php wp_nonce_field('ajax_file_nonce', 'security'); ?>
                        <input type="hidden" name="action" value="my_file_upload">
                        <label for="file_upload">It's a file upload...</label>
                        <input type="file" name="file_upload">
                    </td>
                </tr>
            </table>
            <input type='submit' name="insert" value='Save' class='button'>

        </form>

Ajax 代码在这里:

jQuery('#ajax_form').submit(ajaxformdata);   

    function ajaxformdata()
    {    
        var alldata = jQuery(this).serialize();    

        jQuery.ajax({  
            type:"POST",  
            //url: "/wp-admin/admin-ajax.php?action=signup", 
            url: "/mywp/wp-admin/admin-ajax.php?action=cruddata",                   
            data: alldata,  
            success:function(data)
            {
                alert('Data submited');
            },

            error: function(errorThrown)
            {
                alert(errorThrown);
            }  
        });    

        return false;  
    }  

PHP 代码在这里

<?php
function cruddata()
    {
        global $wpdb;
        $code = $_POST["code"];
        $name = $_POST["name"];
        $imagesss = basename($_FILES["photo"]["name"]);
        $table_name = $wpdb->prefix . "school";

        $wpdb->insert(
             $table_name, //table
            array
                (
                    'code' => $code, 
                    'name' => $name
                    //'image_name' => $imagesss
                );
        $message.="Data inserted";
        }

    add_action( 'wp_ajax_cruddata', 'cruddata' );

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

    试试这个 - 在你的 JS 上,你的 url 应该是 ajaxurl,action 应该是 cruddata 希望这能正常工作。

    注册一个执行你的功能的方法

    add_action('wp_ajax_cruddata', 'cruddata');
    add_action('wp_ajax_nopriv_cruddata', 'cruddata');
    

    你的函数定义 -

    function cruddata() {
       // code goes here...
    }
    

    Javascript:

    jQuery.ajax({
        data: {
            'action': 'cruddata',
        },
        dataType: 'json',
        type: 'post',
        url: ajaxurl,
        beforeSend: function () {
            //
        },
        success: function (data) {
            //
        }    
    });
    

    【讨论】:

    • 我认为您的解释中有一个小错字: add_action('wp_ajax_nopriv_cruddata', 'cruddatat'); - 应该是“cruddata”,对吗?
    • 哦!对不起!!感谢您发现这个错误。更新了:)
    • ajaxurl 是从哪里来的?
    猜你喜欢
    • 2014-10-31
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多