Solved

Java Script Error for alert a formatted html long String

Posted on 2010-09-08
17
327 Views
Last Modified: 2012-05-10
Hi,

I have a php application, with the following code:-
<html>
<script>
function call(str)
{
alert(str);
}
</script>
<?php
$strval ="<h2>Amenities</h2>
<table style=\"width: 100%;\">
    <tbody>
        <tr>
            <td style=\"width: 50%; vertical-align: top;\"><strong>Number of Persons</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Maximum Persons: 4</li>
                </ul>
            <strong>Kitchen Ameneties</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Other: fully equipped kitchen</li>
            </ul>
            <strong>Baths</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Bathrooms: 1</li>
            </ul>
            </td>
            <td style=\"width: 50%; vertical-align: top;\"><strong>Minimum Nights Stay</strong>
            <ul style=\"margin-top: 0px;\">
                <li>all other periods: 2</li>
            </ul>
            <strong>Sleeping</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Max people: 4</li>
                <li>Number of Bedrooms: 1</li>
                <li>Other: 1 Queen bed and 2 single in livrm</li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>";

 ?>
<body onload="call(<?php echo htmlentities($strval); ?>)">

</body>
</html>

Some error occurs while loading the page.

Can you help us!

Thanks in Advance.
0
Comment
Question by:Web_Sight
  • 6
  • 4
  • 4
  • +2
17 Comments
 
LVL 5

Expert Comment

by:drakeshe
ID: 33633716
try
<body onload="call('<?php echo htmlentities($strval); ?>')">

Open in new window

0
 

Author Comment

by:Web_Sight
ID: 33633763
Hi,
I has "unterminated string literal' javascript Error!
0
 
LVL 5

Expert Comment

by:drakeshe
ID: 33633767
send me the output html source.
0
 

Author Comment

by:Web_Sight
ID: 33633773
Hi, It displays
unterminated string literal
call('<h2>Amenities</h2>\n
the code is:-
<html>
<script>
function call(str)
{
alert(str);
}
</script>
<?php
$strval ="<h2>Amenities</h2>
<table style=\"width: 100%;\">
    <tbody>
        <tr>
            <td style=\"width: 50%; vertical-align: top;\"><strong>Number of Persons</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Maximum Persons: 4</li>
                </ul>
            <strong>Kitchen Ameneties</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Other: fully equipped kitchen</li>
            </ul>
            <strong>Baths</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Bathrooms: 1</li>
            </ul>
            </td>
            <td style=\"width: 50%; vertical-align: top;\"><strong>Minimum Nights Stay</strong>
            <ul style=\"margin-top: 0px;\">
                <li>all other periods: 2</li>
            </ul>
            <strong>Sleeping</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Max people: 4</li>
                <li>Number of Bedrooms: 1</li>
                <li>Other: 1 Queen bed and 2 single in livrm</li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>";

 ?>
<body onload="call('<?php echo htmlentities($strval); ?>')">

</body>
</html>
0
 
LVL 5

Expert Comment

by:drakeshe
ID: 33633776
You cannot put html code into a message alert box...

0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 33634073
Hai,

In the body,instead of using "<body onload="call('<?php echo htmlentities($strval); ?>')">
" like this



1)
 
<?php
    $strval = htmlentities($strval);    
?>
<body onload="call('<?php echo $strval; ?>')">

2)

And the script that is used for alert should be in the same straight line,only then the javascript will display the msg


Thanks.
0
 

Author Comment

by:Web_Sight
ID: 33634412
Hi,

Actually the $strval coming from FCKEditor, so its will be the formatted one.
0
 
LVL 5

Expert Comment

by:drakeshe
ID: 33634529
Are you wanting to output the htmlcode so the visitor can see it? or do you want it to come up in a messagebox?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:drakeshe
ID: 33634537
If you want the visitor to see the htmlcode on the screen then use this:
0
 
LVL 5

Expert Comment

