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

148
Visualizações
How to send data using jQuery ajax

this is my html

<!-- <form action="view.php" method="post">  -->
        <input type="text" name="text" id="name">
        <input type="submit" value="submit">
    <!-- </form> -->

I don't want to send the data by using form Couse it only send named attribute data that's why I am sending data by $.ajax() method and this is script

$(document).ready(function () {
    var details = {};
    $('input[type="submit"]')
        .click(function () {
            details = { name: $('input[type="text"]').val() };
            $.post({
                url: "view.php",
                method: "post",
                data: details,
                success: function (result) {
                    console.log("done")}
             });
           window.open('view.php','_self');
});});

after all this thing i want to retrieve the data on view.php file but i am fail to achieve it on that file it is showing the array is empty

<?php
var_dump( $_POST);
//the array is empty
?>

suggest me something please.

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

0

The ajax request and the request made from window.open are two different requests. Think of it like two different phone calls that you'd receive... And having absolutely no "memory" of the conversation at the end. You would need a database for that "memory" and it's something else.

To send some data to the backend (to process it) and want to have a result back into the original page, use the success callback.

HTML in original page:

<input type="text" name="text" id="name">
<input type="button" value="submit">
<div id="backendResult"></div>

JS in original page:

$(document).ready(function () {
  var details = {};
  $('input[type="submit"]').click(function () {
    details = { name: $('input[type="text"]').val() };
    $.post({
      url: "view.php",
      method: "post",
      data: details,
      success: function (result) {
        console.log("done")
        $('#backendResult').html(result)
      }
    });
  });
});

view.php:

<?php
$name = $_POST['name'];
echo 'Hello '.$name.'!';
?>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do like this:

$(document).ready(function () {
    var details = {};
    $('input[type="submit"]')
        .click(function () {
            details = { name: $('input[type="text"]').val() };
            $.post({
                url: "view.php",
                method: "post",
                data: details,
                success: function (result) {  // {name : 'the text you typed'}
                    console.log("done")
                    $("body").load('view.php', function(){
                       $("#name").val(result.name);
                    });
                }
            });
        });
});
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