php - Validating form data with Multiple inputs -
i have code @ moment user input multiple items text input separated comma, gets submitted php backend , using explode, can retrieve correct values , process them through jquery.
here problem. need find way validate input using several input boxes. maximum 8 , struggling find way this, using existing code.
firebug states the post 2 separate box_add posts ie, box_add 192 box_add 193. cannot use [] because interfere validator. grateful someguidance how move forward this. many thanks
html relevant code
<a href="#" id="intkaddmorefilebox" class="btn btn-info">add more box fields</a> <div id="intkinputswrapper"> <div> <input name="box_add" type="text" id="box_add" /> <a href="#" class="removeclass">×</a> <a style="margin-left: 14px;" href="javascript:void(0)" title="just example" class="tooltip">help</a> </div> </div>
php code
<?php session_start(); $connect = mysql_connect("localhost", "root", "") or die ('{"opp":"error","box":"mysql_connect failed"}'); $db = mysql_select_db("sample"); // test vars jquery form $status = mysql_real_escape_string($_request['status']); $company = mysql_real_escape_string($_request['company']); $requested = mysql_real_escape_string($_request['requested']); $activity = mysql_real_escape_string($_request['activity']); $address = mysql_real_escape_string($_request['address1']); $service = mysql_real_escape_string($_request['service']); $destroydate = mysql_real_escape_string($_post['datepicker']); $date = explode('/', $_post['datepicker']); $newdate = $date[2].'-'.$date[1].'-'.$date[0]; //$box = mysql_real_escape_string($_request['box_add']); // never used $authorised = mysql_real_escape_string($_session['kt_name_usr']); $dept = mysql_real_escape_string($_request['dept']); $boxerrortext = 'error'; // split box if multiples $array = explode(',', $_request['box_add']); $outstring = ''; foreach ($array $box) { $box = mysql_real_escape_string($box); $sql = "select item act item = '$box'"; $result = mysql_query($sql) or die ('{"opp":"error","box":"' . mysql_error() . '"}'); // if there dupe entries, send message jquery if (mysql_num_rows($result) > 0) { $outstring .= $box . ' '; } } if ($outstring) { $error = array('opp' => "error", 'box' => $outstring); $output = json_encode($error); echo $output; exit(); } foreach ($array $box) { $outstring .= "<br />company: $company <br /> address: $address <br />service: $service <br />destroy date: $destroydate <br />box: $box <br />"; $box = mysql_real_escape_string($box); $sql = "insert `temp` (service, activity, department, company, address, user, destroydate, date, item, new)"; $sql .= "values ('$service', '$activity', '$dept', '$company', '$address', '$requested', '$newdate', now(), '$box', 1)"; $result = mysql_query($sql) or die ('{"opp":"error","box":"' . mysql_error() . '"}'); } $json = array('opp' => 'insert', 'box' => $outstring); $result = json_encode($json); echo $result; ?>
script add inputs
<script type="text/javascript"> $(function() { var maxinputs = 8; //maximum input boxes allowed var inputswrapper = $("#intkinputswrapper"); //input boxes wrapper id var addbutton = $("#intkaddmorefilebox"); //add button id var x = inputswrapper.length; //initlal text box count var fieldcount=1; //to keep track of text box added $(addbutton).click(function (e) //on add input button click { if(x <= maxinputs) //max input box allowed { fieldcount++; //text box added increment //add input box $(inputswrapper).append('<div><input type="text" name="box_add" id="box_add" /><a href="#" class="removeclass">×</a></div>'); x++; //text box increment } return false; }); $("body").on("click",".removeclass", function(e){ //user click on remove text if( x > 1 ) { $(this).parent('div').remove(); //remove text box x--; //decrement textbox } return false; }) }); </script>
why don't enumerate inputs instead?
something like:
$(inputswrapper).append('<div><input type="text" name="box_add'+fieldcount+'" id="box_add'+fieldcount+'" /><a href="#" class="removeclass">×</a></div>');
and when removing input add rest counter
fieldcounter--;
now, on php side, can process these fields by:
using regex find max amount of inputs sent based on input id (if box_add7 exists, 7 max number of box_add iterate)
or send field counter value on hidden field
or, if using ajax, parameter value of fieldcount.
for ($i = 1; $i <= fieldcounter; $i++){ $value = $_post["box_add".$i]; //do $value }
Comments
Post a Comment