Panta Posted March 28, 2015 Share Posted March 28, 2015 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>'; }}}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 30, 2015 Share Posted March 30, 2015 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. Link to comment Share on other sites More sharing options...
Panta Posted March 31, 2015 Author Share Posted March 31, 2015 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 Link to comment Share on other sites More sharing options...
justsomeguy Posted March 31, 2015 Share Posted March 31, 2015 How are you adding the fields? I see you adding a div and a link, but not a field. Link to comment Share on other sites More sharing options...
Panta Posted April 2, 2015 Author Share Posted April 2, 2015 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 Link to comment Share on other sites More sharing options...
justsomeguy Posted April 2, 2015 Share Posted April 2, 2015 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. Link to comment Share on other sites More sharing options...
Panta Posted April 2, 2015 Author Share Posted April 2, 2015 thanks will try and do that i report my result back. i appreciate Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now