/    Sign up×
Community /Pin to ProfileBookmark

Passing Javascript varible to a PHP script

I am having problem tranferring a JavaScript varible to a PHP script. I am using the URL to transfer the varible:

preview.php?story=this%20is%20the%20text

It works fine for most characters except for the return character when using Internet Explorer for Mac. In IE, the return characters are removed. It works fine in Safari.

Like this forum, I replace tags with HTML code before it’s displayed. This is done using PHP in the preview.php script.

Does anyone have any suggections to precess the varible contents so it can survive the transfer to the PHP in all browsers.

The scripts for the two files are below:

<head>
….
<script language=”JavaScript” type=”text/JavaScript”>
<!–
var story;
function PreviewWin(story) {
story = story.replace(/&/g, ‘%26’);
story = story.replace(/ /g, ‘%20’);
window.open(‘preview.php?story=’ + story, ‘Preview’, ‘width=550,height=400,toolbar=no,menubar=no,status=no’);
}
//–>
</script></head>
….
<form name=”Create” id=”Create” method=”post” action=”newsletter.php”>
<textarea name=”story” cols=”75″ rows=”10″ id=”story”></textarea><br>
<input type=”button” name=”Preview” value=”Preview” onClick=”PreviewWin(document.Create.story.value);”>
</form>

The [b]preview.php[/b] file:

[code=php]<?php
// replace tags to create [b]bold[/b] etc.
echo nl2br(htmlspecialchars(stripslashes($story)));
?>[/code]

Thanks

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@oleragJan 31.2004 — IE seems to use both the line feed and carridge return (rn)

while other browsers only need a carridge return (n).

You can play around with something like the RegExp below

and see if this helps you achieve what your looking for.

str.replace(/[n]+/g, 'rn');
Copy linkTweet thisAlerts:
@theqauthorJan 31.2004 — [i]Originally posted by olerag[/i]

[B]str.replace(/[n]+/g, 'rn'); [/B]
[/QUOTE]


Thanks olerag.

After some experimentation, I found that this works:

story = story.replace(/[r]/g, '%0A');

IE for Mac must use the return instead of the linefeed character in TEXTAREA fields.

The new line replaces all the return chars with linefeeds.

I have tested it in Internet Explorer 5.2.3 for Mac OS X, Safari 1.1.1 and Netscape Communicator 4.8 for Mac and it works in all three.
Copy linkTweet thisAlerts:
@theqauthorJan 31.2004 — As a postscript, adding the following line will make it compatible with Camino 0.7.0:

story = story.replace(/[n]/g, '%0A');

So the finished script is:

function PreviewWin(story) {

story = story.replace(/&/g, '%26');

story = story.replace(/ /g, '%20');

story = story.replace(/[r]/g, '%0A');

story = story.replace(/[n]/g, '%0A');

window.open('preview.php?story=' + story, 'Preview', 'width=550,height=400,toolbar=no,menubar=no,status=no');

}
×

Success!

Help @theq 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...