Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP call to javascript localStorage.setItem special Chars fails

Posted on 2014-04-03
10
Medium Priority
?
1,106 Views
Last Modified: 2014-04-13
The code below is having a problem with the special characters. Does anybody know why?

<script type="text/javascript">
function enter_user_email(str){
   localStorage.setItem('user_email', str);
}
</script>

<?php
$usr_email= "aaaaaa@bbbbb.xx";
echo "<script type=\"text/javascript\">enter_user_email(\"$usr_email\");</script>";
?>
0
Comment
Question by:Solenthaler
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39974645
What special characters?  Is there some confusion about UTF-8 encoding, maybe?
0
 

Author Comment

by:Solenthaler
ID: 39974731
If $usr_email= "aaaaaa@bbbbbxx"; or  "aaaaaabbbbb.xx"; the call to the function succeeds.
However if $usr_email = "aaaaaa@bbbbb.xx"; :
 -> the PHP call to the javascript function is failing and it echoes the following three chars: "); (the 3 last chars from to call code) I think, because of the special chars in $usr_email - the call fails. If i delete the "." and/or the "@" the call works perfect.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 900 total points
ID: 39974802
I think there must be something else in play.  I cannot duplicate the failure.  
http://iconoun.com/demo/temp_solenthaler.php

Install this and run it to see what I'm seeing.

<?php // demo/temp_solenthaler.php
error_reporting(E_ALL);

/*
 * THE PHP VARIABLES FOR OUR SCRIPT
 */
$usr_email= "aaaaaa@bbbbb.xx";

/*
 * THE JAVASCRIPT SNIPPETS
 */
$js1 = <<<EOD
<script type="text/javascript">
function enter_user_email(str){
    localStorage.setItem('user_email', str);
}
</script>
EOD;

$js2 = <<<EOD
<script type="text/javascript">
$(document).ready(function(){
    $("#zap").click(function(){
        foo = localStorage.getItem('user_email');
        alert('user_email: ' + foo);
    });
});
</script>
EOD;

$js3 = <<<EOD
<script type="text/javascript">enter_user_email("$usr_email");</script>
EOD;


/*
 * CREATE OUR WEB PAGE IN HTML5 FORMAT
 */
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>HTML5 Page in UTF-8 Encoding</title>

$js1
$js2

</head>
<body>

$js3

<div id="zap">Click Here To See LocalStorage user_email</div>

</body>
</html>
HTML5;

/*
 * RENDER THE WEB PAGE
 */
echo $htm;

Open in new window

0
Independent Software Vendors: 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:Solenthaler
ID: 39975675
Hi Ray
incredible - thanks for this! I will build up a Demo.
Some environment in my example:
 -> the code is in a self made module for Joomla 2.5 and will use Joomlas user management. Will come back to it on sunday.
Cheers Roland
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39975948
The other thing is that one can't assume that localStorage is available. e.g. it's been known for it to fail on Safari when private browsing is enabled. Maybe test first:

function isLocalStorageSupported() {
    var result = true;
    try {
	localStorage.setItem("foo", "bar");
	result = (localStorage.getItem("foo") == "bar");
    } catch (error) {
	result = false;
    }
    return result;
}

Open in new window

0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 300 total points
ID: 39976262
greetings Solenthaler, Writing from PHP into Javascript with echo , requires More care than writing to page HTML output, For me I always use the OPPOSITE quotes (single ' -double ") when going from PHP to JS, you might try -

$usr_email= "aaaaaa@bbbbb.xx"; // if " here
//use a single ' in JS -
echo '<script>enter_user_email("'.$usr_email.'");</script>';
// be careful, look at source code to see the actual result in JS


= = = = = = = = = = = = = = = =
for many years (forever just about) the type in
<script type="text/javascript">
   has been UNNECESSARY , so leave it out, use,  <script>
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39976669
@CEHJ: I think that's a good idea - to test for support.  Trouble is, I don't know how to "degrade gracefully" if localStorage is not there.  Maybe set a cookie?  Or create a nag-page?  Not sure of the best practices in that case.

@Slick812: The example script parses correctly, but you're right that the quote marks can be confusing.  That's why I favor the use of HEREDOC notation (as shown above).  The quotes and apostrophes in a HEREDOC block have no meaning to PHP so you can concentrate on getting the script right for JavaScript, only.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 39976706
Maybe set a cookie?
Yes, that's a possibility although that too will need to be tested for. It just so happens i've been having problems with both recently (though the bar is higher for me - the web pages are offline)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39976741
Kickstarter.com uses a nag-page if you don't accept their cookie, and it seems to work in a way that makes sense.  But sometimes I feel like clients who don't return cookies or run javaScript are so far out on the edge that we can ignore them without any measurable business impact.  Given that you can't use FB, Twitter, etc., without that functionality it seems like the number of people who disable these things is pretty small.  But I've not seen a survey recently.
0
 

Author Closing Comment

by:Solenthaler
ID: 39997323
Hi experts
finally i made it (with your help - of course). Surprisingly a Joomla Plugin "e-mail cloake" caused the problem. The e-mail string has on the flight been replaced by the encrypted string.  {emailcloak=off} did the trick. Thanks a lot.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

610 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