by:drakeshe
ID: 33634544
Woops it didnt attach... Yeah, so use this so they can see the page content...
<html>
<script>
function call(str)
{
alert(str);
}
</script>
<?php
$strval ="<h2>Amenities</h2>
<table style=\"width: 100%;\">
    <tbody>
        <tr>
            <td style=\"width: 50%; vertical-align: top;\"><strong>Number of Persons</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Maximum Persons: 4</li>
                </ul>
            <strong>Kitchen Ameneties</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Other: fully equipped kitchen</li>
            </ul>
            <strong>Baths</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Bathrooms: 1</li>
            </ul>
            </td>
            <td style=\"width: 50%; vertical-align: top;\"><strong>Minimum Nights Stay</strong>
            <ul style=\"margin-top: 0px;\">
                <li>all other periods: 2</li>
            </ul>
            <strong>Sleeping</strong>
            <ul style=\"margin-top: 0px;\">
                <li>Max people: 4</li>
                <li>Number of Bedrooms: 1</li>
                <li>Other: 1 Queen bed and 2 single in livrm</li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>";

 ?>
<body>
<?php echo($strval); ?>
</body>
</html>

Open in new window

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33634711
This will do.
<body onload="call('<?php echo stripslashes($strval); ?>')">

Open in new window

0
 
LVL 6

Expert Comment

by:Mike D
ID: 33682235
Hi,

the line breaks in php as well as js are causing your problems:

in php you will either have to concatenate each line (ex 1) or use heredoc syntax (ex 2) (see: http://php.net/manual/en/language.types.string.php) and you will have to add the correct javascript for multi-line statements (the '+').

ex 1:
$strval = '<h2>Amenities</h2>' .
'<table style=\"width: 100%;\">' .
'   <tbody>' .
'        <tr>';

ex 2:
$strval = <<<END
<h2>Amenities</h2>'+
'<table style=\"width: 100%;\">'+
'   <tbody>'+
'        <tr>
END;

In both cases you will loose the actual line-breaks in the alert box. You will have to manually add '\\n' (escaped) in order to produce line-breaks in the alert box

$strval = <<<END
<h2>Amenities</h2>\\n'+
'<table style=\"width: 100%;\">\\n'+
'   <tbody>\\n'+
'        <tr>\\n
END;


Hope this helps

JD

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33682461
Php doesn't have problems with strings spanning multiple lines. Don't say if you don't know.

This code is perfectly valid in PHP:
$str='one line

two lines';

Open in new window

0
 
LVL 6

Expert Comment

by:Mike D
ID: 33683000
Yes, in php it's perfectly valid, in the situation discussed here it will lead to a 'SyntaxError: Unexpected token ILLEGAL'.

$str2='one line'.
'two lines';

will work.

0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 33684033
This will not be the same in certain situations. I guess it could be like this:

Also a function might help.
$str2='one line'.'\n'.'two lines';



function prepare_text_for_javascript($a) {

return str_replace("\n", '\n', $a);

}

Open in new window

0
 
LVL 12

Accepted Solution

by:
Rok-Kralj earned 500 total points
ID: 33684056
Complete code:
<html>

<script>

function call(str)

{

alert(str);

}

</script>

<?php

$strval ="<h2>Amenities</h2>

<table style=\"width: 100%;\">

    <tbody>

        <tr>

            <td style=\"width: 50%; vertical-align: top;\"><strong>Number of Persons</strong>

            <ul style=\"margin-top: 0px;\">

                <li>Maximum Persons: 4</li>

                </ul>

            <strong>Kitchen Ameneties</strong>

            <ul style=\"margin-top: 0px;\">

                <li>Other: fully equipped kitchen</li>

            </ul>

            <strong>Baths</strong>

            <ul style=\"margin-top: 0px;\">

                <li>Bathrooms: 1</li>

            </ul>

            </td>

            <td style=\"width: 50%; vertical-align: top;\"><strong>Minimum Nights Stay</strong>

            <ul style=\"margin-top: 0px;\">

                <li>all other periods: 2</li>

            </ul>

            <strong>Sleeping</strong>

            <ul style=\"margin-top: 0px;\">

                <li>Max people: 4</li>

                <li>Number of Bedrooms: 1</li>

                <li>Other: 1 Queen bed and 2 single in livrm</li>

            </ul>

            </td>

        </tr>

    </tbody>

</table>";



function prepare_text_for_javascript($a) {

     return str_replace("\n", '\n', $a);

}



 ?>

<body onload="call('<?php echo htmlentities(prepare_text_for_javascript($strval)); ?>')">



</body>

</html>

Open in new window

0
 

Author Closing Comment

by:Web_Sight
ID: 33698455
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now