/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Issue with server-side form validation – alert client-side

Hello, everyone.

[B]Before anyone replies with “that doesn’t belong in this forum”, please read the whole post, carefully. I’m pretty sure it does belong here.[/B]

I’m working on a form that will be validated server-side in PHP5, but I’m trying to set it up so that if anything needs correcting it will re-direct back to the form page and display a JavaScript alert dialog. When the re-direct occurrs, I am getting a JavaScript “unterminated string constant” error.

Here is some of my code.

On the form page (which is PHP), I am param’ing $_SESSION variables for each field and setting most of them to blank, and then setting the value for each field to its respective $_SESSION variable. On this page, at the top, I also have the following:

[CODE]<script language=”JavaScript1.5″ type=”text/javascript”>
<?php if(isset($_SESSION[‘notNice’]) && !empty($_SESSION[‘notNice’])) { ?>
alert(“<?php echo $_SESSION[‘notNice’]; ?>”);
<?php } ?>
</script>[/CODE]

Now on the page that handles the form processing, I am first setting all field values to their respective $_SESSION variable, then doing validation like so:

[code=php]
$_SESSION[‘notNice’] = “”;
if($_POST[‘firstName’] == ”) {
$_SESSION[‘notNice’] .= “Please enter your First Name.n”;
}
else {// Check to make sure of a certain format (ie, no numbers, etc.)

}
if($_POST[‘lastName’] == ”) {
$_SESSION[‘notNice’] .= “Please enter your Last Name.n”;
}
else {// Check to make sure of a certain format (ie, no numbers, etc.)

}[/code]

Does PHP treat “n” as a line break, like JavaScript does? Could this be what is causing the unterminated string constant? Or am I missing something else?

Thanks,

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeauthorJan 04.2012 — I guess it does. I played around with it, a bit, and changed all the n to <br>. Then in the JavaScript code, I saved the $_SESSION to a JS variable, then set the JS variable to equal itself[COLOR="DarkOrange"].replace(/<br>/g,"n") [/COLOR]and alerted the JS variable. Works flawlessly.

Thanks,
×

Success!

Help @WolfShade 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.17,
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,
)...