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

202
Visualizações
Pass javascript string variable to another page and get the variable in PHP with AJAX

I basically don't seem to understand sending a variable to another page. I've tried PHP sessions, javascript cookies and ajax POST and GET. I'm trying to send the innerHTML of a div, with the data created by a jQuery call, a variable called savedartists. It displays correctly in the console.log on the sending page but the $_POST['savedArtists'] is undefined in the receiving page. I have spent hours looking at different posts on this site but I haven't been able to get it to work. Any help is appreciated.

<input class="et_pb_button et_pb_button_0 et_pb_bg_layout_light" onClick="savequote();" type="button" id="savedchoices" value="Commander la prestation" >

<script>
function savequote() {
    var savedartists = document.getElementById('selectedList').innerHTML;
    console.log(savedartists);
    $.ajax({
        type: "POST",
        url: 'example.com/artiste/mise-en-relation/',
        data: { savedArtists : savedartists },
        success: function(data) {
            console.log("success!");
            location.href = "example.com/artiste/mise-en-relation/";
        }
    });
}
</script>

On the receiving page (example.com/artiste/mise-en-relation/)

<?php
if(isset($_POST['savedArtists']))
{
    $uid = $_POST['savedArtists'];
    echo $uid;
} else {
    echo 'zit!';
}
?>

Thanks for your time

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Capturing as an answer for future readers...

Fundamentally what's happening here is that two requests are being made to the target page. The first one is the AJAX request:

$.ajax({
    type: "POST",
    url: 'example.com/artiste/mise-en-relation/',
    data: { savedArtists : savedartists },
    success: function(data) {
        //...
    }
});

This is a POST request which contains the data you expect, and works just fine. However, the result of this request is being ignored. That result is available in the success callback, but the code doesn't do anything with it:

console.log("success!");
location.href = "example.com/artiste/mise-en-relation/";

Instead, what the code is doing is performing a redirect. This creates a second request to that same page (though it's essentially irrelevant that it's the same page). This is a GET request and contains no data to send to the server.

At its simplest, you should either use AJAX or redirect the user. Currently you're mixing both.

I want to redirect to the other page.

In that case AJAX is the wrong tool for the job. You may not even need JavaScript at all, unless you want to modify the elements/values of a form before submitting that form. But if all you want is to POST data to another page while directing the user to that page, a plain old HTML form does exactly that. For example:

<form method="POST" action="example.com/artiste/mise-en-relation/">
  <input type="text" name="savedArtists">
  <button type="submit">Submit</button>
</form>

In this case whatever value the user enters into the <input> will be included in the POST request to example.com/artiste/mise-en-relation/ when the user submits the form.

about 4 years ago · Juan Pablo Isaza 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