473,775 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a Smarty array to javascript Function

33 New Member
I am trying to pass a smarty array to a javascript function

what is he easiest way to pass the values? I tried these code but not working. Please help me where iam wrong.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $url_names = array();
  4. $sql = "SELECT `page_url` FROM ".TABLE_USER_PAGES." ";
  5. $result = $uk->tep_mysql_query($sql);
  6. while($rec_url = mysql_fetch_row($result)){
  7.  $url_names[] = $rec_url[0];
  8. }
  9.  
  10. ?>
  11.  
Template file
Expand|Select|Wrap|Line Numbers
  1. {include file=page_head.tpl}
  2. {literal}
  3. <script language="javascript">
  4. var regex_alphanumeric = /^[a-zA-Z0-9\-\_\s]{2,}$/;
  5. function chk_url(url){
  6. //var m = new Array();
  7.  if (!regex_alphanumeric.test(document.create_form.page_name.value)) {
  8.     alert("Page Name seems to be invalid, please check it");
  9.     document.create_form.page_name.value=''; 
  10.     document.create_form.page_name.focus(); return false;
  11.   }else{
  12.          for(var i=0; i<url.length;i++){
  13.              if(url[i] == document.create_form.dispname.value) {
  14.                 alert("Page Name already exist, please try another");
  15.                 document.create_form.page_name.value=''; 
  16.                 document.create_form.page_name.focus(); return false;
  17.             }
  18.          }
  19.   }
  20.  
  21. }
  22. </script>
  23. {/literal}
  24.  
  25.  
  26.   <form name="create_form"  method="post" enctype="multipart/form-data">
  27.   <table width="100%">
  28. <tr><td><input name="page_name" type="text" class="textbox1" id="page_name"   value="{php}echo $_POST['page_name']; {/php}" size="60" maxlength="60">
  29.  
  30.       <input type="text" name="page_title" class="textbox1" size="78" value="{php}echo $_POST['page_title']; {/php}" onFocus="return chk_url('{$url_names}')"></td></tr></table>
  31.  

Thanks in advance
Nov 15 '07 #1
2 8067
Atli
5,058 Recognized Expert Expert
Hi Ruth.

Please use [code] tags when posting code.

[code=php] PHP code goes here. [/code]

Thank you.

Moderator
Nov 16 '07 #2
RuthC
33 New Member
Hi Atli,

Actually I am using the php code in seperate file. and pass the array to a smarty variable using

$smarty->assign('url_na mes' , $url_names);

Anyway I embeded the php code into the template file and modified little

Now its working fine. Thanks for your advice.

Expand|Select|Wrap|Line Numbers
  1. {php}
  2. $url_names = array();
  3. $sql = "SELECT `page_url` FROM ".TABLE_USER_PAGES." ";
  4. $result = mysql_query($sql);
  5. while($rec_url = mysql_fetch_row($result)){
  6.  $url_names[] = $rec_url[0];
  7. }
  8. echo "<script language='JavaScript'>\n";
  9. echo "var js_array = new Array();\n";
  10. foreach($url_names as $key => $value)
  11. {
  12.     echo "js_array[$key] = '$value';\n";
  13. }
  14. echo "</script>"
  15. {/php}
  16. {literal}
  17. <script language="javascript">
  18. function chk_url(){
  19. //var m = new Array();
  20.  if (!regex_alphanumeric.test(document.create_form.page_name.value)) {
  21.     alert("Page Name seems to be invalid, please check it");
  22.     document.create_form.page_name.value=''; 
  23.     document.create_form.page_name.focus(); return false;
  24.   }else{
  25.           //alert(js_array.length);
  26.          for(var i=0; i<js_array.length;i++){
  27.              if(js_array[i] == document.create_form.dispname.value) {
  28.                 alert("Page Name already exist, please try another");
  29.                 document.create_form.page_name.value=''; 
  30.                 document.create_form.page_name.focus(); return false;
  31.             }
  32.      } 
  33.   }
  34. }
  35. </script>
  36. {/literal}
  37.  
  38. <form name="create_form" method="post" enctype="multipart/form-data">
  39. <table width="100%">
  40. <tr><td><input name="page_name" type="text" class="textbox1" id="page_name" value="{php}echo $_POST['page_name']; {/php}" size="60" maxlength="60"></td></tr>
  41.  
  42. <tr><td><input type="text" name="page_title" class="textbox1" size="78" value="{php}echo $_POST['page_title']; {/php}" onFocus="return chk_url()"></td></tr>
  43. </table>
  44. </form>
  45.  
Thanks.
Nov 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2314
by: Richard | last post by:
I need help. I have a smarty based program that I am modifying. I have a smarty template file that consists of smarty and HTML. I need to integrate some PHP database calls into it. My problem is I cannot figure out how to pass value between smarty and PHP. Going from php to smarty I've tried this: {php} $smarty = new Smarty; $smarty->assign("x", 100);
1
2308
by: Jim Red | last post by:
Hello I have a Smarty(.php.net) Question. I want to create a Navigation with a Smarty Template. How can I display an Array recursively? Do I have to write a Plugin?
5
6969
by: glin | last post by:
Hi there, does anyone know how to use smarty to generate a selection list without PHP assigning a array? eg. a list for selection minutes, but without php to assign a 1~60 array into smarty. Thanks in advance.
12
6558
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the courses to pass the correct option value and then be displayed at the following URL: http://www.dslextreme.com/users/kevinlyons/selectResults.html I am passing countries, products, and courses. The first two display
3
14951
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
4959
by: Freebird | last post by:
Hello you all, =] I've a smarty var, like this: {$var} that is equal to: ttttttttttttt|zzzzzzzzzzzz It can be any size, the important is that they are separated by the '|' How do I 'explode' that into an array or 2 variables in the smarty template
6
5581
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an object collection and stanrd using it it starts to fall apart. Clearly there is something about javascript's usage of passing "By ref" that i am not getting. i have had a look on the web and found some examples, but i cant see why my code does not...
2
3230
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_content" is a...
1
3115
by: RuthC | last post by:
I am trying to pass a smarty array to a javascript function what is he easiest way to pass the values? I tried these code but not working. Please help me where i am wrong. <?php $url_names = array(); $sql = "SELECT `page_url` FROM ".TABLE_USER_PAGES." "; $result = $uk->tep_mysql_query($sql); while($rec_url = mysql_fetch_row($result)){
2
2110
dlite922
by: dlite922 | last post by:
difficult to explain, easier to show: Here's what my data looks like on the PHP side: Array ( => Array ( => 20003001 => ABC RESTAURANT
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10107
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.