0

I have a list that is pre-filled depending on the clicks made within the website. The variable value is filled with the data name of the element clicked. Is there a way to email the variable value once a submit button is clicked that works in the background e.g doesn't open an email client.

var operatorPrice = "";
var emergencyStop = "";

$(".estop").click(function() {
  emergencyStop = $(this).attr("data-name");
  document.getElementById("station").innerHTML = emergencyStop;
  operatorPrice = $(this).attr("name");
  document.getElementById("station-price").innerHTML = operatorPrice;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li>
  <span id="station"></span> <span id="station-price" class="qty price"> 
       </span>
</li>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Luke
  • 13
  • 8

2 Answers2

0

If I'm right you want functionality that adds the variable on the email client?

I have done a similar thing and I'm a beginner, here is how I did it.

function errorModal(dataObject) {

  // Get the parent element to append a button and h3
  const divDOM = document.querySelector('#apnd');

  // Create h3 message
  const h3DOM = document.createElement('h3');
  h3DOM.classList.add('text-white', 'text-center', 'py-3');
  h3DOM.textContent = 'An error occured :(';

  // Create the a tag 
  let aDOM = document.createElement('a');
  // Add class to the a tag
  aDOM.classList.add('btn', 'btn-lg', 'btn-warning');
  aDOM.textContent = 'SEND AN EMAIL!';

  // Email body and add a attribute href
  aDOM.href = 'mailto:hello@example.com?subject=From%20a%20Website&body=Website:%20' +dataObject.websiteForma+ '%0D%0A%20Name:%20' +dataObject.nameForma+
  '%0D%0A%20Phone:%20' +dataObject.phoneForma+ '%0D%0A%20Goals:%20' +dataObject.whatAreYourGoals+ '%0D%0A%20Where%20do%20you%20advertise:%20' +dataObject.whereDoYouAdvertise+
  '%0D%0A%20Leads%20or%20Sales:%20' +dataObject.doYouWantLeadsOrSales+ '%0D%0A%20PPC%20spend%20' +dataObject.PpcSpend+ "%0D%0A%0D%0A%20Don't%20worry%20just%20hit%20send.%20";

  // Append h3 tag to the parent
  divDOM.appendChild(h3DOM);
  // Append a tag to the parent
  divDOM.appendChild(aDOM);
}

Here's what I did

  • I made AJAX call to my PHP mailer
  • Inside the AJAX error: passed the function errorModal(dataObject) and my object.
  • Instead of adding a object like me you can pass your variable.

%20 - Is used for the space. %0D%0A - Is used for a new line.

Elvis S.
  • 362
  • 1
  • 3
  • 13
0

Figured it out heres how i did it. Convert Variable Value to PHP using Json when Submit is Clicked.

$.ajax({
  url:"jsonTest.php",
  method: "post",
  data: { operatorOne: JSON.stringify( emergencyStop ) },
  success: function(res){
  console.log(res);
  }
})
});

Then in jsonTest.php

require_once("../../../../wp-load.php");

$operatorOne = $_POST['operatorOne'];
print_r($operatorOne);



 $title = 'New Build';
 $to = 'receivers email here';
 $subject = 'New Quote'; 
 $message  = $operatorOne;

  $mail = wp_mail( $to, $subject, $message, $headers );

Successfully received an email with the variable value although its got a few back slashes in it now sure why.

Luke
  • 13
  • 8