SOLVED
Wrapping each instance of the & character in the php with htmlspecialchars() fixed my problem by outputting &
instead of &
and allowed the next page to recognize each of the variables correctly!
/SOLVED/
I am currently working on a WordPress site and am trying to create some javascript in the php and then echo it onto the page. Part of this javascript opens a new window/tab using window.open() and a url I have generated in the php. However, when I echo the variable containing the url, the all the &
characters are auto encoded as &
.
Here is an example of what is happening:
...
?>
<script type="text/javascript">
var url = "&";
var url2 = "<?php echo "&"; ?>";
var url3 = <?php echo json_encode("&"); ?>;
var url4 = "<?php echo html_entity_decode("&"); ?>";
var url5 = "<?php echo html_entity_decode("&"); ?>";
</script>
<?php
...
Then the actual javascript that is shown when I inspect the element where the script is being inserted is
...
<script type="text/javascript">
var url = "&";
var url2 = "&";
var url3 = "&";
var url4 = "&";
var url4 = "&";
</script>
...
I have tried urlencode()-ing all of the &’s out of the url, but then $_GET does not recognize them as url variables.
Is this just a WordPress thing, or is it a php thing? And, is there anything I can do to make the &’s show up as just &’s?!
EDIT
Since there seems to be some confusion on what I’m trying to do, let me explain further. I am creating a url using php, not javascript. I am then generating some javascript using php and then echoing that out onto the page. The above excerpt only shows the problem that I am actually facing. The actual cod segment looks something like this:
$header_element .= "<script type="text/javascript">n"
. "function validateForm(form) {n"
. "tvar isInvalid = false;n"
. $str_all_validation
. "tif (!isInvalid) {n"
. "ttrunReport(form);n"
. "t} else {n"
. "talert('You must enter all report parameters.');n"
. "t}n"
. "}n"
. "function runReport(form) {n"
. "twindow.open('/?page_id=172&PageId=$str_page_id&ReportName=$str_report_name"
. "$str_all_param, '_blank')n"
. "}n"
. "</script>";
The above outputs correctly aside from having all of the & characters output as &
.
I think your encoding for either your page or your php is mixe up.
try adding this line in your php:
this should make sure that whatever you output using php is in UTF-8.
And make sure your encoding for your javascrip is the same.
PS You can change the
charset=UTF-8
part for something likecharset=ISO-8859-1
or whatever encoding your using in your javascript.EDIT:
DO NOT USE THE LAST ANSWER NOT AN ENCODING PROBLEM!!!
try and replace
html_entity_decode()
with thehtmlspecialchars()