By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,710 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,710 IT Pros & Developers. It's quick & easy.

Passing a Smarty array to javascript Function

P: 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 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)){
$url_names[] = $rec_url[0];
}
$smarty->assign('url_names' , $url_names);

?>

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

{include file=page_head.tpl}
{literal}
<script language="javascript">
var regex_alphanumeric = /^[a-zA-Z0-9\-\_\s]{2,}$/;
function chk_url(url){
//var m = new Array();
if (!regex_alphanumeric.test(document.create_form.pag e_name.value)) {
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_form" method="post" enctype="multipart/form-data">
<table width="100%">
<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>

<tr><td><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>
</form>




Thanks in advance
Nov 15 '07 #1
Share this Question
Share on Google+
1 Reply


Dasty
Expert 100+
P: 101
your html page is looking like this right now:

[HTML]onFocus="return chk_url('http://sample1.com,http://sample2.com,http://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','http://sample2.com','http://sample3.com'])"
[/HTML]
Nov 15 '07 #2

Post your reply

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