Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Javascript variable placed in php heredoc doesn't work

    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:
    Code:
    <?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....
    Code:
    array[1] = 'May120128AM';


    ...places the literal value of:
    Code:
     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:
    Code:
    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:
    Code:
    <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:
    Code:
    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.
    Code:
    <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>

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Try

    Code:
    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;
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    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:
    Code:
    value='" +AM+"'
    works. Could you explain?

    Thanks very much for your help.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by milesdriven View Post
    I'm really curious why:
    Code:
    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.

  • #5
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Inigoesdr View Post
    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.

    Code:
    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):
    Code:
    '" +AM+"'
    This ends string one:
    Code:
    '"
    This begins string three:
    Code:
    "'
    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.
    Last edited by milesdriven; 08-07-2012 at 03:27 AM.

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    Avariable.innerHTML = "string one"+AM+"string three";
    The semi-colon is the JavaScript statement termination character. Like this full-stop.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks very much to everyone.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •