Upload JS information to PHP

Asked

Viewed 87 times

0

I need to take the information from a JS file (form.js) and load it into a PHP file (mail.php).

In the form.js file there is a function that calculates the value of the delivery based on the CEP groups. Here is its variable:

var taxadeentrega = total - taxa;

Below is an excerpt from the code for your understanding:

//aqui eu pego o cep
var cep = document.getElementById("cep").value;

//verifica se deve incrementar ou não
if(cep == "20010-090" || cep == "20020-100" || cep == "20021-130" || cep == "20021-315" || cep == "20030-901" || cep == "20030-021" || cep == "20210-030" || cep == "24220-280"){               
    //se for um dos ceps acima, incrementa 1.2 no valor final
    taxa = 1.50;            
}
if(cep == "20020-010"|| cep == "22050-032" || cep == "20020-040" || cep == "20020-080" || cep == "20030-905" || cep == "24220-031" || cep == "20002-010" || cep == "20030-015"){
    //se for um dos ceps acima, incrementa 0.7 no valor final
    taxa = 1.00;            
}

total += taxa;

if(taxa != 0){
    //caso a taxa seja diferente de 0, mostra ao usuário
    document.getElementById("idTaxa").innerHTML = "Custo adicional: R$ " + taxa;
}

This is the value of the delivery, I need this information to be loaded in my mail.php file where today it pulls straight from the form field like this:

$entrega = $_POST["taxadeentrega"];

I need that information properly to apply another rule. In short, I need $entrega = VALOR_DA_TAXA which will be pulling from the form.js file

  • If the receipt is by a POST, there should be a sending of form for receipt or, a sending AJAX for the PHP page. There are these negotiations?

  • You can use GET method?

1 answer

0

You could submit the form via post, but if you want to deal directly with JS and PHP, you can use AJAX, below an example with jQuery:

$.post("mail.php", {taxadeentrega: taxa})
.done(function(data){
    // CASO TENHA CONCLUIDO
    console.log(data);
})
.fail(function(data){
    // CASO TENHA FALHADO
});

PS: If you’re going to use pure Javascript, go to that reference.

  • Thank you Anderson for your attention, but I don’t understand! Where do I put it? In the JS or PHP file, I’m a programming layman.

  • is a JS script, but for you to use this script you need to use jQuery. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> put this tag inside the <head> in your project

Browser other questions tagged

You are not signed in. Login or sign up in order to post.