Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

After some answers I have edited my code. This is my plugin structure

stpl1
   stpl1.php
   getuser.php

In stpl1.php I have the following code:

<?php
/*
Plugin Name: Wordpress Plugin Stijn 1
Description: Een Wordpress plugin die data uit een database haalt en invult via Ajax 

in een form.
Version: 1.1
Author: Stijn Aerts
Author URI: http://stijnaerts.be
License: GPL2
*/

add_action( 'wp_head', 'ajax_lookup_userdata' );

    function ajax_lookup_userdata(){
    if (!is_admin()) {
            wp_enqueue_script('jquery');
        }
    $url = plugins_url('getuser.php', __FILE__);
    ?>

    <script type="text/javascript">
    jQuery(document).ready(function($){
        alert('test');
    jQuery('#input_1_2').change(function(){
            showUser(this.value);
        });

        function showUser(str){

            if(str==''){
                jQuery('form').html('');
            }

            jQuery.get(<?php echo $url; ?>, { q: str }, function(response){
                jQuery('#input_1_3').val('test');
            });
        }
    });
    </script>
<?php
}

?>

And in getuser.php I have the following code:

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost',****,****,****);
if (!$con) {
  die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM Personeel WHERE personeelsNummer = '".$q."'";
$result = mysqli_query($con,$sql);

echo "test";

mysqli_close($con);
?> 

I activated the plugin and I go to this page: test website

I enter a number in the first text input, in the second text input should come 'test'. But nothing happens... It seems like this code is causing the failing:

jQuery.get(<?php echo $url; ?>, { q: str }, function(response){
                    jQuery('#input_1_3').val('test');
                });

What am I doing wrong?

Thanks for the help I have already gotten, it was very helpfull and learnfull.

share|improve this question
2  
Start by reading AJAX in Plugins to learn how to properly use AJAX in WordPress. –  Milo Jul 30 at 17:44

2 Answers 2

up vote 0 down vote accepted

Well, that just won't work for so many reasons if this is actually your whole plugin.

I'm guessing you are getting a fatal error here. This is because you are using HTML (and javascript for that matter) in the PHP document.

Also, if the getuser.php is not located at the root of your site, JavaScript won't find it.

I would also use the jQuery method, as you are already using it.

Try this:

 <?php
/*
Plugin Name: Wordpress Plugin Stijn 1
Description: Een Wordpress plugin die data uit een database haalt en invult via Ajax in een form.
Version: 1.0
Author: Stijn Aerts
Author URI: Stijn Aerts
License: GPL2
*/

add_action( 'wp_footer', 'ajax_lookup_userdata' );

function ajax_lookup_userdata() {

    $url = plugins_url( 'getuser.php', __FILE__ ); // get the url to the getuser.php-script

    ?>
    <script type="text/javascript">
    jQuery( document ).ready(function($) { // jQuery runs in noConflict mode, use this while developing for WordPress

        $('#input_1_2').change(function() {
            showUser(this.value)
        });

        function showUser(str) {

            if(str == '') {
                $('form').html('');
            }

            $.get(<?php echo $url; ?>, { q: str }, function(response) {
                $('form').html(response);
            });

        }
    });
    </script>
<?php
}
?> 

Also I would look in to using WordPress' native ajax functions for plugins.

share|improve this answer
    
After some extra work it is working now, thanks! –  stijn26 Aug 10 at 17:23

First of all the ajax code in WordPress is different from the normal ajax. Second, you don't add a Javascript into a function, however, it's a better practice to create a JS file and enqueue it. Here is how to do it:

JS file:

jQuery(document).ready(function($){ 
                $('#input_1_2').change(function(){
                    jQuery.ajax({
                        type:"POST",  //request type
                        url: ajaxurl, //fixed ajax file url
                        //action is the php function to call, after that you can send any data
                        //to this function like postID.
                        data: {'action':'getusers','q': 'value'}, //function name to implement and data to send
                        //where to put the result.
                        success:function(data){
                            $("<form>").html(data);
                            console.log(data);
                        },
                        error:function(errorThrown){
                            console.log(errorThrown);
                        }

                    });
                });
            });

php file:

function my_scripts(){
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

        wp_register_script( 'Jsfile', get_template_directory_uri().'/js/my_ajax_file.js', array( 'jquery' ) );
        wp_enqueue_script( 'Jsfile' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

function getusers(){
    $q = $_REQUEST['q'];
    //do your processing staff
    return #data;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.