获取数据库中的数据,并用它来完成的一种形式 - 字preSS [英] Getting data from database and use it to complete a form - Wordpress

查看:145
本文介绍了获取数据库中的数据,并用它来完成的一种形式 - 字preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

主要插件文件:

 < PHP

    / *
    插件名称:FixFormData
    说明:如果你想自动完成与现有数据的形式,这个插件是给你的。
    版本:1.1
    作者:斯泰恩Aerts
    作者URI:http://stijnaerts.be
    许可:GPL2
    * /

    要求(plugin_dir_path(__FILE__)menu.php');
    require_once(plugin_dir_path(__FILE__)getuser.php');


    add_action('wp_enqueue_scripts','ffd_load_scripts');

    功能ffd_load_scripts()
    {
        wp_register_script('ffd_js_script,WP_PLUGIN_URL/ FixFormData / JS / ffd_js_script.js',阵列('jQuery的'));
        wp_localize_script('ffd_js_script','myAjax',阵列(
            ajaxurl'=> ADMIN_URL(管理-ajax.php)
            )
        );

        wp_enqueue_script('jQuery的');
        wp_enqueue_script('ffd_js_script',plugin_dir_url(__ FILE__)JS / ffd_js_script.js');
    }
 

getuser.php:

 < PHP
 功能的getUser($海峡)
{
    全球WPDB $;

    $ myoption = get_option('fixformdata_options');
    $ myoptionValue = maybe_unserialize($ myoption);

    $结果2 = $ wpdb-> get_row
    (
        $ wpdb-> prepare
        (
            SELECT * FROM {$ myoptionValue [表名]}其中personeelsNummer =%D,$海峡
        )
    );

    如果($结果2)
    {
        回声json_en code($结果2);
    }
}
?>
 

ffd_js_script.js:

  jQuery的(文件)。就绪(函数($){
    jQuery的('#input_1_2')。改变(函数()
    {
        jQuery.ajax({
            类型:'后',
            数据类型:JSON,
            网址:myAjax.ajaxurl,
            数据:{行动:的getUser,THIS.VALUE},
            更迭:函数(响应){
                变种解析= JSON.parse(响应);
                VAR ARR = [];
                为(变种中的x解析){arr.push(解析[X]);}
                jQuery的('#input_1_3')VAL(改编[1])。
                jQuery的('#input_1_4')VAL(改编[2])。
            }
        })
    });
});
 

我如何正确地实现这一点?我第一次做了一个插件,我读过很多关于它,看到的例子很多,但我没能正确地实现这一点。

编辑:

如果我取代SQL语句如下:

 SELECT * FROM {$ myoptionValue [表名]}其中personeelsNummer =%S,1
 

我得到我在控制台结果由于以下code:

 回声json_en code($结果2);
 

那么下面code不正确地执行:

 更迭:功能(响应){
                变种解析= JSON.parse(响应);
                VAR ARR = [];
                为(变种中的x解析){arr.push(解析[X]);}
                jQuery的('#input_1_3')VAL(改编[1])。
                jQuery的('#input_1_4')VAL(改编[2])。
            }
 

解决方案

好了,你知道么? 第一次当您本地化的脚本添加一个随机数为更好的安全性:

  wp_register_script(ffd_js_script,WP_PLUGIN_URL/ FixFormData / JS / ffd_js_script.js',阵列('jQuery的'));
wp_localize_script('ffd_js_script','myAjax',阵列(
    ajaxurl'=> ADMIN_URL(管理-ajax.php),
    随机数=> wp_create_nonce('AJAX-随机数)
    )
);
 

然后,你需要把getuser.php的地方在那里的插件,可以看到它,所以一定要包括它或将其放置在插件的主要文件

 函数的getUser($海峡)
{
    全球WPDB $;

    //验证随机数
    如果(!wp_verify_nonce($ _REQUEST ['_现时'],Ajax的随机数))
        死亡(非autorizzato!');

    $ myoption = get_option('fixformdata_options');
    $ myoptionValue = maybe_unserialize($ myoption);

    $结果2 = $ wpdb-> get_row
    (
    $ wpdb-> prepare
    (
        SELECT * FROM {$ myoptionValue [表名]}其中personeelsNummer =%D,$海峡
    )
    );

    如果($结果2)
    {
    回声json_en code($结果2);
    }

    死();
}
 

比,使来自阿贾克斯的funciont的getUser可调用的,你需要做适当的挂钩:

  // AJAX挂钩
add_action('wp_ajax_nopriv_getuser','的getUser');
add_action('wp_ajax_getuser','的getUser');
 

在JS,你需要通过随机数和:

  $。职位(myAjax.ajaxurl,
     {
        动作:的getUser,
        _nonce:myAjax.nonce,
        价值:$(这).value的
     },
     功能(响应){...});
 

在JS的休息似乎不错,原谅我的一些拼写错误,我写了,就在这里,所以它没有经过测试。

I have the following files:

Main plugin file:

<?php

    /*
    Plugin Name: FixFormData
    Description: If you want to autocomplete a form with existing data, this plugin is for you.
    Version: 1.1
    Author: Stijn Aerts
    Author URI: http://stijnaerts.be
    License: GPL2
    */

    require( plugin_dir_path( __FILE__ ) . 'menu.php');
    require_once( plugin_dir_path( __FILE__ ) . 'getuser.php');


    add_action( 'wp_enqueue_scripts', 'ffd_load_scripts' );

    function ffd_load_scripts()
    {
        wp_register_script('ffd_js_script', WP_PLUGIN_URL.'/FixFormData/js/ffd_js_script.js', array('jquery'));
        wp_localize_script('ffd_js_script', 'myAjax', array(
            'ajaxurl' => admin_url('admin-ajax.php')
            )
        );

        wp_enqueue_script('jquery');
        wp_enqueue_script('ffd_js_script', plugin_dir_url(__FILE__) . 'js/ffd_js_script.js');
    }

getuser.php:

<?php
 function getuser($str)
{
    global $wpdb;

    $myoption =  get_option( 'fixformdata_options' );
    $myoptionValue = maybe_unserialize( $myoption );    

    $result2 = $wpdb->get_row
    (
        $wpdb->prepare
        (
            "SELECT * FROM {$myoptionValue[tablename]} WHERE personeelsNummer = %d", $str
        )
    );

    if($result2) 
    {
        echo  json_encode( $result2 );
    }
}
?>

ffd_js_script.js:

jQuery(document).ready(function($){
    jQuery('#input_1_2').change(function()
    {
        jQuery.ajax({
            type : 'post',
            dataType : 'json',
            url : myAjax.ajaxurl,
            data : {action: 'getuser', this.value},
            succes: function(response){
                var parsed = JSON.parse(response);
                var arr = [];
                for(var x in parsed){ arr.push(parsed[x]);}
                jQuery('#input_1_3').val(arr[1]);
                jQuery('#input_1_4').val(arr[2]);
            }
        })
    });
});

How do I properly implement this? First time I'm making a plugin and I have read much about it and saw alot of examples, but I'm failing to implement this correctly.

EDIT:

IF I replace the sql statement with the following:

"SELECT * FROM {$myoptionValue[tablename]} WHERE personeelsNummer = %s", 1

I get my results in the console due to following code:

echo  json_encode( $result2 );

So following code is not executing properly:

succes: function(response){
                var parsed = JSON.parse(response);
                var arr = [];
                for(var x in parsed){ arr.push(parsed[x]);}
                jQuery('#input_1_3').val(arr[1]);
                jQuery('#input_1_4').val(arr[2]);
            }

解决方案

ok, you're getting there. 1st When you localize the script add a nonce for better security:

wp_register_script('ffd_js_script', WP_PLUGIN_URL.'/FixFormData/js/ffd_js_script.js', array('jquery'));
wp_localize_script('ffd_js_script', 'myAjax', array(
    'ajaxurl' => admin_url('admin-ajax.php'),
    'nonce'   => wp_create_nonce( 'ajax-nonce' )
    )
);

Then, you need to place getuser.php somewhere where the plugin can see it, so be sure to include it or place it in the main file of the plugin

function getuser($str)
{
    global $wpdb;

    //verify the nonce
    if ( ! wp_verify_nonce( $_REQUEST['_nonce'], 'ajax-nonce' ) )
        die ( 'Non autorizzato!');

    $myoption =  get_option( 'fixformdata_options' );
    $myoptionValue = maybe_unserialize( $myoption );    

    $result2 = $wpdb->get_row
    (
    $wpdb->prepare
    (
        "SELECT * FROM {$myoptionValue[tablename]} WHERE personeelsNummer = %d", $str
    )
    );

    if($result2) 
    {
    echo  json_encode( $result2 );
    }

    die();
}

Than, to make the funciont getuser callable from ajax you need to do the proper hooking:

//ajax hooks
add_action( 'wp_ajax_nopriv_getuser', 'getuser' );
add_action( 'wp_ajax_getuser', 'getuser' );

In the js, you need to pass the nonce as well:

$.post(myAjax.ajaxurl,
     {
        action : 'getuser',
        _nonce : myAjax.nonce, 
        value : $(this).value
     },
     function( response ) { ... });

the rest of the js seems good, forgive me for some typos, I wrote it right here so it's not tested.

这篇关于获取数据库中的数据,并用它来完成的一种形式 - 字preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