Passing a Smarty array to javascript Function

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

    #1

    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 i am wrong.

    <?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];
    }
    $smarty->assign('url_na mes' , $url_names);

    ?>

    Template file
    --------------

    {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"> </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('{$url_ names}')"></td></tr>
    </table>
    </form>




    Thanks in advance
  • Dasty
    Recognized Expert New Member
    • Nov 2007
    • 101

    #2
    your html page is looking like this right now:

    [HTML]onFocus="return chk_url('http://sample1.com,htt p://sample2.com,htt p://sample3.com')"
    [/HTML]

    Right? (look at the page source code to see) It is not converted into array by js. (as you are expecting in the function), you have to adjust your php code to generate output like this:

    [HTML]onFocus="return chk_url(['http://sample1.com','h ttp://sample2.com','h ttp://sample3.com'])"
    [/HTML]

    Comment

    Working...