/    Sign up×
Community /Pin to ProfileBookmark

I need help with the javascript syntax.

I’m using a javascript to call a php page. the php looks up some db choices and passes the output back to the html page.

Originally, the php output was a simple link and it worked fine.

Now, I’m trying to make that output a little more complicated and it fails.

The php generates the code I want (I’ve checked it as a standalone) but when passing back to the html page it fails as an unterminated string constant.

productpage.htm:

<SCRIPT LANGUAGE=”JavaScript” type=”text/javascript” SRC=”lookup2a.php?prodName=Bar F Bell Boots B250″>
</SCRIPT>

lookup2a.php:

some code….

?>

<!–
document.write(“<?php echo $cartlink; ?>”);
//–>

the $cartlink produces good html code:
<form method=’POST’ action=’cart3.php’>
<input type=’hidden’ name=’action’ value=’add_item’>
<input type=’hidden’ name=’prodId’ value=’286′>
<input type=’hidden’ name=’qty’ value=’1′>
<br>Available Sizes:<br>
<select name=’size’ size=’1′><option value=’large’>large<option value=’medium’>medium<option value=’small’>small</select>
<br>Available colors:<br>
<select name=’color’ color=’1′><option value=’Black’>Black<option value=’Burgundy’>Burgundy<option value=’Hunter Green’>Hunter Green<option value=’Lavender’>Lavender<option value=’Navy Blue’>Navy Blue<option value=’Neon Green’>Neon Green<option value=’Neon Pink’>Neon Pink<option value=’Pink’>Pink<option value=’Purple’>Purple<option value=’Red’>Red<option value=’Royal Blue’>Royal Blue<option value=’Silver’>Silver<option value=’Turquiose’>Turquiose<option value=’White’>White</select>
<br><input type=’Submit’ value=’Add Bar F Bell Boots B250 to Shopping Cart’></form><br>

BUT,

within the produt.htm page, I get the error:
unterminated string literal

and it points to the document.write(” spot.

I’ve tried various combinations of quotes, single quotes, and no quotes, but haven’t found the right combination.

Anybody know the right combo for this?
Thanks

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrMar 31.2004 — [i]Originally posted by steveR [/i]

[B]BUT,



within the produt.htm page, I get the error:

unterminated string literal



and it points to the document.write(" spot.



I've tried various combinations of quotes, single quotes, and no quotes, but haven't found the right combination.



Anybody know the right combo for this?

Thanks [/B]
[/QUOTE]

PHP is processed by the Server; it's already been delt with after the page loads. So, you can't execute any PHP code unless you reload the page. You can't execute PHP code with JS, because JS is client side, and PHP is server side.
Copy linkTweet thisAlerts:
@steveRauthorMar 31.2004 — yep, I agree.

the js goes and starts the script. because the src file is a php file, it goes out to the server and executes that script.

when the php script falls through to the end, its final line is formatted in javascript.

<? //php file

bunch of php code....

?>

<!--
document.write("<?php echo $cartlink; ?>");
//-->


the document.write works within the htm page. (or it used to)

This works fine with a simple $cartlink variable. In my original version $cartlink would be something like this:

$cartlink = '<a href='cart2.php?action=add_item&prodId='.$prodId.'&qty=1'>Add '.$prodName.' to Shopping Cart</a><br>';

That would work. But now, when I try to make a more complicated piece of html code, it fails.

I've seen people do the same sort of thing with document.getElementbyId(), but I didn't fully understand that one either.
Copy linkTweet thisAlerts:
@steveRauthorMar 31.2004 — maybe this helps:

I can take all references to php out and only have the jscript within the htm file:

product.htm:

...some html code....

<SCRIPT LANGUAGE="JavaScript" type="text/javascript" >

<!--//

document.write("

<form method='POST' action='cart3.php'>

<input type='hidden' name='action' value='add_item'>

<input type='hidden' name='prodId' value='286'>

<input type='hidden' name='qty' value='1'>

<br>Available Sizes:<br>

<select name='size' size='1'><option value='large'>large<option value='medium'>medium<option value='small'>small</select>

<br>Available colors:<br>

<select name='color' color='1'><option value='Black'>Black<option value='Burgundy'>Burgundy<option value='Hunter Green'>Hunter Green<option value='Lavender'>Lavender<option value='Navy Blue'>Navy Blue<option value='Neon Green'>Neon Green<option value='Neon Pink'>Neon Pink<option value='Pink'>Pink<option value='Purple'>Purple<option value='Red'>Red<option value='Royal Blue'>Royal Blue<option value='Silver'>Silver<option value='Turquiose'>Turquiose<option value='White'>White</select>

<br><input type='Submit' value='Add Bar F Bell Boots B250 to Shopping Cart'></form><br>

");

//-->

</SCRIPT>


And I get an unterminated string literal error. the console points to document.write("
Copy linkTweet thisAlerts:
@Paul_JrMar 31.2004 — [i]Originally posted by steveR [/i]

[B]And I get an unterminated string literal error. the console points to document.write(" [/B][/QUOTE]

In JavaScript, everything must be on one line to work. If you want to break this code up into multiple lines as you have, you must concatenate the lines together, like so:
[size=1]
document.write(
[color=red]"[/color]&lt;form method='POST' action='cart3.php'&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;input type='hidden' name='action' value='add_item'&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;input type='hidden' name='prodId' value='286'&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;input type='hidden' name='qty' value='1'&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;br&gt;Available Sizes:&lt;br&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;select name='size' size='1'&gt;&lt;option value='large'&gt;large&lt;option value='medium'&gt;medium&lt;option value='small'&gt;small&lt;/select&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;br&gt;Available colors:&lt;br&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;select name='color' color='1'&gt;&lt;option value='Black'&gt;Black&lt;option value='Burgundy'&gt;Burgundy&lt;option value='Hunter Green'&gt;Hunter Green&lt;option value='Lavender'&gt;Lavender&lt;option value='Navy Blue'&gt;Navy Blue&lt;option value='Neon Green'&gt;Neon Green&lt;option value='Neon Pink'&gt;Neon Pink&lt;option value='Pink'&gt;Pink&lt;option value='Purple'&gt;Purple&lt;option value='Red'&gt;Red&lt;option value='Royal Blue'&gt;Royal Blue&lt;option value='Silver'&gt;Silver&lt;option value='Turquiose'&gt;Turquiose&lt;option value='White'&gt;White&lt;/select&gt;[color=red]"[/color]
[color=red]+"[/color]&lt;br&gt;&lt;input type='Submit' value='Add Bar F Bell Boots B250 to Shopping Cart'&gt;&lt;/form&gt;&lt;br&gt;[color=red]"[/color]
);
[/size]

What that's doing is splitting each line up into it's own string, and then concatenating them (with the "+" operator) together into one big string for the document.write().
Copy linkTweet thisAlerts:
@steveRauthorMar 31.2004 — thank you.

I knew it had to be relatively simple. Now I understand what I'll have to do.
×

Success!

Help @steveR spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...