我如何将值从javascript .tpl页面传递给smarty中的普通php页面

时间:2012-07-04 07:51:39

标签: php ajax smarty

header.tpl(within /templetes)

---------------------------------------------
<HTML>
<HEAD>
<TITLE>{$title}</TITLE>
{literal}
<style type="text/css">
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#333333;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; }
</style>


  <script type="text/javascript" language="javascript">
  $(document).ready(function(){
    $("#commentForm").validate();
                                 });

  function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!

 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         //alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var cname = document.getElementById('cname').value;
 var lname = document.getElementById('lname').value;
 var date = document.getElementById('date').value;

 var queryString = "?cname=" + cname ;
 queryString +=  "&lname=" + lname + "&date=" + date;

ajaxRequest.onreadystatechange=stateChanged;
ajaxRequest.open("POST","demo/process.php?cname="+cname+"&lname="+lname+"&date="+date,true);
ajaxRequest.send(null); 

}


  </script>

{/literal}
</HEAD>
<BODY >

index.tpl如下

{include file="header.tpl" title= $title }
<h1>Login Form</h1>
<form class="cmxform" id="commentForm" >
 <fieldset>

   <p>
     <label for="cname"> First Name</label>
     <em>*</em><input id="cname" name="cname" size="25" class="required" minlength="2" />
   </p>
   <p>
     <label for="lname">Last Name</label>
     <em>*</em><input id="lname" name="lname" size="25"  class="required" />
   </p>
   <p>
     <label for="Date">Date</label>
     <em>*</em><input id="date" name="date" size="25"  class="required" value="" />
   </p>

   <p>
   <input class="submit" type="submit" value="Submit" class="button" onclick='ajaxFunction()' />
   </p>
 </fieldset>
 </form>

现在我想在process.php中获取cname,lname和date。不幸的是,我没有得到它我尝试了很多

当我在index.php中求值时,我没有得到任何值,请指导我..........

1 个答案:

答案 0 :(得分:0)

您是否尝试使用$smarty->get_template_vars()(Smarty2)或$smarty->getTemplateVars()(Smarty3)抓取值?