Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

escaping java with combined php

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
maz
Smarty n00b


Joined: 07 May 2006
Posts: 2

PostPosted: Sun May 07, 2006 12:25 pm    Post subject: escaping java with combined php Reply with quote

Hi
I am trying i to incorporate a 'mod' into a 'Main' php script program. The 'Main Script' uses smarty. The 'mod' does not. (The mod I purchased, and the guy who was supposed to give support has disappeared),

The purpose of the 'mod' was to add a 'drop down' menu to the 'Main Script' page. As an item is selected from the initial dropdown, a second dropdown menu appears populated with reigions from the database. Selecting from the second dropdown then populates a third dropdown and so on.

I have a main.php file and a main.tpl file . Unfortunately I do not know where to start. Which parts of the mod do I add to the main.php file and which parts to the main.tpl file.

So I decided to add the whole mod to the main.tpl file.

The mod code (shown below) includes both php and java.

My main problem is this:

The Java part is not continuous. It has php tags <PHP? ?> there too. If I use the {literal}{/literal} to encompass the WHOLE script, I get mysql errors contained in the php parts. If I then add {php}{/php} around each individual php element within the java code, the errors go away, the dropdowns do appear, but I only have 'empty' dropdowns'

I am not sure what to do.


The original code for the mod is as follows.

Code:
 

<script>
var RegJSMenu = 1;
function ClearList(list){
    var i = 0;
    var o = list.options;
    for (i = o.length; i >= 0; --i) o[i] = null;
    list.disabled = true;
}

function addElement(list, text_in, value_in){
    var o = list.options;
    var nIdx;
        if (o.length < 0) nIdx = 0;
        else nIdx = o.length;
        o[nIdx] = new Option(text_in, value_in);
        list.disabled = false;
}

function RenderTR(s, show){
  var isTR = false;
  show = show ? "block" : "none";
  if(isTR){
    var obj = document.getElementsByTagName("TR");
    for (i=0; i<obj.length; i++) {
      if (obj[i].id == s) obj[i].style.display = show;
    }
  }
  else{
    if(document.getElementById) el = document.getElementById(s);
    else { el = document.all ? document.all[s] : new Object(); }
    if(!el) return;
    if(el.style) el = el.style;
    el.display = show;
  }
}

