Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Not getting any values in my alert...

Posted on 2008-10-12
6
Medium Priority
?
376 Views
Last Modified: 2012-05-05
I'm trying to call a js function in an onclick event from within my php smarty template.  

$myarray = Array ([0] => A, [1] => B, [2] => C);
$json = json_encode($myarray);
$smarty->assign('myarray', $json);

<a href="javascript:void(0);" onclick="saveall (9, {$json})">Save All </a>

Right now, the js function merely consists of:

function saveall(id, thearray) {
  var arr = eval('(' + thearray + ')')
  alert(arr);
}

But, I'm not getting any alerts at all?
0
Comment
Question by:n00b0101
6 Comments
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 22699993
Hi there, do a view source from the browser and check the onclick code.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22700327
and using FF, look in the javascript console.
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22701764
If you have FF and are developing javascript,  I'd suggest you install the FireBug plugin.

http://getfirebug.com/

It will tell you if you have any bugs and you can even step through code.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:n00b0101
ID: 22702614
I have FF installed w/ Firebug, and no errors are appearing in the console. I did try to addslashes to escape the double quotes. View source, I see this:


<a href="javascript:void(0);" onclick="saveall(9, {\"item\":\"56753\",\"color\":\"Blue\",\"size\":\"XL\"});">Save</a>

Open in new window

0
 
LVL 17

Accepted Solution

by:
NicksonKoh earned 1200 total points
ID: 22707862
Hi,

Use single quote instead and it should solve your problem.

<a href="javascript:void(0);" onclick="saveall(9, {'item':'56753','color':'Blue','size':'XL'});">Save</a>
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 800 total points
ID: 22709127
NicksonKoh is correct. The HTML does not understand the double quote in the script and evaluates
<a href="javascript:void(0);" onclick="saveall(9, {\"item\":\"56753\",\"color\":\"Blue\",\"size\":\"XL\"});">Save</a>
 
as
 
<a href="javascript:void(0);" onclick="saveall(9, {\"
 
item\":\"56753\",\"color\":\"Blue\",\"size\":\"XL\"});">Save</a>
 
or 
 
<a href="javascript:void(0);" onclick="saveall(9, {"
 
item":"56753","color":"Blue","size":"XL"});">Save</a>
 
 
If your JSON parser cannot be forced to use single quotes, SWAP the quotes (and return false instead of the void(0) thingy):
 
 
<a href="#" onclick='saveall (9, {$json}); return false'>Save All </a>

Open in new window

0

Featured Post

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!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

876 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