Solved

Not getting any values in my alert...

Posted on 2008-10-12
6
351 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 300 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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

838 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