0

It not consistent, but with some users it will fail from time to time, and with users on a Remote Desktop as well.

I think it is done correctly, but not totally sure. PLease advise me what to do, and if there's something wrong.

It's simple plugin that compares input to a database table and returns the corresponding result without refreshing the page.

From the .php

add_action( 'wp_enqueue_scripts', 'inputtitle_Submit_scripts' );  
add_action( 'wp_ajax_ajax-inputtitleSubmit', 'eidlookup_inputtitleSubmit_func' );
add_action( 'wp_ajax_nopriv_ajax-inputtitleSubmit', 'eidlookup_inputtitleSubmit_func' );

add_shortcode("eidlookup", "eid_lookup_form");

function inputtitle_Submit_scripts() {

//    wp_enqueue_script( 'input_submit', eidlookup_plugin_url . '/js/input_submit.js', array( 'jquery' ));  
      wp_enqueue_script( 'input_submit', plugin_dir_url( __FILE__ ) . '/js/input_submit.js', array( 'jquery' ));    
      wp_localize_script( 'input_submit', 'eid_Ajax', array(
        'ajaxurl'       => admin_url( 'admin-ajax.php' ),
        'nextNonce'     => wp_create_nonce( 'eidajax-next-nonce' ))
    );
}

/* Lookup in database */

function eidlookup_inputtitleSubmit_func() {
    // check nonce
    $nonce = $_POST['nextNonce'];   
    if ( ! wp_verify_nonce( $nonce, 'eidajax-next-nonce' ) )
        die ( 'Beklager, det lykkedes ikke. Genindlæs siden (Ctrl-F5 på pc) og prøv igen.');

    $zipcode = $_POST['zip'];
    // generate the response
    global $wpdb;
    $tablename  = "{$wpdb->prefix}levering";

    $sql    = "SELECT Levering FROM {$tablename} WHERE Zip LIKE %s";
    $result = $wpdb->get_col($wpdb->prepare($sql, $zipcode));
    //error handling
    if ($result != Array()) { 

        $response = array();

        foreach($result as $res){
            $response = $res;
        }

        // response output
        echo "Vi leverer i postnummer " . $zipcode . ":<br /><strong>" . $response . "</strong>";

    }else{
        echo "Det indtastede postnummer blev ikke genkendt - kontroller venligst og prøv igen.";
    }
    die();
    // IMPORTANT: don't forget to "exit"
    exit;
}


/* define formular, add shortcode
*   [eid-lookup] - no args  
 */

 function eid_lookup_form(){
    ob_start();
    ?>
    <div class="form-signing">
        <label>Postnummer:</label><br />
        <input type="text" name="eid-lookup-input" id="eid-lookup-input" width="100px" /><br />
        <button class="btn btn-large" id="button">Vis dage</button>
    </div>

    <div id="info"></div> 
    <?php
    return ob_get_clean();
 } //end eid_lookup_output

And from input_submit.js:

jQuery(document).ready(function($){
      $('#button').click(function(){
        $.post(
            eid_Ajax.ajaxurl,
            {
                // wp ajax action
                action : 'ajax-inputtitleSubmit',

                // vars
                zip : $('input[name=eid-lookup-input]').val(),

                // send the nonce along with the request
                nextNonce : eid_Ajax.nextNonce
            },
            function( response ) {
                console.log( response);
                $("#info").html(response);
            }
        );
        return false;
        });             
  });
4

0 回答 0