基本上,我想要做的是将联系人添加为收藏夹。我列出了带有复选框的联系人,并在选中复选框后将值发送回我的数据库。如何从选中的复选框中获取值以保存在我的数据库中?
function mycontacts(){
var xmlhttp = new XMLHttpRequest();
var url = serverURL() + "/listcontact.php";
url += "?phoneno=" + localStorage.getItem("phoneno") +
"&contactno=" + contactno + "&contactid" + contactid;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getContacts(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function getContacts(response) {
var arr = JSON.parse(response);
var tbl=$("<table/>").attr("id","mytable");
$("#div1").append(tbl);
for(var i=0;i<arr.length;i++){
var tr="<tr>";
var chk="<th>"+"<input type='checkbox' />"+"</th>";
var td1="<td>"+arr[i].contactname+"</td>";
var td2="<td>"+arr[i].contactno+"</td></tr>";
$("#mytable").append(tr+chk+td1+td2);
}
}
function savefavourite(){
contactName = $("#contactname").val();
contactno = $("#contactno").val();
phoneno = localStorage.getItem("phoneno");
if (validate()){
var xmlhttp = new XMLHttpRequest();
var url = serverURL() + "/favourite.php";
url += "?contactName=" + contactName + "&contactno=" + contactno + "&phoneno=" + phoneno;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getFavourite(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
}
function validate(){
var validate = true;
if (contactName == "") {
alert("Contact Name is required");
$("contactname").focus();
validate = false;
}
else if (contactno == "") {
alert("Contact Number is required.");
$("#contactno").focus();
validate = false;
}
return validate;
}
function getFavourite(response) {
var arr = JSON.parse(response);
if (arr[0].result == "1"){
window.location = "home.html";
alert("Your favourite has been added.");
}
else {
alert("Unsuccessful");
}
}