473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing a Smarty array to javascript Function

33
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 8050
Atli
5,058 Expert 4TB
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
Hi Atli,

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

$smarty->assign('url_names' , $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
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...
1
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
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. ...
12
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...
3
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) {...
2
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...
6
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...
2
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...
1
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 =...
2
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.