M
M
Maxim Maximov2018-10-17 11:46:06
WordPress
Maxim Maximov, 2018-10-17 11:46:06

Wordpress not accepting $_POST?

I made a form, but php does not accept POST
php code

add_action('wp_ajax_join_mailinglist', 'join_mailinglist_callback');
add_action('wp_ajax_nopriv_join_mailinglist', 'join_mailinglist_callback');

function join_mailinglist_callback() {
    // Куда отправлять
    $to = '[email protected]';
    
    $postlink = trim(strip_tags($_POST['postlink'])); // Так не принимает переменную ///////////

    $postlink = $_POST['postlink']; // и Так не принимает переменную ////////////////////////////
     


    $email = trim(strip_tags($_REQUEST['email'])); // А так принимает


    $subject = 'Сообщение с сайта' . $posttitle;
    
    $headers = "From: " . $email . "\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=utf-8\r\n";
    
    $message = "<html><div>";
    $message .= 'Сообщение с сайта <br /> От: '. $posttitle;
    $message .= 'Сообщение со страницы <br /> От: '. $postlink;
    $message .= '<br /> Email отправителя: '. $email;
    $message .= "</div></html>\r\n";
    $mail = mail($to, $subject, $message, $headers);

    if($mail){
        echo 'Вы успешно подписались' . $postlink . $email; //  Переменные для проверки
    }else {
        echo 'Ошибка попробуйте ещё раз';
    }

wp_die();
}

Send script
jQuery(document).ready(function(){
  jQuery("#mailinglistsubmit").click(function()  {

    var postlink = jQuery('#postlink').val();
    var email = jQuery('#mailinglistemail').val();


    jQuery.ajax({
          url: ajaxurl,
          data: {
              action: 'join_mailinglist', 
              type: 'POST',
              email: email,  
              postlink: postlink,
          },
             success:function(data) {
              console.log(data);
              jQuery("#mailinglist #message").text( data );
          },
          error: function(errorThrown){
            console.log(errorThrown); 
          }
          }); 

        return false;
      
    });
  });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2018-10-17
@webbuddu

Apparently, because the request still has the GET type! :)
type: 'POST',take out for data.

jQuery.ajax({
          url: ajaxurl,
          type: 'POST',
          data: {
              action: 'join_mailinglist', 
              email: email,  
              postlink: postlink,
          },
             success:function(data) {
              console.log(data);
              jQuery("#mailinglist #message").text( data );
          },
          error: function(errorThrown){
            console.log(errorThrown); 
          }
          }); 
        return false;
    })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question