Recently, we have made a small program for the expert library, and no expert has a field of expertise, which is submitted and edited by a checkbox. Asynchronous transfer using AJAX because you do not want to refresh the page.
First read all the majors from the professional data sheet and generate a checkbox:
$ (document). Ready (function () {$.ajax ({type:"POST", URL:"Http://localhost/yibu/index.php/Home/Expertadd/expertecho",//corresponding Controller read professional list success:function (result) {vardata =$.parsejson (Result); varLen =data.length; varListhtml=""; for(vari =0; i < Len; i++) {//loop to incorporate the professional into the checkbox listhtml+="<input type= ' checkbox ' id= '"+data[i]["Major"]+"' name= ' major ' value= '"+data[i]["ID"]+"'/>"+data[i]["Major"]+""; } listhtml="<tr><td colspan= ' 2 ' ><div class= ' Divcheck ' >"+listhtml+"</div></td></tr>"; $('#myTable'). Append (listhtml)})})
This code not only generates a checkbox, but also stores the professional name and the corresponding professional ID in the checkbox.
The next step is to determine the checkbox status:
var boolmajor = chemajor.is (': Checked ')//checkbox status Judgment
If the checkbox is selected, the traversal takes value and is stored in a two-dimensional array:
for (var i = 0; i < chemajor.length; i++) {// traversal checkbox if (chemajor[i].checked) { Datamajor.push ({"Majorid": Chemajor[i].value, "Major": Chemajor[i].id}); Professional Ability array Assignment }
}
A two-dimensional array cannot be passed directly to the controller in the background thinkphp, and JSON serialization is required:
var jsonmajor = json.stringify (datamajor); // professional capability arrays are serialized with JSON
Serialization can be called directly after the AJAX data transfer:
$.ajax ({// call Ajax Type: "POST", URL:"<{:u (' Expertadd/add ')}>", data: {' Dataexpert ':d ataexpert, ' jsonmajor ': jsonmajor},// Pass data to controller success:function (Result) { if(result== "Success") { var appenddiv= ' < Div id= "Appenddiv" class= "alert Green_alert" ><p> expert information written successfully! </p></div> '; $ ("#clearfix"). After (Appenddiv); }
}})
Background to the data, you need to deserialize the JSON string, note the addition of the parameter "true". This is to force the conversion to an array.
$major=d ("major");//Locating data Tables$jsonMajor=$_post["Jsonmajor"];//Receive JSON sequence$arrMajor= Json_decode ($jsonMajor,true);//Professional capability Array deserialization$len=Count($arrMajor); for($i= 0;$i<$len;$i++) {$arrMajor[$i["Expertid"]=$resultExpert;//Professional Ability Array join the corresponding expert ID}$resultMajor=$major->addall ($arrMajor);if($resultExpert>0 &&$resultMajor>0) {//Two tables are written to the front end with the input information successfully Echo"Success";}
After the JSON string is deserialized, it is further processed and the corresponding expert ID is added.
At this point, the input of the expert information has a corresponding professional field.
The effect is as follows: first, the foreground interface.
Insert two data tables, expert information table:
Professional Competency Chart for experts:
The data was saved successfully.
Ajax passing two-dimensional array to background thinkphp controller