|
 |
|
อยากจะกำหนดค่าของ checkbox ที่ส่งโดย javascript (ajax) |
|
 |
|
|
 |
 |
|
คือเวลาส่งข้อมูลไปแล้ว ค่าของ checkbox มันเป็น true กับ false
ต้องการที่จะให้เป็น 1 กับ 0 ครับ
ส่วนของ ajax
Code (JavaScript)
var HttPRequest = false;
function doCallAjax(Mode) {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'tab_add.php';
var pmeters = "&tArtist=" + encodeURI( document.getElementById("txtArtist").value ) +
"&tTitle=" + encodeURI( document.getElementById("txtTitle").value ) +
"&tFile=" + encodeURI( document.getElementById("txtFile").value ) +
"&tRgt=" + encodeURI( document.getElementById("chkRgt").checked ) +
"&tLgt=" + encodeURI( document.getElementById("chkLgt").checked ) +
"&tBass=" + encodeURI( document.getElementById("chkBass").checked ) +
"&tPer=" + encodeURI( document.getElementById("chkPer").checked ) +
"&tVocal=" + encodeURI( document.getElementById("chkVocal").checked ) +
"&tKb=" + encodeURI( document.getElementById("chkKb").checked ) +
"&tMode=" + Mode;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("tab_add_span").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
document.getElementById("tab_add_span").innerHTML = HttPRequest.responseText;
document.getElementById("txtArtist").value = '';
document.getElementById("txtTitle").value = '';
document.getElementById("txtFile").value = '';
document.getElementById("chkRgt").checked = '';
document.getElementById("chkLgt").checked = '';
document.getElementById("chkBass").checked = '';
document.getElementById("chkPer").checked = '';
document.getElementById("chkVocal").checked = '';
document.getElementById("chkKb").checked = '';
}
}
}
ส่วน input
Code
<td><input type="text" name="txtArtist" id="txtArtist"></td>
<td><input type="text" name="txtTitle" id="txtTitle"></td>
<td><input type="text" name="txtFile" id="txtFile"></td>
<td><input type="checkbox" name="chkRgt" id="chkRgt" value="1"></td>
<td><input type="checkbox" name="chkLgt" id="chkLgt" value="1"></td>
<td><input type="checkbox" name="chkBass" id="chkBass" value="1"></td>
<td><input type="checkbox" name="chkPer" id="chkPer" value="1"></td>
<td><input type="checkbox" name="chkVocal" id="chkVocal" value="1"></td>
<td><input type="checkbox" name="chkKb" id="chkKb" value="1"></td>
ส่วนของการเพิ่มข้อมูลลง db
Code (PHP)
if($strMode == "ADD")
{
$strSQL = "INSERT INTO tt_tabs ";
$strSQL .="(artist,title,file,ins_rgt,ins_lgt,ins_bass,ins_per,ins_vocal,ins_key) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["tArtist"]."','".$_POST["tTitle"]."','".$_POST["tFile"]."','".$_POST["tRgt"]."','".$_POST["tLgt"]."' ";
$strSQL .=",'".$_POST["tBass"]."','".$_POST["tPer"]."','".$_POST["tVocal"]."','".$_POST["tKb"]."') ";
$objQuery = mysql_query($strSQL);
}
ขอบคุณครับ
Tag : PHP, HTML/CSS, Ajax

|
ประวัติการแก้ไข 2011-03-12 15:07:56
|
 |
 |
 |
 |
Date :
2011-03-12 15:05:27 |
By :
tsctao |
View :
2010 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ในโคดที่เป็น PHP ก่อนการบันทึกข้อมูลก็เช็คในส่วนนี้ก่อนจะบันทึกข้อมูล
if($_POST["tRgt"]==true)
{
$tRgt = 1;
}else{
$tRgt = 0;
}
เอาตัวแปล $tRgt นี้ไปบึนทึกแทน
|
 |
 |
 |
 |
Date :
2011-03-12 17:13:14 |
By :
ooddoo888 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|