Passing a Smarty array to javascript Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RuthC
    New Member
    • Nov 2007
    • 33

    Passing a Smarty array to javascript Function

    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.
    [code=php]
    <?php

    $url_names = array();
    $sql = "SELECT `page_url` FROM ".TABLE_USER_PA GES." ";
    $result = $uk->tep_mysql_quer y($sql);
    while($rec_url = mysql_fetch_row ($result)){
    $url_names[] = $rec_url[0];
    }

    ?>
    [/code]
    Template file
    [code=php]
    {include file=page_head. tpl}
    {literal}
    <script language="javas cript">
    var regex_alphanume ric = /^[a-zA-Z0-9\-\_\s]{2,}$/;
    function chk_url(url){
    //var m = new Array();
    if (!regex_alphanu meric.test(docu ment.create_for m.page_name.val ue)) {
    alert("Page Name seems to be invalid, please check it");
    document.create _form.page_name .value='';
    document.create _form.page_name .focus(); return false;
    }else{
    for(var i=0; i<url.length;i+ +){
    if(url[i] == document.create _form.dispname. value) {
    alert("Page Name already exist, please try another");
    document.create _form.page_name .value='';
    document.create _form.page_name .focus(); return false;
    }
    }
    }

    }
    </script>
    {/literal}


    <form name="create_fo rm" method="post" enctype="multip art/form-data">
    <table width="100%">
    <tr><td><inpu t name="page_name " type="text" class="textbox1 " id="page_name" value="{php}ech o $_POST['page_name']; {/php}" size="60" maxlength="60">

    <input type="text" name="page_titl e" class="textbox1 " size="78" value="{php}ech o $_POST['page_title']; {/php}" onFocus="return chk_url('{$url_ names}')"></td></tr></table>
    [/code]


    Thanks in advance
    Last edited by Atli; Nov 16 '07, 12:18 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi Ruth.

    Please use &#91;code] tags when posting code.

    &#91;code=ph p] PHP code goes here. [/code]

    Thank you.

    Moderator

    Comment

    • RuthC
      New Member
      • Nov 2007
      • 33

      #3
      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.

      [CODE=php]
      {php}
      $url_names = array();
      $sql = "SELECT `page_url` FROM ".TABLE_USER_PA GES." ";
      $result = mysql_query($sq l);
      while($rec_url = mysql_fetch_row ($result)){
      $url_names[] = $rec_url[0];
      }
      echo "<script language='JavaS cript'>\n";
      echo "var js_array = new Array();\n";
      foreach($url_na mes as $key => $value)
      {
      echo "js_array[$key] = '$value';\n";
      }
      echo "</script>"
      {/php}
      {literal}
      <script language="javas cript">
      function chk_url(){
      //var m = new Array();
      if (!regex_alphanu meric.test(docu ment.create_for m.page_name.val ue)) {
      alert("Page Name seems to be invalid, please check it");
      document.create _form.page_name .value='';
      document.create _form.page_name .focus(); return false;
      }else{
      //alert(js_array. length);
      for(var i=0; i<js_array.leng th;i++){
      if(js_array[i] == document.create _form.dispname. value) {
      alert("Page Name already exist, please try another");
      document.create _form.page_name .value='';
      document.create _form.page_name .focus(); return false;
      }
      }
      }
      }
      </script>
      {/literal}

      <form name="create_fo rm" method="post" enctype="multip art/form-data">
      <table width="100%">
      <tr><td><inpu t name="page_name " type="text" class="textbox1 " id="page_name" value="{php}ech o $_POST['page_name']; {/php}" size="60" maxlength="60"> </td></tr>

      <tr><td><inpu t type="text" name="page_titl e" class="textbox1 " size="78" value="{php}ech o $_POST['page_title']; {/php}" onFocus="return chk_url()"></td></tr>
      </table>
      </form>
      [/CODE]

      Thanks.

      Comment

      Working...