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

239
Visualizações
jQuery AJAX datatype html using

Hi I'm using jquery ajax function with php. Here is the my problem; Firstly I'm using datatype:"html" my problem is php variables not returning.

js

$.ajax({
type: "POST",
dataType: "html",
url: ajaxurl,
data:dataajax,
success:function(data) {
    var $data = $(data);
    $(".list").append($data);
},
error : function(jqXHR, textStatus, errorThrown){
}});

php

echo "<div>".$_POST['value']."</div>";

if I use like this it's working but when I remove the html tags ajax return nothing.

broken php

echo myfunction($_POST['value']);
echo $_POST['value'];

How can I fix this or can I use return $output with jquery ajax?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem is with your success callback function.

data is a html string, and as such you don't need to wrap it in jquery.

Use this.

$.ajax({
type: "POST",
dataType: "html",
url: ajaxurl,
data:dataajax,
success:function(data) {

    $(".list").append(data);
},
error : function(jqXHR, textStatus, errorThrown){
}});
about 4 years ago · Santiago Trujillo Relatório

0

Ajax uses JSON data for your request and response. what you are going to want to do is return a JSON variable so that the javascript response function can access elements properly.

Luckily php is designed to do just that thing.

$ajaxData = $_POST['value'];
echo json_encode(array('response' => $ajaxData));

what are you actually trying to do with this data? if all you need to do is wrap some html in a div you can use functions like wrap() in javascript and you wont actually have to send an ajax to the server.

Either way you should really consider your data needs and try to send a json object in the ajax request so that there are actual variables to send. html markup is probably not going to be useful for the php code.

about 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