1

Anyone got a way of thinking about this?

I'm going a bit bats working on this:

$toReturn .= "
    function addProd(pExists) 
    {
    document.getElementById('products').innerHTML = \"<tr><td id='prod_n'><input type='text' size='10' maxlength='10' name='proj_n' id='prod_n' onchange=".chr(92).chr(34)."saveData(setSaveParams('ajaxSaveDataController.php', 'PROD', 'n'), this.value)".chr(92).chr(34)." value='";

$toReturn .="'></td>

                                    <td id='prod_spec'>    <textarea cols='60' name='prod_spec' id='prod_spec' rows='20' onchange=".chr(92).chr(34)."saveData(setSaveParams('ajaxSaveDataController.php', 'PROD', 'prod_spec'), this.value)".chr(92).chr(34)." value='";                                    

$toReturn .="'></td></tr>\" + document.getElementById('prodsTab').innerHTML;

if (pExists == 0)
{
    document.getElementById('prodsTab').innerHTML = \"<tr><th id='proj_spec_h'>Name</td><th id='proj_spec_h'>Spec</td></tr>\" + document.getElementById('prodsTab').innerHTML;";

I've transcribed that so don't worry overmuch about an off topic typo.

What's going on here is I'm using PHP to write Javascript that calls an Ajax function, and what's blowing me away is the "s and the 's

So I'm writing a return string, in PHP, so all that's in "

Then I want to write some innerHTML, so that I can put inside \"

Then the HTML arguments can be in '

All's fine so far.

So then my onchange call is supposed to end up as onchange="function('a', 'b')", well, where are my "s going to come from? If I use " that'll end the PHP string. If I use \" that'll end the innerHTML string. So I ended up getting PHP to interpret this chr(92).chr(34)

So that's very messy and it's starting to hurt my head.

And now it's failing on that blank line after the second $toReturn, which I wanted there for readability.

I must be doing something wrong in terms of style or, something.

How do you handle nested "s?

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
JNAScarb
  • 53
  • 7
  • Why can't you just hard code this in the html, or a js file? I don't see any dynamic data going in there - just static strings. – gilly3 Jan 24 '12 at 22:03
  • It will get more dynamic once I'm through this glitch, and the 'value' is inserted there with PHP, I just removed that bit for clarity. – JNAScarb Jan 25 '12 at 12:22

4 Answers4

4

Consider using the HEREDOC syntax. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

<?php

$my_string = <<<EOD
    <script type="text/javascript">
        var x = "this string has a ' known as a single quote!";
    </script>
EOD;


?>

The advantage with this syntax is being able to freely write your html or javascript without escaping quotes.

Update

Here is a little more info on HEREDOC.

First and foremost the most common error related to heredoc syntax is caused by whitespace. HEREDOC syntax requires an opening and closing identifier. In the example above I have used EOD as the identifier. It is important that the opening identifier has no whitespace after it and the closing identifier is on a new line, has no whitespace before it and no whitespace between the identifier and the semi-colon.

You do not have to use EOD as your identifier. You can use something more descriptive. I like to use an identifier that describes the block of code.

<?php

$html = <<<HTML
    <html>
        <head>
            <title>Example html</title>
        </head>
        <body>
            <p class="paragraph">This is my html!</p>
        </body>
    </html>
HTML;

$javascript = <<<JAVASCRIPT
    <script type="text/javascript">
        var helloText = "Hello, my name is Jrod";
        alert(helloText);
    </script>
JAVASCRIPT;

?>

To use variables inside your heredoc syntax you will need to use curly braces around your variable in the code.

$widget = <<<WIDGET
    <p>Good Morning {$name}</p>
WIDGET;
Jared
  • 12,406
  • 1
  • 35
  • 39
4

Use a double escaped double quotes:

\\\"

In this way when this will get printed, it will become a simple escaped double quote (\") and it won't be in the innerHTML.

entropid
  • 6,130
  • 5
  • 32
  • 45
3

Have you considered tried quoting the quotes Such as \\\"?

After one step, this will usually become \", and after the second step ".

Anyway, generating (javascript) code inside (html) code from (php) code is bound to become messy. Consider using a templating system, or separating concerns.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
1

Another trick is defining the strings in javascript before putting them into the onchange, then using single quotes there

<script type='text/javascript'>
<!--
var a = 'a';
var b = 'b';
-->
</script>
...
<... onchange='function(a, b)'>
Ben Barden
  • 2,001
  • 2
  • 20
  • 28