Log in

View Full Version : javascript php quotes



cursed
02-19-2007, 02:58 AM
How would I do something like this?



<?php

echo "<script type="text/javascript">
function openAlert() {
Dialog.alert("Add your <b>HTML</b> message here", {windowParameters: {className: "alphacube"}})
}

</script>";

?>

Baiscally trying to use quotes in javascript to work with php.

thetestingsite
02-19-2007, 03:01 AM
Either escape the quotes with a backslash ( \ ) or use single quotes ( ' ).

An example:



<?php

echo "<script type=\"text/javascript\">
function openAlert() {
Dialog.alert(\"Add your <b>HTML</b> message here\", {windowParameters: {className: \"alphacube\"}})
}

</script>";

?>


Notice the backslashes.

Hope this helps.

cursed
02-19-2007, 03:07 AM
so would this work?

echo " <script type=\"text/javascript\" src="/javascripts/prototype.js\"> </script>
";

thetestingsite
02-19-2007, 03:18 AM
Only if you did like this:



echo " <script type=\"text/javascript\" src=\"/javascripts/prototype.js\"> </script>
";


Notice the slashes in red.

Hope this helps.

Added Later: Just some FYI, if you have an echo with double quotes around a string ( " ), it would be wise to use single quotes in the string if needed (like in the above snippet, quotes are needed). Otherwise, backslashes to escape the quotes inside quotes would do just as well.

cursed
02-19-2007, 04:21 AM
im getting an error:
Parse error: syntax error, unexpected T_STRING in /home/public_html/config.php

heres my code:



$systemMessageStart = "<BR>"<script type=\"text/javascript\">
function openAlert() {
Dialog.alert(\"";

$systemMessageEnd = "\", {windowParameters: {className: \"alphacube\"}})
}

</script>";

codeexploiter
02-19-2007, 08:40 AM
<?php

echo "<script type=\"text/javascript\">function openAlert() { Dialog.alert(\"Add your <b>HTML</b> message here\", {windowParameters: {className: \"alphacube\"}})}</script>";

?>


The above mentioned solution is based on the code that you had pasted in your first posting. If you want to assign this script tag line into some variable then not much change in the syntax, have a look at the following item


$scriptLine = "<script type=\"text/javascript\">function openAlert() { Dialog.alert(\"Add your <b>HTML</b> message here\", {windowParameters: {className: \"alphacube\"}})}</script>";

or


$scriptLine = "<script type='text/javascript'>function openAlert() { Dialog.alert('Add your <b>HTML</b> message here', {windowParameters: {className: 'alphacube'}})}</script>";

I personally prefer the first notation as it clearly distinguish items with little difficulty compared to the other method

mburt
02-19-2007, 11:04 AM
Be careful when adding JavaScript events to objects via PHP. See here:

<div onclick="<?php if ($condition) {echo "openAlert('argument1','argment2')";}; ?>"
Must be single quotes. It would be trying to put double-quotes inside double-quotes.

<div onclick="<?php if ($condition) {echo "openAlert(\"argument1\",\"argment2\")";}; ?>"
would be invalid.

Twey
02-19-2007, 02:42 PM
It's much easier to break out of PHP parsing mode:
function displaySystemMessage($msg) {
?>
<script type="text/javascript">
function openAlert() {
Dialog.alert(
"<?php echo($msg); ?>",
{ 'windowParameters' : { 'className' : "alphacube" } }
);
}
</script>
<?php
}Wherever you output HTML in any form, always break out of PHP parsing mode if possible. It results in code that's much easier to read and debug, increases the efficiency of your script, and avoids having to struggle with escaping quotes.

cursed
02-19-2007, 11:58 PM
heres my code:




$systemMessageStart = ?><BR><script type="text/javascript">
function openAlert() {
Dialog.alert(
<?php

$systemMessageEnd = ?>", {windowParameters: {className: "alphacube"}})
}

</script><?php


what am i missing? I get an error.

thetestingsite
02-20-2007, 12:27 AM
What's the error? The only thing I can see that's wrong with the code you posted is the ending <?php that either doesn't end, or should not be there at all. Post the error message though, it could be something else.

Hope this helps.

cursed
02-20-2007, 12:50 AM
Parse error: syntax error, unexpected ';' in /home/public_html/config.php on line X

the line number doesnt do anything because i didnt post all of my config.

and <?php was for the other part of the code in config.php

Twey
02-20-2007, 11:44 AM
You can't assign the result of a non-parsed block to a variable like that. Use a function as I demonstrated.

cursed
02-22-2007, 01:16 AM
heres my edited version:

$message {
?> <script type="text/javascript">
function openAlert() {
Dialog.alert(
"<?php echo "$showDate$system_message[message]"; ?>",
{ 'windowParameters' : { 'className' : "alphacube" } }
);
}
</script>
<?php }

Now i have this error:

Parse error: syntax error, unexpected ';' in /home/public_html/header.inc.php on line X

Note: I edited a different file

Twey
02-22-2007, 12:14 PM
(sigh)
You can't assign the result of a non-parsed block to a variable like that. Use a function as I demonstrated.Can't, can not, may not.