Solved

Not getting any values in my alert...

Posted on 2008-10-12
6
343 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:Tiggerito
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tutorial for ebay api 3 34
where is team.js? 3 25
alert(innerHTML); 8 16
I was able to prove the index of the even , how can i get the even index? 4 11
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 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