function JsReg_Reload(num, rid){

var aCat = new Array();

<?PHP
function show_region_list_4_select($region_fatherid,$add_space)
{
  global $region_tbl;
  $sql_tree = "SELECT * FROM $region_tbl where region_fatherid='$region_fatherid' order by region_seq";
  $res_tree = mysql_query($sql_tree);
  $num_tree=0;
  $num_tree =  @mysql_num_rows($res_tree);
  $region_array=array();
  for ($i=0; $i<$num_tree; $i++)
  {
         $row_tree=mysql_fetch_array($res_tree);
         $region_id=$row_tree["region_id"];
         $region_array[]=$region_id;
         if ($add_space<2) {show_region_list_4_select($region_id,$add_space+1);}
  }
  if ($num_tree>0)
  {
      $region_values=implode(",",$region_array);
      echo "aCat[$region_fatherid]=new Array($region_values);\n";
  }
}
show_region_list_4_select(0,0);
?>

var aName = {0:'',
<?PHP
  $sql_tree = "SELECT * FROM $region_tbl order by region_name";
  $res_tree = mysql_query($sql_tree);
  $num_tree =  mysql_num_rows($res_tree);
  for ($i=0; $i<$num_tree; $i++)
   {
         $row_tree=mysql_fetch_array($res_tree);
         $region_id_1=$row_tree["region_id"];
         $region_name_1=$row_tree["region_name"];
         //if ($region_available) {$region_name_1=$row_tree["$lang_reg"]." *";} else {$region_name_1=$row_tree["$lang_reg"];}
         if ($i!=$num_tree-1) {echo "$region_id_1:'$region_name_1',\n";} else {echo "$region_id_1:'$region_name_1'};\n";}
   }
?>
  var ctl;
  var i = num;
  while(1){
          i++;
    ctl = eval("document.main_form.region_" + i);
    if(!ctl) break;
    ClearList(ctl);
    RenderTR("area_" + i, 0);
  }
  if(!aCat[rid]) return;
  ctl = eval("document.main_form.region_" + (num + 1));
  var id;
  addElement(ctl, "- Please Select -", "");
  for (i = 0; i < aCat[rid].length; i++){
          id = aCat[rid][i];
    addElement(ctl, aName[id], id);
  }
  RenderTR("area_" + (num + 1), 1);
}


function region_Reload(num){

  var ctl_reg = eval('document.main_form.region_' + num);
  var f = document.main_form;
  var rid = ctl_reg.options[ctl_reg.selectedIndex].value;

  if((num==1 && rid=="") && !RegJSMenu) return;
  if(!rid && num>1){
    num--;
    ctl_reg = eval('document.main_form.region_' + num);
    rid = ctl_reg.options[ctl_reg.selectedIndex].value;
  }

  if(RegJSMenu){
    JsReg_Reload(num, rid);
    return;
  }

}

</script>

<tr><td valign='top'>Location:</td><td>

<table border=0 width="100%" cellpadding=0 cellspacing=1 id="area_1" >
<tr>
<td width="80%"><select name='region_1' OnChange="region_Reload(1)">
<option value="0">- Please Select -</option>
<?
  $sql_tree = "SELECT * FROM $region_tbl where region_fatherid='0' order by region_seq";
  $res_tree = mysql_query($sql_tree);
  $num_tree =  mysql_num_rows($res_tree);
  for ($i=0; $i<$num_tree; $i++)
   {
         $row_tree=mysql_fetch_array($res_tree);
         $region_id_1=$row_tree["region_id"];
         $region_name_1=$row_tree["region_name"];
         if ($region_id_1==$region_id) {echo "<option value='$region_id_1' selected>";} else {echo "<option value='$region_id_1'>";}
         echo $region_name_1."</option>";
   }
?>
</select><br><?
        $result_region = mysql_query ("select * from $ads_tbl,$region_tbl where ad_region=region_id AND siteid = '$siteid'");
        $row_region = @mysql_fetch_array($result_region);
        $region_name = $row_region["region_name"];
if (!empty($region_name)) echo "Previous region: <b>$region_name</b> (if you don't select new region it would not be changed.)";
?>
</td>
</tr>
</table>


<table border=0 width="100%" cellpadding=0 cellspacing=1 id="area_2" style="display:none">
<tr>
<td width="80%"><select name='region_2' OnChange="region_Reload(2)">
<option value="0">- Please Select -</option>
</select>
</td>
</tr>
</table>


<table border=0 width="100%" cellpadding=0 cellspacing=1 id="area_3" style="display:none">
<tr>
<td width="80%">
<select name='region_3' OnChange="region_Reload(3)">
<option value="0">- Please Select -</option>
</select>
</td>
</tr>
</table>

</td></tr>

<?
// \REGIONS MODULE
?>



These are my changes I have simply added the {literal}{/literal} and {php}{/php} tags as shown.
Code:


{literal}
<script>
var RegJSMenu = 1;
function ClearList(list){
    var i = 0;
    var o = list.options;
    for (i = o.length; i >= 0; --i) o[i] = null;
    list.disabled = true;
}

function addElement(list, text_in, value_in){
    var o = list.options;
    var nIdx;
        if (o.length < 0) nIdx = 0;
        else nIdx = o.length;
        o[nIdx] = new Option(text_in, value_in);
        list.disabled = false;
}

function RenderTR(s, show){
  var isTR = false;
  show = show ? "block" : "none";
  if(isTR){
    var obj = document.getElementsByTagName("TR");
    for (i=0; i<obj.length; i++) {
      if (obj[i].id == s) obj[i].style.display = show;
    }
  }
  else{
    if(document.getElementById) el = document.getElementById(s);
    else { el = document.all ? document.all[s] : new Object(); }
    if(!el) return;
    if(el.style) el = el.style;
    el.display = show;
  }
}

function JsReg_Reload(num, rid){

var aCat = new Array();

{php}
function show_region_list_4_select($region_fatherid,$add_space)
{
  global $region_tbl;
  $sql_tree = "SELECT * FROM $region_tbl where region_fatherid='$region_fatherid' order by region_seq";
  $res_tree = mysql_query($sql_tree);
  $num_tree=0;
  $num_tree =  @mysql_num_rows($res_tree);
  $region_array=array();
  for ($i=0; $i<$num_tree; $i++)
  {
         $row_tree=mysql_fetch_array($res_tree);
         $region_id=$row_tree["region_id"];
         $region_array[]=$region_id;
         if ($add_space<2) {show_region_list_4_select($region_id,$add_space+1);}
  }
  if ($num_tree>0)
  {
      $region_values=implode(",",$region_array);
      echo "aCat[$region_fatherid]=new Array($region_values);\n";
  }
}
show_region_list_4_select(0,0);
{/php}

var aName = {0:'',
{php}
  $sql_tree = "SELECT * FROM $region_tbl order by region_name";
  $res_tree = mysql_query($sql_tree);
  $num_tree =  mysql_num_rows($res_tree);
  for ($i=0; $i<$num_tree; $i++)
   {
         $row_tree=mysql_fetch_array($res_tree);
         $region_id_1=$row_tree["region_id"];
         $region_name_1=$row_tree["region_name"];
         //if ($region_available) {$region_name_1=$row_tree["$lang_reg"]." *";} else {$region_name_1=$row_tree["$lang_reg"];}
         if ($i!=$num_tree-1) {echo "$region_id_1:'$region_name_1',\n";} else {echo "$region_id_1:'$region_name_1'};\n";}
   }
{/php}
  var ctl;
  var i = num;
  while(1){
          i++;
    ctl = eval("document.main_form.region_" + i);
    if(!ctl) break;
    ClearList(ctl);
    RenderTR("area_" + i, 0);
  }
  if(!aCat[rid]) return;
  ctl = eval("document.main_form.region_" + (num + 1));
  var id;
  addElement(ctl, "- Please Select -", "");
  for (i = 0; i < aCat[rid].length; i++){
          id = aCat[rid][i];
    addElement(ctl, aName[id], id);
  }
  RenderTR("area_" + (num + 1), 1);
}


function region_Reload(num){

  var ctl_reg = eval('document.main_form.region_' + num);
  var f = document.main_form;
  var rid = ctl_reg.options[ctl_reg.selectedIndex].value;

  if((num==1 && rid=="") && !RegJSMenu) return;
  if(!rid && num>1){
    num--;
    ctl_reg = eval('document.main_form.region_' + num);
    rid = ctl_reg.options[ctl_reg.selectedIndex].value;
  }

  if(RegJSMenu){
    JsReg_Reload(num, rid);
    return;
  }

}

</script>

{/literal}

<tr><td valign='top'>Location:</td><td>

<table border=0 width="100%" cellpadding=0 cellspacing=1 id="area_1" >
<tr>
<td width="80%"><select name='region_1' OnChange="region_Reload(1)">
<option value="0">- Please Select -</option>
{php}
  $sql_tree = "SELECT * FROM $region_tbl where region_fatherid='0' order by region_seq";
  $res_tree = mysql_query($sql_tree);
  $num_tree =  mysql_num_rows($res_tree);
  for ($i=0; $i<$num_tree; $i++)
   {
         $row_tree=mysql_fetch_array($res_tree);
         $region_id_1=$row_tree["region_id"];
         $region_name_1=$row_tree["region_name"];
         if ($region_id_1==$region_id) {echo "<option value='$region_id_1' selected>";} else {echo "<option value='$region_id_1'>";}
         echo $region_name_1."</option>";
   }
{/php}
</select><br><?
        $result_region = mysql_query ("select * from $ads_tbl,$region_tbl where ad_region=region_id AND siteid = '$siteid'");
        $row_region = @mysql_fetch_array($result_region);
        $region_name = $row_region["region_name"];
if (!empty($region_name)) echo "Previous region: <b>$region_name</b> (if you don't select new region it would not be changed.)";
{/php}
</td>
</tr>
</table>


<table border=0 width="100%" cellpadding=0 cellspacing=1 id="area_2" style="display:none">
<tr>
<td width="80%"><select name='region_2' OnChange="region_Reload(2)">
<option value="0">- Please Select -</option>
</select>
</td>
</tr>
</table>


<table border=0 width="100%" cellpadding=0 cellspacing=1 id="area_3" style="display:none">
<tr>
<td width="80%">
<select name='region_3' OnChange="region_Reload(3)">
<option value="0">- Please Select -</option>
</select>
</td>
</tr>
</table>

</td></tr>

{php}
// \REGIONS MODULE
{/php}


Not sure if the tags are correct. I would appreciate some help on this.

I know the ideal would be to put the complete java script and the php elements in the main.php file. But then I dont know how to include the options for dropdowns in the tpl file.

Any help would be appreciated. (I can pay for any support too).

Many thanks
Back to top
View user's profile Send private message
shannera
Administrator


Joined: 13 Feb 2006
Posts: 802
Location: Edertal, Germany

PostPosted: Mon May 08, 2006 10:13 am    Post subject: Reply with quote

Without looking at every single line of code: by placing the entire php logic in the template you are rendering the template idea useless. So you have to refactor your sourcecode completely: everything that is HTML or Javascript (enclosed with {literal}{/literal} tags) is done in the template, all the database queries are in the normal PHP file that calls the script, the information obtained from the db queries are passed to smarty. If you don't want to redo your work, you will have to drop smarty usage, as it seems you are not really needing it.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP