Solved

Java Script Error for alert a formatted html long String

Posted on 2010-09-08
17
332 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

696 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