Solved

Not getting any values in my alert...

Posted on 2008-10-12
6
323 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now