Alexander Sterk
Alexander Sterk

Reputation: 31

Send Ajax data to PHP

I have the following jQuery code on my page:

function ajaxFunction() {
$.ajax({
  url:'location.php',
  type:'POST',
  data:'lat='+lat+'&long='+long,
  success:function(d){
    console.log(d);
  },
  error(w,t,f){
    console.log(w+' '+t+' '+f);
  }
});
}

Now what should I put in the location.php to make the data go to my email box? I'm a total noob and looking up codes or tutorials won't work because I don't understand it...

Also can I just let the page redirect to another page if the Script is done? Can I just add:

window.location.href = "http://stackoverflow.com";

Or should I put more code... I found all these coeds on the internet because I don't understand anything, so could you please help me?

Upvotes: 0

Views: 458

Answers (2)

aljx0409
aljx0409

Reputation: 242

In your location.php, you could do something like this to make the data received by php script to go to your email box:

$messageBody = "lat: " . $_POST['lat'] . " long: " . $_POST['long'];
if (mail("[email protected]", "The Subject Line", $messageBody)) {
    echo "success";
    exit;
}

Check out the link for more info on mail() function: http://php.net/manual/en/function.mail.php

To redirect:

function ajaxFunction(lat, long) {
$.ajax({
  url:'location.php',
  type:'POST',
  data:'lat='+lat+'&long='+long,
  success:function(d){
    // When the ajax call is successful, the redirection should happen here
    window.location = "http://stackoverflow.com";
  },
  error(w,t,f){
    // this is when the ajax call fails
    console.log(w+' '+t+' '+f);
  }
});
}

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318508

You can use the mail() function in PHP to send an email.
You also need to fix your JS code - unless you actually made lat and long global variables they'll be undefined.

Upvotes: 0

Related Questions