2014-08-28 44 views
0

我有一个主要的插件文件,需要getuser.php(也是插件文件)。 在getuser.php我已在主要插件文件编写的函数的getUser()在jQuery中使用自定义PHP函数 - wordpress

现在我试图调用用下面的代码此功能:

主要插件文件:

<?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_footer', 'ajax_lookup_userdata'); 

function ajax_lookup_userdata(){ 

    if (!is_admin()) { 
      wp_enqueue_script('jquery'); 
     } 
    ?> 
<script type="text/javascript"> 
    jQuery(document).ready(function($){ 
    jQuery('#input_1_2').change(function(){showUserName(this.value);}); 
     function showUserName(str){ 
      if(str==''){jQuery('#input_1_3').val('');} 
     jQuery.get("<?php echo plugins_url() . '/FixFormData/getuser.php'; ?>", { q: str }, 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]); 
      }); 
     } 
    }); 
</script> 
<?php 
} 
?> 

getuser.php:

<?php 
    if (isset($_POST['q']) && !empty($_POST['q'])) { 
     getuser($_POST['q']); 
     exit; 
    } 

function getuser($str) 
{ 
    //$q = intval($_GET['q']); 
    //include "../../../wp-load.php"; 
    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); 
    } 
} 
?> 

这仍然不起作用。

+0

您试图调用PHP函数与JavaScript?你不能这样做 – QuinnFTW 2014-08-28 19:31:41

+0

是的我知道,我也试过<?php echo getuser()?> – 2014-08-28 19:33:15

+0

getuser()是做什么的?它是否返回一个变量? – ndiego 2014-08-28 19:43:05

回答

2

我认为wp_localize_script是你在找什么。 http://codex.wordpress.org/Function_Reference/wp_localize_script

您可以通过将json数据传递给javascript文件来保持javascript和php分离,而wordpress会以干净优雅的方式做到这一点。

但是,如果你要做的需求与Ajax请求来完成,而不是比你要使用wp_ajax_勾一个很好的教程这里的页面加载: http://www.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/

+0

您好我已经提出了一个新问题,因为我完全编辑了我的代码。 http://stackoverflow.com/questions/25566254/getting-data-from-database-and-use-it-to-complete-a-form-wordpress – 2014-08-29 10:26:23

1

这听起来像你的getuser()功能是用PHP编码的,对吗?假设如此,你将无法通过jQuery调用它。 PHP是一种服务器端语言。它一旦运行,就是这样,它不能在没有重新加载页面的情况下再次访问,并且仍然只能通过服务器端语言访问。 jQuery是客户端脚本,在用户计算机上完成的。

基本上,你需要getuser()函数以某种形式的JavaScript,或者输出你想要显示的内容并让jQuery显示页面。

2

您需要将数据发送到您的功能位于

$(function() { 

function showUserName(str) { 
    if (str === '') { 
     $('#input_1_3').val(''); 
    } 

    $.ajax({ 
     type: 'post', 
     url: 'getuser.php', //where your function is located 
     data: { whatever: value }, //post data $_POST['whatever'] = value 
     success: function(data) { 
      var data = JSON.parse(data); //whatever the server echoes will end up in data 
      $('#input_1_3').val(data[0]); 
      $('#input_1_4').val(data[1]); 
     } 
    }); 
} 

$('#input_1_2').change(function() { 
    showUserName($(this).val()); 
}); 
}); 

你也应该确保你的PHP代码是准备到响应请求的URL。

<?php 

    if (isset($_POST['whatever']) && !empty($_POST['whatever'])) { 
     getuser($_POST['whatever']); 
     exit; 
    } 

    function getuser($str) { 
     // logic and stuff 

     echo json_encode(["whatever", "you want to return"]); 
    } 

    ?> 
+0

感谢您的帮助,我编辑了我的代码,我的代码现在在OP中,但它仍然无法工作,但... – 2014-08-28 20:20:38

+0

也请记住,这是为wordpress和我需要正确的挂钩,我有一个很好的解决方案正在工作,但它没有corredt挂钩。 – 2014-08-28 20:25:17

+0

对不起,我忘了给ajax请求添加一个类型。说实话,我对wordpress一无所知,但这应该起作用。你究竟回来了什么?我可以看到php吗? – hellaminx 2014-08-28 20:28:46