?
Solved

Java Script Error for alert a formatted html long String

Posted on 2010-09-08
17
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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
 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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