Solved

Not getting any values in my alert...

Posted on 2008-10-12
6
365 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
[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
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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.

705 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