Reputation: 467
I have a typical ajax call like so:
$.ajax({
method: "GET",
url: "something.php?",
data: data,
success: function(data){
//something//
}
});
There is nothing wrong with this code. Now, corresponding with this is my something.php
, which is like so:
<?php
print("<p>Something</p>");
?>
My PHP subsequently returns <p>Something</p>
, p tags and all. I would like to know what I should be doing for the ajax response to return with html formatting so that it recognizes the p tags.
EDIT: The problem here isn't about outputting random text. My something.php
is meant to print data. Thus, it has to come from the PHP url route. I am having trouble understanding how to output html tags alongside php data. like echo <p>$something</p>
Currently, this would return precisely <p>\\data here</p>
with the p tags.
Upvotes: 1
Views: 436
Reputation: 79
You should use an id
in p
tag
$.ajax({
method: "GET",
url: "something.php?",
data: data,
success: function(data){
// find p tags with desire_output id and replace it by
// responds value to inner html of p tag.
$("#desire_output").html("Something else");
}
});
If you don't understand anything regarding this, you can feel free knock.
Upvotes: 1
Reputation: 607
You can use jQuery
to look for the p
tag in the DOM and change it on callback.
$.ajax({
method: "GET",
url: "something.php?",
data: data,
success: function(data){
$("p").text("Something else"); // find p tags and replace text value
}
});
Keep in mind that will change all p
tags on the page unless you specify which one with a class/id attribute. Remember too that once the PHP page has loaded and you start making ajax calls you will be manipulating the DOM elements on the rendered page (p, div, etc) with javascript and jQuery - not with PHP.
Upvotes: 3