Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:16:45+00:00 2026-05-31T12:16:45+00:00

Hope someone can help me. I have used the following tutorial as a guide

  • 0

Hope someone can help me.

I have used the following tutorial as a guide to running ajax in wordpress http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

I have the following code in my functions.php file

wp_enqueue_script( 'my-ajax-request','/wp-content/themes/son-of-suffusion/js/ajax.js', array( 'jquery' ) ); 
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); 


/*AJAX STUFF*/

// this hook is fired if the current viewer is not logged in
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
 // if logged in:
do_action( 'wp_ajax_' . $_POST['action'] );


// if both logged in and not logged in users can send this AJAX request,
// add both of these actions, otherwise add only the appropriate one
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' );
add_action( 'wp_ajax_myajax-submit', 'myajax_submit' ); 

function myajax_submit() {   

    echo 'ajax submitted';    

    die;

    // IMPORTANT: don't forget to "exit"  
    exit;

}

and this code in my ajax.js file

// JavaScript Document

jQuery(function($){
   $(".selected").click(function () {

      alert("jQuery is working");
      $.post(MyAjax.ajaxurl, {
    action: 'myajax-submit',
      postID : MyAjax.postID

  }, function(response) {

     $("#content").html("loading...");
     $("#content").html(response);      


    });

     });


})

The jQuery alert is displaying, but the ajax code is not working and the full page refreshes. Am I doing something wrong? There are no errors in my js code, when I use Firebug.

Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:16:46+00:00Added an answer on May 31, 2026 at 12:16 pm

    In your click function, pass an event like below.And add

    ev.preventDefault

    $(".selected").click(function (ev) {
    
      //prevents default action of the element
      ev.preventDefault();
      alert("jQuery is working");
      $.post(MyAjax.ajaxurl, {
      action: 'myajax-submit',
      postID : MyAjax.postID
    
      }, function(response) {
    
     $("#content").html("loading...");
     $("#content").html(response);      
    
    
    });
    
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope someone can help me with this.. I have a magento store up&running, via
I hope someone can help me with the following... I have this code below
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I hope someone can help me out. I have a table that logs our
Really hope someone can help me as I'm a bit stuck :S I have
I hope someone can help... This issue has been discussed here and I have
I hope someone can help. The following piece of php code (see below) is
I hope someone can help with this issue. I have a popup iframe using
I hope someone can help me. I have a website that uses javascript to
I hope someone can help me as I am a real newbie. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.