Jump to content
Sign in to follow this  
Panta

How do i retain dynamic added fileds after submit

Recommended Posts

Good day, I have a code that adds field dynamically, but my problem is after submitting the form , if there are errors that restricted the form from submitting which are written in php, the dynamic added fields are lost. I wish to retain them and their values even when error occur, is there a way to check error with php and also retain the dynamic added field. thanks

<script>/*This script is identical to the above JavaScript function.*/var ct = 1;function new_link(){	if(ct!==3){	ct++;	var div1 = document.createElement('div');	div1.id = ct;	// link to delete extended form elements	var delLink = '<div style="text-align:right;margin-right:65px"><a href="javascript:delIt('+ ct +')">Del</a></div>';	div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;	document.getElementById('newlink').appendChild(div1);}}// function to delete the newly added set of elementsfunction delIt(eleId){	d = document;	var ele = d.getElementById(eleId);	var parentEle = d.getElementById('newlink');	parentEle.removeChild(ele);}</script><style>   #newlink {width:600px}</style><form method="post" action="" name="go"><input name="so" value="yes"  type="hidden"/><div id="newlink"><div>		<input type="text" name="linkurl[]" value="http://www.satya-weblog.com"> 	 Link Description:  <textarea name="linkdesc[]" cols="50" rows="5" ></textarea> </div></div>	<p>		<br>		<input type="submit" name="submit1">		<input type="reset" name="reset1">	</p><p id="addnew">	<a href="javascript:new_link()">Add New </a></p></form><!-- Template --><div id="newlinktpl" style="display:none"><div><table border=0>	<tr>		<td> Link URL: </td>		<td> <input type="text" name="<?php $name='linkurl[]';		echo $name		;?>" value=""> </td>	</tr>	<tr>		<td> Link Description: </td>		<td> <textarea name="linkdesc[]" cols="50" rows="5" ></textarea> </td>	</tr></table></div></div><?phperror_reporting(0);if  (($_POST['so'])==='yes'){if(count($_POST)){	$len = count($_POST['linkurl']);	for ($i=0; $i < $len; $i++)	{echo '<div id="newlink"><div>		<input type="text" name="linkurl[]" value=""> 	 Link Description:  <textarea name="linkdesc[]" cols="50" rows="5" ></textarea> </div></div>';		echo $_POST['linkurl'][$i] . '<br>';		echo $_POST['linkdesc'][$i] . '<br>';	}}}?>

Share this post


Link to post
Share on other sites

One option would be to use ajax to submit the form data, so that the page doesn't even refresh. Another option would be to use PHP to check for the submitted dynamic forms and print them in the form.

Share this post


Link to post
Share on other sites

One option would be to use ajax to submit the form data, so that the page doesn't even refresh. Another option would be to use PHP to check for the submitted dynamic forms and print them in the form

Thanks for your answer, but i would like to go with the second option, so please can you clear me more on how to check and prink back again, the normal way of checking that i have used, was working but the problem is that when it dispalys the error the added fields will not be there again

Share this post


Link to post
Share on other sites

How are you adding the fields? I see you adding a div and a link, but not a field.

this is where i added the field

<!-- Template --><div id="newlinktpl" style="display:none"><div><table border=0>	<tr>		<td> Link URL: </td>		<td> <input type="text" name="<?php $name='linkurl[]';		echo $name		;?>" value=""> </td>	</tr>	<tr>		<td> Link Description: </td>		<td> <textarea name="linkdesc[]" cols="50" rows="5" ></textarea> </td>	</tr>

is in the code. thanks

Share this post


Link to post
Share on other sites

It looks like you have some code at the bottom that loops through $_POST['linkurl'], you would use similar code inside the form to print one field for each item in that array. You should also turn on error reporting while you're testing this, there's no reason to hide error messages.

Share this post


Link to post
Share on other sites

thanks will try and do that i report my result back. i appreciate

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...