PHP Echo Ampersand Ajax Anchor Tag Issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jockey_jockey
Forum Newbie
Posts: 3
Joined: Fri Apr 25, 2008 2:10 am

PHP Echo Ampersand Ajax Anchor Tag Issues

Post by jockey_jockey »

Hello,

I have an XSL file and I am firing an Ajax call from a link that would do a GET request to the PHP script and it would return a string that basically holds a snippet of HTML and as I get back the response, the Ajax callback would manipulate an exisiting section of the HTML with the response just received.

Its working fine except that I am printing anchor tags which is a GET request to some script. The problem is that the "Ampersand" is not correctly passed on to the XHTML page.

Snippets are below:

Script that creates the string and echoes on the server side:Snippet:

$print .= "<td>";
$print .= "<a href='" . "main.php?page=config_detail" . "&" . "app=" . $_GET['app'] . "&" . "config=" . $config_id_ex[$x]. "'>" .$config_name_ex[$x]. "</a>";
$print .= "</td>";

Link that is actually present on the XHTML file after the AJAX is executed is:

http://localhost/main.php?page=config_d ... onfig=1000

But I want it to be "&".

Please help,
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: PHP Echo Ampersand Ajax Anchor Tag Issues

Post by Kieran Huggins »

jockey_jockey
Forum Newbie
Posts: 3
Joined: Fri Apr 25, 2008 2:10 am

Re: PHP Echo Ampersand Ajax Anchor Tag Issues

Post by jockey_jockey »

Hello,

Thanks for you reply,

I appreciate that. I am doing the exact same thing, just not using the function itself, but I tried that too. It still gets ouputted on the page as:

main.php?page=config_detail&app=26&%2338;config=1000

There is an anchor that calls a PHP page that creates the string of html and prints it at the last. Its being received by an Ajax Callback:

$print .= "<td>";
$print .= "<a href='" . "main.php?page=config_detail" . "&" . "app=" . $_GET['app'] . "&" . "config=" . $config_id_ex[$x]. "'>" .$config_name_ex[$x]. "</a>";
$print .= "</td>";

echo $print;

Javascript (Jquery):

$.ajax({
url: page,
type: 'GET',
data: params,
dataType: 'text',
timeout: 10000000,
error: function(xhr,err,e) {
alert('error getting raw data' + err);
},
success: function(text){
//alert ("Success");
//alert(msg);
//alert()
$("table#detail_table").html(text);
//$("div#test").html(msg);
//alert ("Success Again");
}
});

text holds the string sent to the Javascript by the PHP.

Thanks,
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: PHP Echo Ampersand Ajax Anchor Tag Issues

Post by Kieran Huggins »

install firebug (a firefox addon) and watch the AJAX call run - take a look at the response text when the ajax call fires. Is it properly escaped there?
jockey_jockey
Forum Newbie
Posts: 3
Joined: Fri Apr 25, 2008 2:10 am

Re: PHP Echo Ampersand Ajax Anchor Tag Issues

Post by jockey_jockey »

Kieran Huggins wrote:install firebug (a firefox addon) and watch the AJAX call run - take a look at the response text when the ajax call fires. Is it properly escaped there?

I did that and the response has all &s as "&".

More details:

The URLs that are being generated is a part of a custom framework that we use. So the main.php?page=config_detail means that I go to main.php which redirects me to a page called config_detail.php.

Code that has the anchor to the page generating the XHTML fragment:

<a id="all_data" name="main.php?process=spec_run&app={$sItem}&all" style="color:#FFFFFF;"> view all </a>

So this goes to a page called spec_run at a specific location which is not important for the problem that I have.

The request is handled by an AJAX Call (Jquery) as follows:

$(document).ready(function() {

$("a#all_data").click(function() {

var furl = $(this).attr("name");
var turl = furl.split("?");
var page = turl[0];
var params = turl[1];

$.ajax({ //Building request
url: page,
type: 'GET',
data: params,
dataType: 'html', //Datatype of the response
timeout: 10000000,
error: function(xhr,err,e) {
alert('error getting raw data' + err);
},
success: function(text){
$("table#detail_table").html(text); //On Success, replacing the table tag that has the id: "detail_table" with the contents that it received
}
});

return false;

});
});

The code that generates the XHTML on the spec_run page:

$print = "";
$print .= "<table id='detail_table'>";
$print .= "<thead>";
$print .= "<tr>";
$print .= "<a class='sort_link' href='#'>Build</a>";
$print .= "</th>";
$print .= "<th>";
$print .= "<a class='sort_link' href='#'>Config</a>";
$print .= "</th>";
$print .= "<th>";

for ($x=0; $x<$r; $x++) {
if($x % 2 == 0)
$print .= '<tr class="odd">';
else
$print .= '<tr class="even">';

//Build:
$print .= "<td>";
$print .= "<a href='main.php?page=build_detail&app=" .$_GET['app']. "&build=" .$build_id_ex[$x]. "'>" .$build_name_ex[$x]. "</a>";
$print .= "</td>";

//Config:

$print .= "<td>";
$print .= "<a href='" . "main.php?page=config_detail" . "&" . "app=" . $_GET['app'] . "&" . "config=" . $config_id_ex[$x]. "'>" .$config_name_ex[$x]. "</a>";
$print .= "</td>";


$print .= "</tr>";
$print .= "</tbody>";
$print .= "</table>";

echo $print;


The tables is being rendered correctly and everything else looks fine except the anchor tags that have messed up ampersands. It actually gets rendered as:

main.php?page=config_detail&app=26&%2338;config=1000

Please advice,
Post Reply