Use jQuery to add form fields and PHP Sessions to remember the fields were added
I am creating a multi-paged form and now i am consulting the Stack guru's for some assistance;
On this page the user can add more input fields [type=text] onto the page
So basically there is a singular input for "CHILD NAME" then a Add button to allow the user to add more children if they have more than one.
My form allows you to go backwards and forwards, the form remembers what was input, how would i get jQuery and php to remember that the form f开发者_运维问答ields were added so they are not hidden when the user re-vists the page ?
well with adding inputs you can do:
$('form').append('<input>',{type: 'text', name: 'input_name'})
and you can save previous parts of the form data with php $_SESSIONs
example of a form:
in php you will get all the post (or get) values:
$_POST = array ('name1' => 'me', 'name2' => 'you' )...
and then you can save those in a session with:
$_SESSION['lastpostedvalues'] = $_POST;
or something like that.
remember to have session_start()
at the top of the php file
try this if it helps
html code
<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']
}
** Make sure to format the html to fit your needs
http://jsfiddle.net/tsegay/5VhVp/
精彩评论