I am trying to pass a variable frm php to a javascript function, however a space keeps getting appeneded, and I can not see how.
The relevant php code snippet:
<p><a href='#' onclick=\"makewindows(" . $html . "); return false;\">Click for full description </a></p>".$brand."
<p><a href=\"#\" onclick=\"deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>
$brand is what I want to pass, and deleteRec is the name of the function.
At the end of the first line I am echoing out brand before the link to deleteRec, and it contains no space. In my test case, it is set to simply 'o'.
The link that is genereated for deleteRec however, clearly contains a space, and I don't know where it is coming from.
<a href="#" onclick="deleteRec('190274380300', ' o', '2', '250343889611')">DELETE</a>
Change:
<p><a href=\"#\" onclick=\"deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>
to:
<p><a href=\"#\" onclick=\"deleteRec('".$ARTICLE_NO."', '".trim($brand)."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>
and tell us how it goes.
Do var_dump($brand) and look closely - there's almost certainly a space in it!
In which case, you can guard against it with trim
$brand=trim($brand);
Try do echo the following:
echo "--$brand--";
This way you'll be able to see if there are any spaces in the variable.
As a general matter of style, I would change second link from:
<a href=\"#\" onclick=\"
deleteRec('".$ARTICLE_NO."', '".$brand."', '".$pg."', '".$nextArticleNo."')\">DELETE</a>
to:
<?php
$deleteRecArgs = "'$ARTICLE_NO', '$brand', '$pg', '$nextArticleNo'";
?>
<a href="#" onclick="deleteRec(<?php echo $deleteRecArgs?>)">DELETE</a>
It's easier to read and maintain.