napsternapster Posted March 19, 2009 Share Posted March 19, 2009 hi.. Im trying to validate a form using ajax on the server-side.My problem it must validate the contant of dynamically created textboxes and give error messages from php back to ajax The code for the form is follows <?php $display_info .="<h1>Basic information</h1>"; //$display_info .= ""; //get parts of the record $entity_select ="select et_id, et_desc as type_ent from entity_type order by et_id asc"; $select_entity_type = $conn->query($entity_select); //checks if the returned are less than 1 it must show an error message //if they are.informing the user if(mysql_num_rows($select_entity_type) < 1) { //no records $display_info .="<p><em>Sorry, no records to select!</em></p>"; } else { $display_info .= "<form method=\"post\" name=\"fmInfo\" \" action=\"$_SERVER[php_SELF]\"> <table align=\"top\" width=\"60%\" border=\"0\" cellpadding=\"3\" cellspacing=\"3\"> <tr><td align=\"left\">Select Entity Type: </td> <td align=\"left\" ><select name=\"sel_id\" id=\"sel_id\" onchange=\"entityError(fmInfo)\"> <option value=\"0\">-- Select one --</option>"; /*loops through the returned values by sql statement from the db and displays the entity_type in combobox*/ while($recs =$conn->fetchArray($select_entity_type)) { $id = $recs['et_id']; $display_name = stripslashes($recs['type_ent']); //populates the combobox $display_info .= "<option value=\"$id\"> $display_name</option><br />"; } } /* o_name => official_name k_name => known_name */ //the design of the all user interface for adding $display_info .= "</select></td><td align=\"left\" width=\"70%\"> <span id=\"lblEntity_error\" style=\"color:red\" align=\"left\"> </span></td></tr> <tr> <td align =\"left\" >Official Name:</td> <td align=\"left\" > <input type=\"text\" name=\"txtOff_name\" id=\"txtOff_name\" onchange=\"TextColor(fmInfo)\" value=\"$official_name\" size=30 maxlength=70></td> <td align=\"left\" ><span id=\"lblOff_error\" style=\"color:red\" align=\"left\"> </span></td> </tr> <tr> <td align=\"left\" >Known Name: </td> <td align=\"left\" ><input type=\"text\" id=\"txtknown_name\" name=\"txtknown_name\" value=\"$known_name\" onchange=\"KnownName(fmInfo)\" size=30 maxlength=70></td> <td align=\"left\" ><span id=\"lblknown_error\" style=\"color:red\" align=\"left\"> </span></td> </tr>"; $next = 0; $array1 = array(); //$official = trim(strip_tags($_POST[txtOff_name])); //$counter = 0; //end of basic information $display_info .="<tr><td colspan=\"2\" align=\"left\">Select information type from the bellow</td></tr><br/><br />"; //reading the information types $info_types = "select info_types_id,info_types_desc from info_types LIMIT 1"; $get_info_types = $conn->query($info_types); while($rec_info_type = $conn->fetchArray($get_info_types)) { $info_id = $rec_info_type['info_types_id']; $info_desc =$rec_info_type ['info_types_desc']; $counter++; $info_desc = strtoupper($info_desc); $display_info .= "<tr><td width=\"50%\" align=\"left\"><a href=\"$_SERVER[php_SELF]?info_id=$info_id\"><div id=\"expander$counter\" style=\"display:inline\"><a href=\"#\" onclick=\"javascript:NameValidation($counter)\" class=\"ec\">[+]</a></div> $info_desc</td></tr><br /> </table>"; //show sthe second table if contact type is selected $display_info .= "<table align=\"left\" id=\"d$counter\" style=\"display:none\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\"> "; //getting the bridge information $select_info_type_detail = "select detailed_info_types_id from info_types_detailed_info_types where info_types_id = '$info_id'"; $get_select_info_type_detail = $conn->query($select_info_type_detail); while($rec_detailed_info_bridge = $conn->fetchArray($get_select_info_type_detail)) { $detailed_info_type_id = $rec_detailed_info_bridge['detailed_info_types_id']; ///echo $detailed_info_type_id."<br />"; //getting the information types detailed eg email,fax,mobile $select_detailed_info = "select detailed_info_types_id ,detailed_info_types_desc from detailed_info_types where detailed_info_types_id = '$detailed_info_type_id'"; $get_select_detailed_info = $conn->query($select_detailed_info); while($rec_detailed_info = $conn->fetchArray($get_select_detailed_info)) { $count_detailed++; $detailed_id = $rec_detailed_info['detailed_info_types_id']; $detailed_desc = $rec_detailed_info['detailed_info_types_desc']; $display_info .= "<td align=\"left\" valign=\"top\">$detailed_desc </td>"; //getting the infor from the detailed info types identitfier table $display_info.="<td align=\"left\" >"; for($i = 1; $i <= 9; $i++) { if($i == 1 ) { //$display_info .= $id." >> ".$desc ." <=> "; $display_info .="<select id=\"select_id$info_id"."_$detailed_id"."_$i"."\" name=\"select_id$info_id"."_$detailed_id"."_$i"."\" style=\"display:inline\" onchange=\"showNext(this.value,'txt$info_id"."_$detailed_id"."_$i','select_id$info_id"."_$detailed_id"."_$i')\" <option value=\"0\">-- Select one --</option>"; } else if($i > 1 && $i <= 9) { $display_info .="<select id=\"select_id$info_id"."_$detailed_id"."_$i"."\" style=\"display:none\" onchange=\"showNext(this.value,'txt$info_id"."_$detailed_id"."_$next','select_id$info_id"."_$detailed_id"."_$i','txt$info_id"."_$detailed_id"."_$previous')\" name=\"select_id$info_id"."_$detailed_id"."_$i\" <option value=\"0\">-- Select one --</option>"; } //adds 1 to $next $next = $i + 1; if($i >= 1) { $previous = $next - 1; } //reading the records from detailed_info_types_identifiers and detailed_info_types_detailed_info_types_identifiers $select_identifier = "select detailed_info_types_identifiers_id, detailed_info_types_identifiers_desc from detailed_info_types_identifiers inner join detailed_info_types_detailed_info_types_identifiers on detailed_info_types_identifiers.detailed_info_types_identifiers_id = detailed_info_types_detailed_info_types_identifiers.Detailed_Info_Types_Identifier_ID and detailed_info_types_detailed_info_types_identifiers.detailed_info_types_id = '$detailed_id'"; //where detailed_info_types_identifiers_id = '$identifier_bridge_id' $get_select_identifier = $conn->query($select_identifier); //populating the dropdown while($records = $conn->fetchArray($get_select_identifier)) { $detailed_info_types_identifiers_id = $records['detailed_info_types_identifiers_id']; $detailed_info_types_identifiers_desc = $records['detailed_info_types_identifiers_desc']; $display_info .="<option value=\"$detailed_info_types_identifiers_id\"> $detailed_info_types_identifiers_desc</option><br />"; ///echo $detailed_info_types_identifiers_desc; } $display_info .="</select> "; //<span id=\"lblError$info_id"."_$detailed_id"."_$i"."\" style=\"color:red\" align=\"left\"></span>"; //checks if $i >= 1 and must be less than 9 if($i >=1 && $i < 9) { $display_info .= " <input type=\"text\" size=15 style=\"display:none\" maxlength=40 name=\"txt$info_id"."_$detailed_id"."_$i"."\" id=\"txt$info_id"."_$detailed_id"."_$i"."\" value=\"$info\" onblur=\"showNext(this.value,'select_id$info_id"."_$detailed_id"."_$next','txt$info_id"."_$detailed_id"."_$i','select_id$info_id"."_$detailed_id"."_$previous')\" onmouseover=\"ShowErrors($counter,$count_detailed,fmInfo);\"> "; } //when the $i is greater than 8 shows the last textbox else if($i > { $display_info .=" <input type=\"text\" size=15 style=\"display:none\" maxlength=40 name=\"txt$info_id"."_$detailed_id"."_$i"."\" id=\"txt$info_id"."_$detailed_id"."_$i"."\" onblur=\"showNext(this.value,'','txt$info_id"."_$detailed_id"."_$i','select_id$info_id"."_$detailed_id"."_$previous')\">"; } //checks if the $i == 3 or ==6 then breaks if($i == 3 || $i == 6) { $display_info .="<br />"; } } $display_info .="</td></tr>"; } } $display_info .= "</td></tr> </table>"; } //echo $counter." ".$count_detailed; $display_info .=" <input type=\"hidden\" name=\"op\" value=\"add\">"; $display_info .= " <br /> <br /><tr><td align=\"left\"><input type=\"button\" id=\"btnSubmit\" name =\"btnSubmit\" value=\"Submit\" onclick=\"validate($counter,$count_detailed,fmInfo)\" ></td></tr><br /> <tr><td align=\"left\" ><span id=\"lblerror\" name=\"lblerror\" style=\"color:red\" align=\"left\"> </span></td></tr> </form>"; ?> ============================ Th code for the ajax/javascript function validate(contact_counter,detailed_counter,f) { xmlHttp = new XMLHttpRequest(); //alert(official_name); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); } var counter = 0; var error_count = 0; var server_error_count = 0; var message = ""; var ct = 0; var count = 0; var selection = document.getElementById("sel_id").value; var o_name = document.getElementById("txtOff_name").value; var kn_name = document.getElementById("txtknown_name").value; ///alert(o_name); //check if selection is made if(selection == 0) { error_count++; document.getElementById("sel_id").style.background = "red"; document.getElementById("lblEntity_error").innerHTML = "Select entity type first"; } else { document.getElementById("sel_id").style.background = "white"; document.getElementById("lblEntity_error").innerHTML = ""; } //check if official name has text if(o_name.length == 0) { document.getElementById("txtOff_name").style.background = "red"; document.getElementById("lblOff_error").innerHTML = "Please enter the official name."; error_count++; } else if(o_name.length < 3) //checks if the length of officail name is greate than 5 { document.getElementById("txtOff_name").style.background = "red"; document.getElementById("lblOff_error").innerHTML = "Official name is too small."; error_count++; } else { document.getElementById("txtOff_name").style.background = "white"; var url="Name.php"; url=url+"?name="+o_name; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "") { document.getElementById("txtOff_name").style.background = "red"; document.getElementById("lblOff_error").innerHTML = xmlHttp.responseText; server_error_count++; } else { //document.getElementById("lblOff_error").innerHTML = xmlHttp.responseText; document.getElementById("txtOff_name").style.background = "white"; } //xmlHttp.responseText =""; } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } //checks if known name has text if(kn_name.length == 0) { error_count++; document.getElementById("txtknown_name").style.background = "red"; document.getElementById("lblknown_error").innerHTML = "Please enter known name."; } else if(kn_name.length < 2) //checks if known name length is greater or less than 2 { error_count++; document.getElementById("txtknown_name").style.background = "red"; document.getElementById("lblknown_error").innerHTML = "Known name is too small."; } else { var url="known_name.php"; url=url+"?kname="+kn_name; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != 2 ) { document.getElementById("txtknown_name").style.background = "red"; document.getElementById("lblknown_error").innerHTML = xmlHttp.responseText; server_error_count++; } else { //document.getElementById("lblOff_error").innerHTML = xmlHttp.responseText; document.getElementById("txtknown_name").style.background = "white"; } } } xmlHttp.open("GET",url,true); xmlHttp.send(null); } for(counter = 1; counter <= contact_counter; counter++) { for(var count = 1; count <= detailed_counter; count++) { for(ct = 1;ct < 9 ;ct++) { if(counter == 1) { if(count == 6) { count += 2; } if(document.getElementById("txt"+counter+"_"+count+"_"+ct).value == null && document.getElementById("select_id"+counter+"_"+count+"_"+ct).value == null) { //do nothing } else { var info = document.getElementById("txt"+counter+"_"+count+"_"+ct).value; var selected = document.getElementById("select_id"+counter+"_"+count+"_"+ct).value; if(count == 1) { if(info !="") { if(!validate_numbers(info)) { error_count++; document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; //fax = info; } else if(validate_numbers(info)) { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; var url="validateContactInformation.php"; url=url+"?information="+info; url=url+"&sid="+Math.random(); alert(info); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "2" && xmlHttp.responseText != "") { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; server_error_count++; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } } } else if(count == 3) { if(info !="") { if(!validate_numbers(info)) { error_count++; document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; var url="validateContactInformation.php"; url=url+"?information="+info; url=url+"&sid="+Math.random(); alert(info); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "2" && xmlHttp.responseText != "") { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; server_error_count++; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } } } else if(count == 4) { if(info !="") { if(!validate_numbers(info)) { error_count++; document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; var url="validateContactInformation.php"; url=url+"?information="+info; url=url+"&sid="+Math.random(); alert(info); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "2" && xmlHttp.responseText != "") { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; server_error_count++; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } } } else if(count == 2) { if(info != "") { if(!validate_email(info)) { error_count++; document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; var url="validateContactInformation.php"; url=url+"?information="+info; url=url+"&sid="+Math.random(); alert(info); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "2" && xmlHttp.responseText != "") { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; server_error_count++; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } } } else if(count == 5) { if(info !="") { if(!validate_site(info)) { error_count++; document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; var url="validateContactInformation.php"; url=url+"?information="+info; url=url+"&sid="+Math.random(); alert(info); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "2" && xmlHttp.responseText != "") { //document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; server_error_count++; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } } } else if(count == 10) { if(info !="") { if(!validate_code(info)) { error_count++; document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; var url="validateContactInformation.php"; url=url+"?information="+info; url=url+"&sid="+Math.random(); alert(info); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "2" && xmlHttp.responseText != "") { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "red"; server_error_count++; } else { document.getElementById("txt"+counter+"_"+count+"_"+ct).style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); } } } } } else if(counter ==2) { } else if(counter ==3) { } else if(counter ==4) { } } } } if(error_count > 0) { //alert(message); } else { /*var ct = 0; alert(kn_name); var url="known_name.php"; url=url+"?kname="+kn_name; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState==4 && xmlHttp.status==200) { if(xmlHttp.responseText != "") { document.getElementById("txtknown_name").style.background = "red"; document.getElementById("lblknown_error").innerHTML = xmlHttp.responseText; ct++; } else { //document.getElementById("lblOff_error").innerHTML = xmlHttp.responseText; document.getElementById("txtknown_name").style.background = "white"; } } } xmlHttp.open("POST",url,true); xmlHttp.send(null); //f.submit();*/ } } //end of function validate ============================== The code to validate the contact information is a php file <?php $information = trim(strip_tags(stripslashes($_POST["information"]))); include_once "functions.php"; if(isset($information)) { if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $information)) { //if does not contain @ let it pass } else if(ereg("^[^@]{1,64}@[^@]{1,255}$", $information)) //when contacts contains "@" do the following check { if(!validate_Email($information)) { echo "Please enter a correct email address."; } else { echo ""; } } elseif(!stristr($information,"http://www.")) { // do nothing pass the contact } else if(stristr($information,"http://www.")) //when contacts contains "http://www." do the following check { if(!validate_site($information)) { echo "Please enter a valid web address."; } else { echo ""; } } elseif(!is_numeric($information)) { //is not a number do number } else if(is_numeric($information)) //is a number do the following validation { if(strlen($information) == 10) { if(!validate_numbers($information)) { echo "Please enter correct contact nummbers."; } else { echo ""; } } else if(strlen($information) <= 6) { if(!validate_code($information)) { echo "Please enter a valid area code."; } else { echo ""; } } } else { echo "Please check your contact detailes before submiting"; } } echo $information."jsdkhfjhsdj"; ?> ================================= The ajax/javascript must validate the client-side.if no errors found it must continue validating the server-side using ajax and must return the error messages and the textboxes on the main form must be red if errors found on it. Quote Link to comment https://forums.phpfreaks.com/topic/150104-how-validate-sever-side-using-ajax/ Share on other sites More sharing options...
Floydian Posted March 19, 2009 Share Posted March 19, 2009 My problem it must validate the contant of dynamically created textboxes and give error messages from php back to ajax That sounds like a problem, as in "the goal of your program". But, it doesn't sound like a problem, as in "but here is the error I'm getting" or "it works up until this point" or something like that. Basically, you've told us nothing about were your script failed. Quote Link to comment https://forums.phpfreaks.com/topic/150104-how-validate-sever-side-using-ajax/#findComment-788603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.