Skip to content

Instantly share code, notes, and snippets.

@junrillg
Last active November 25, 2015 01:05
Show Gist options
  • Select an option

  • Save junrillg/89dae7da6d629168c77d to your computer and use it in GitHub Desktop.

Select an option

Save junrillg/89dae7da6d629168c77d to your computer and use it in GitHub Desktop.

Revisions

  1. junrillg revised this gist Nov 25, 2015. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion ajax_request
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    ==============================================
    PHP FILE
    ==============================================

    add_action( 'wp_ajax_sample_action', 'my_action_callback' );
    add_action( 'wp_ajax_nopriv_sample_action', 'my_action_callback' );

    @@ -25,7 +29,9 @@ function this_script(){
    }


    ----------------
    ==============================================
    Javascript File
    ==============================================
    $( '.submit' ).on( 'click', function(e){

    e.prevetDefault();
  2. junrillg created this gist Nov 25, 2015.
    52 changes: 52 additions & 0 deletions ajax_request
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    add_action( 'wp_ajax_sample_action', 'my_action_callback' );
    add_action( 'wp_ajax_nopriv_sample_action', 'my_action_callback' );

    function my_action_callback(){

    $_POST['his_not_email'];

    if ( email_exist( $_POST['his_not_email'] ) ) {
    echo json_encode( array( 'success' => 'true' ) );
    } else {
    echo json_encode( array( 'success' => 'false' ) );
    }

    die();

    }

    add_action( 'wp_enqueue_scripts', 'this_script' );

    function this_script(){
    wp_enqueue_script( 'handle', 'htt://location.js', array(), false, true );
    wp_localize_script( 'handle', 'sample', array(
    'url' => admin_url( 'admin-ajax.php' )
    ))
    }


    ----------------
    $( '.submit' ).on( 'click', function(e){

    e.prevetDefault();

    var email = $( '.input-email' ).val();

    $.ajax({
    url: sample.url
    type: 'POST',
    dataType : 'json'
    data: {
    action : 'sample_action',
    this_not_email : email
    }
    sucess: function(respond){
    console.log( respond.sucess );
    }
    error: function(e){
    console.log(e);
    }
    })

    } );