0

I have a piece of code that is not working. It is a handler for POST submission. Below is the code sending data.

$.ajax({
  type: 'POST',
  url: '/calculator.php',
  data: $('form').serialize(),
  success: function(data) {
    console.log(data);
  }
});

The handler - calculator.php looks like:

<?php
$parse_url = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
require_once($parse_url[0], 'wp-load');
send_email();
echo 'success';

function sendEmail() {
  ..
  wp_mail($to, $subject, $message, $headers);
}
?>

When calculator.phps is being invoked from the command line, it works fine. However, submitting the from with ajax or postman does nothing. I have a varnish cache in front of my server, but it should not matter. Should it?

Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43
  • url: '/calculator.php' was this path correct? – Shijin TR Oct 24 '17 at 06:19
  • yes, the path is fine. – Moshe Shmukler Oct 24 '17 at 06:19
  • 1
    can you post the xhr body and header? – madalinivascu Oct 24 '17 at 06:22
  • But you are using explode('wp-content' to get the script name.so it is confusing – Shijin TR Oct 24 '17 at 06:24
  • Headers: ```Accept:*/* Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8,ru;q=0.6 Cache-Control:no-cache Connection:keep-alive Content-Length:184 Content-Type:application/x-www-form-urlencoded; charset=UTF-8 Cookie:__atuvc=0%7C34%2C0%7C35%2C4%7C36%2C105%7C37%2C44%7C38; _ga=GA1.2.1051388021.1502790997 Host:xxxxx.com Origin:http://xxxxx.com Pragma:no-cache Referer:http://xxxxx.com/calculator/ User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 X-Requested-With:XMLHttpRequest``` returns OK. – Moshe Shmukler Oct 24 '17 at 06:38
  • 1
    you have set action for ajax in wordpress unless it will not work. Please check the manual https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) – Prasanna Venkatesh Oct 24 '17 at 06:45

1 Answers1

0

Put the calculator.php file in the root of the wordpress installation with the following changes. and use this path as your ajax url

calculator.php

<?php
require_once('wp-load.php');
send_email();
echo 'success';

function sendEmail() {
  ..
  wp_mail($to, $subject, $message, $headers);
}
?>
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • tried before, and just in case now. does not help. In fact, even `die` does not have any affect. – Moshe Shmukler Oct 24 '17 at 06:36
  • Getting 200 status for the xhtr request? – Shijin TR Oct 24 '17 at 06:54
  • I am not a WordPress person. Do see that for plugins there is a way to register actions with ajax admin. Don't want to learn how to wrap a plugin around that code, if that is an option. Did not write that thing, but my job is to get it working. – Moshe Shmukler Oct 24 '17 at 07:00
  • 1
    you need to add action for a working ajax call. You refer the manual and you don't need a plugin to do so.This link might help you https://stackoverflow.com/questions/43557755/how-to-call-ajax-in-wordpress – Prasanna Venkatesh Oct 24 '17 at 07:18