WordPress is giving me 404 page not found for functions.php?

<script>
  $(document).ready(function(){
    $("#agentsubmit").click(function(){
      var ajaxurl = '<?php echo site_url();?>/wp-admin/admin-ajax.php';
    	$.ajax({
     		    type: "POST",
    			dataType : "json",
    			url : ajaxurl,
    			data : { 
    			        action :'join_mailinglist_callback',
    				    'email': email
    			        },
    		    success:function(data){
    			   // your success call
    			   $(".alert-message").html(data);
    		    }
    	});				
    });
 });
    </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<form class="agnet-contact-form" name="contact_form" method="post" action=" " id="agnet-contact-form1">
  <div class="col-xs-6 col-sm-6 col-md-6">
      <input type="text" name="full_name" placeholder="Name" required>
      <input type="text" name="phone_number" placeholder="Phone" required>
      <input type="text" name="email_address" placeholder="Email" id="contactemail" required>
  </div>						
  <div class="col-xs-6 col-sm-6 col-md-6">
      <textarea name="message" placeholder="Message" required></textarea>enter code here
      <input type="submit" class="agent_submit" name="submit" id="agentsubmit" value="submit now">
  </div>						
</form>
Here is code which I wrote in functions.php which is action of ajax.

/* send mail using ajax*/
add_action('wp_ajax_join_mailinglist', 'join_mailinglist_callback');
add_action('wp_ajax_nopriv_join_mailinglist', 'join_mailinglist_callback');

    function join_mailinglist_callback() {
    $email = $_POST['email'];
    if(!empty($email)) {
        $yourEmail = 'fc@abc.com';
        $subject = 'contacting Us';
        $success = mail($yourEmail, $subject, $email);
        if(!empty($success)) {
            echo 'Email sent successfullly.';
        } else {
            echo 'Email Does not send sorry please try again.';
        }
    }
    die();
}   

This code gave me error NetworkError: 404 Not Found – http://localhost/wordpress/functions.php
I have used jquery and also include min.js file in my code.And pass the url of admin ajax but alert is not working in a response I have tried out.
What is the reason for that and how solve it.
If email sent successfully give response and it will write on Div that Email sent successfully Otherwise it says that Email don’t send from the functions.php file

Related posts

Leave a Reply

1 comment

  1. Refer to wp_ajax.

    The code in for ajax function is wrong try this :

    <script>
    $(document).ready(function(){
    $("#agentsubmit").click(function(){
      var ajaxurl = '<?php echo site_url();?>/wp-admin/admin-ajax.php';
        $.ajax({
                type: "POST",
                dataType : "json",
                url : ajaxurl,
                data : { 
                        action :'join_mailinglist',
                        'email': email
                        },
                success:function(data){
                   // your success call
                   $(".alert-message").html(data);
                }
        });             
    });
    });
    </script>