I'm building a website based on wordpress and I put there a form which uses jQuery. However, I'm starting to understand jquery just now, so i am not feeling very well while working with it yet. I have two questions.
First One: Regarding this form, is it necessary to validade the form at both client and server side? Or only at server-side (php) is enough?
Second Question: The form have some fields, which can be duplicated if I click on a button. For this, I'm using the following code:
Jquery Code:
(function($){
$countForms = 1;
$.fn.addForms = function(idform){
var myform = "<table>"+
" <tr>"+
" <td>Field A ("+$countForms+"):</td>"+
" <td><input type='text' name='fielda["+$countForms+"]'></td>"+
" <td>Field B ("+$countForms+"):</td>"+
" <td><textarea name='fieldb["+$countForms+"]'></textarea></td>"+
" <td><button>remove</button></td>"+
" </tr>"+
"</table>";
if(idform=='mybutton'){
alert(idform);
myform = $("<div>"+myform+"</div>");
$("button", $(myform)).click(function(){ $(this).parent().parent().remove(); });
$(this).append(myform);
$countForms++;
}
};
})(jQuery);
$(function(){
$("#mybutton").bind("click", function(e){
e.preventDefault();
var idform=this.id;
if($countForms<3){
$("#container").addForms(idform);
}
});
});
Html:
<div id="container"><div>
<form method="post" name="b" >
<table>
<tr>
<td>Field A</td>
<td><input type='text' name='dadoA'></td>
<td>Field B</td>
<td><textarea name="dadoB"></textarea></td>
<td><button>remove</button></td>
</tr>
</table>
</div>
</div>
<button id="mybutton">add form</button>
<div align="center">
<p><input type="submit" value="Registar" name="registar"></p>
</div>
I know how to use php to validate these fields. What I don't know is how to validate when I duplicate them.
I mean, I have this form with 2 fields, I duplicate them and suppose user fills this new (third) field with a wrong/unaccepted value.
After clicking "Submit", how can I use PHP to validate input, refresh the page, echo the 2 original fields and the duplicate ones(which user duplicated), all with their respective inputs filled by user and with the error message?