[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Script Error for alert a formatted html long String

Posted on 2010-09-08
17
Medium Priority
?
336 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
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.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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