Solved

Java Script Error for alert a formatted html long String

Posted on 2010-09-08
17
330 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php date function ( diffrence between two dates ) 6 40
PHP Curl Problem 10 35
How do I remove "" from json_encode 5 20
PHP $_POST vs asp request 4 22
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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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