Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
Send email automatically (php) and add text from JavaScript

I have made/got a code that will get a location with java script. The idea is that it automatically emails someone with the location from the Javascript. However, how would you get the the location in the email, and how would you make it send automatically?

My code is :

<!DOCTYPE html>
<html>
<head>
<title>TestPage</title>
</head>
<body>
<h1>Test</h1>
<p>Your Loaction:
<p id="demo"></p>
<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}
window.onload = getLocation();
</script>
<?php
$to      = 'example@example.com';
$subject = 'Subject';
$message = 'Your location is' <p id="demo">''</p>;

mail($to, $subject, $message,);
?>
</body>
</html>

So i want the php to automatically send and put the Location in the email -- is this even possible? does anyone know how?

Lots of thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are mixing PHP and JavaScript.

PHP is executed on the server, THEN the result is sent to the client and renderd, THEN JavaScript is run. Your code is not syntactically correct (because of the <p> tag around the PHP string), but even if it were, it would simply send an e-mail everytime someone requests the webpage, no matter whether they look at it, or whether they have JavaScript or geolocation enabled.

What you want, for whatever reason, is two separate pages - the HTML + JavaScript one which will attempt to get the user's location, and a PHP script. The JavaScript code will need to send an AJAX request to the PHP script with the location, once it obtained it.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda