...

View Full Version : Javascript variable placed in php heredoc doesn't work



milesdriven
08-07-2012, 01:06 AM
The code below is written specifically to ask a question. Its posted here because it's in a php heredoc. I don't know if that makes a difference or not:


<?php
print <<<HELLO
<script type="text/javascript">
array = [];
array[1] = 'May120128AM';
function doSomething(AM)
{
var time = encodeURIComponent(document.getElementById(AM).value);
Avariable.innerHTML = "<button id='May120128AM_Click_To_Sch' name='apt_time' value= +\'AM+\' onClick='clickToSchedule(); return false'>Click Here To Schedule</button>"
}
doSomething(array[1]);
</script>
HELLO;
?>


I know this line....


array[1] = 'May120128AM';




...places the literal value of:


May120128AM (no quotes)

inside array[1]

The argument AM in the function doSomething(AM) above becomes equal to the literal May120128AM without quotes. That works fine in the first line of the function shown below, probably because its pure javascript:


var time = encodeURIComponent(document.getElementById(AM).value);


In the second line, javascript is writing html. Html requires single quotes around the string that is the value attribute, because those single quotes are nested inside double quotes the entire line of code is in. After the javascript in the second line above inside the brackets is finished writing the html, the html needs to look like this:


<button id='May120128AM_Click_To_Sch' name='apt_time' value= 'May120128AM' onClick='clickToSchedule(); return false'>Click Here To Schedule</button>


The variable AM contains the literal:


May120128AM


Html requires single quotes around this literal when it becomes the value of that line. I tried putting them in like shown below(look for the code in blue below. This didn't work. I tried putting the escape character inside an alert. A firebug console error told me the escape character was illegal. The script works when the string itself is put inside single quotes as the value attribute and the AM variable in placed inside the getElementById() function only. Does anyone know how I place the AM variable there so it is converted to its value and made part of the html by being placed inside single quotes? Thanks for your help.


<script type="text/javascript">
array = [];
array[1] = 'May120128AM';
function doSomething(AM)
{
var time = encodeURIComponent(document.getElementById(AM).value);
Avariable.innerHTML = "<button id='May120128AM_Click_To_Sch' name='apt_time' value= +\'AM+\' onClick='clickToSchedule(); return false'>Click Here To Schedule</button>"
}
doSomething(array[1]);
</script>

AndrewGSW
08-07-2012, 01:26 AM
Try


Avariable.innerHTML = "<button id='May120128AM_Click_To_Sch' name='apt_time' value='" +AM+"' onClick='clickToSchedule(); return false'>Click Here To Schedule</button>";

notice the semi-colon at the end;

milesdriven
08-07-2012, 01:42 AM
I tested it a few different ways (complicated script) and it looks like it works. The semi colon at the end - is it there to tell javascript the whole line is a javascript statement? What does it tell javascript when the semi colon is omitted?

I'm really curious why:


value='" +AM+"'


works. Could you explain?

Thanks very much for your help.

Inigoesdr
08-07-2012, 02:46 AM
I'm really curious why:


value='" +AM+"'


works. Could you explain?

It works because you are concatenating the value of AM to the string. How you had it previously wasn't concatenating because you didn't end your double-quote for the string. It was just the string 'AM' in part of a larger string. This is purely a JavaScript issue. :thumbsup:

milesdriven
08-07-2012, 03:50 AM
How you had it previously wasn't concatenating because you didn't end your double-quote for the string.
When I first read this part of your response I thought the string you were talking about was everything between the double quotes in my original line of code, which was almost the entire line. Now I understand you were telling me the first of the three strings that make this line did not have a double quote to close it.



Avariable.innerHTML = "string one"+AM+"string three";

AM is string two.

This is not a string in itself(my error was I thought it was a string in itself):


'" +AM+"'


This ends string one:


'"


This begins string three:


"'


What does the semi-colon at the end do that makes it essential?


Thanks for your help. Thanks for allowing me to post this here, even though it turned out to be a javascript issue. I wasn't sure if being in a heredoc changed the javascript syntax requirements.

AndrewGSW
08-07-2012, 01:16 PM
Avariable.innerHTML = "string one"+AM+"string three";

The semi-colon is the JavaScript statement termination character. Like this full-stop.

milesdriven
08-07-2012, 02:33 PM
Thanks very much to everyone.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum