2011-03-14 35 views
3

我正在创建一个多页面表单,现在我正在咨询Stack guru的一些帮助;使用jQuery添加表单域和PHP会话来记住添加的字段

在这个页面上,用户可以添加更多的输入字段[类型=文本]到页面上

所以基本上是“子名称”,则添加按钮的单一输入,允许用户添加更多的孩子如果他们有多个。

我的表单允许你前后移动,表单会记住输入的内容,我如何获得jQuery和php来记住表单域被添加了,所以当用户重新浏览页面时它们不会被隐藏?

回答

2
与添加输入

那么你可以这样做:

$('form').append('<input>',{type: 'text', name: 'input_name'}) 

,你可以保存表单数据之前的部分用php $ _SESSIONs形式的

例如:在PHP你会

获取所有后(或获取)值:

$_POST = array ('name1' => 'me', 'name2' => 'you')... 

然后你可以保存那些与会议:

$_SESSION['lastpostedvalues'] = $_POST; 

或类似的东西。

记得有session_start()在PHP文件

+0

我明白了,你能给出一个从jquery生成的输入的例子以及它如何存储在Session中吗?编辑 – Xavier 2011-03-14 23:31:44

+0

以添加如何将帖子值添加到会话中。 – Neal 2011-03-14 23:34:29

+0

jquery代码不包含表单名称或类型变量 – Xavier 2011-03-14 23:47:48

0

的顶部试试这个,如果有帮助

HTML代码

<span> 

    <span id="childs"></span> 

    <input type="text" id="testid" /> 

    <input type="submit" id="submit" value="Add" /> 
</span> 

的jQuery ---

$(document).ready(function(){ 
$('#submit').click(function(){ 
    $('#childs').append('<a href="">'+$('#testid').val()+'</a><br />'); 
    $value=$('#testid').val(); 
    $.get('somePHPCodewhichaddsToSession.php',{ 
     data:$values 
    },function(data){ 
     //some staff after the session is add 
    }); 

}); 
}); 
/*PHP code to add the sessions*/ 


if(isset($_GET['data'])){ 
    $_SESSION['AnyName']=$_GET['data']; //this could be an array to hold all the childern names 
    if(isset($_SESSION['AnyName'])) 
     echo true; //handle this return in your ajax call oncomplete callback 
    else 
     echo false; 
} 

/* PHP code to dispaly the childrens*/ 

//get the array from the session 

if(isset($_SESSION['AnyName']){ 
    get the values and display or do something with them 
     $values= $_SESSION['AnyName'] 
    } 

**确保格式化html以符合您的需求

http://jsfiddle.net/tsegay/5VhVp/