我有一个标准的html表单
<input type="radio" class="input" id="radiofill" name="radiofill" onclick="ajax('frm_tb_set_pgs','ajx-db/ajx-db_set_pgs_add_1.php','frm_type','tb_set_pgs','errmsg','','','','false','POST','false');"><label for="radiofill"> Populate Data</label>
我希望用户能够按下单选按钮并用最后一个填写表单 mysql表中的数据行。
ajax函数如下:
function buildPOST(theFormName, URLto) {
var theForm = document.getElementById(theFormName);
var qs = '';
for (e = 0; e < theForm.elements.length; e++) {
if (theForm.elements[e].name != '') {
var name = theForm.elements[e].name;
qs += (qs == '') ? '' : '&';
qs += name + '=' + encodeURIComponent(theForm.elements[e].value);
}
}
// qs+="\n";
return qs;
}
function getobjval(value1, varname) {
return '&' + varname + '=' + value1;
}
function ajax(theFormName, URLto, frm_type, varname, olddivid, newdivid, itemid, pgid, resetfrm, pgval, reloadopt) {
var xmlMessage;
var getvals;
var gelm_var;
var gelm;
var selval;
if (URLto.indexOf("?") == -1) {
URLto = URLto + "?d=0";
}
xmlMessage = buildPOST(theFormName, URLto);
if (itemid == '' || itemid == null) {} else {
gelm = document.getElementById(itemid);
//xmlMessage=xmlMessage+"&itemval="+gelm.value;
switch (frm_type) {
case "sel":
getvals = gelm.value;
break; //getvals = gelm.options[length].value;
case "addsel":
if (varname != null) {
gelm_var = document.getElementById(varname);
selval = gelm.options[gelm.selectedIndex].value;
// xmlMessage = xmlMessage+getobjval(selval,varname)
}
break;
case "but_add":
if (gelm.style.visibility = 'visible') {
gelm.style.visibility = 'hidden';
} else {}
break;
case "none":
break;
}
}
var oXMLHttpRequest = new XMLHttpRequest;
oXMLHttpRequest.open(pgval, URLto + "&" + xmlMessage + "&varnm=" + varname, true);
if (pgval == "POST") {
//Send the proper header information along with the request
oXMLHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
oXMLHttpRequest.onreadystatechange = function () {
if (this.readyState == XMLHttpRequest.DONE) {
if (newdivid == null || newdivid == '') {
if (olddivid == null || olddivid == '') {} else {
var olddiv = document.getElementById(olddivid);
olddiv.innerHTML = '';
olddiv.innerHTML = this.responseText;
}
} else {
var newdiv = document.createElement('div');
newdiv.innerHTML = this.responseText;
if (olddivid == null || olddivid == '') {} else {
var olddiv = document.getElementById(olddivid);
olddiv.innerHTML = '';
document.getElementById(newdivid).appendChild(newdiv);
olddiv.parentNode.removeChild(olddiv);
}
}
// document.getElementById(itemid).options.focus();}
}
if (resetfrm == 'true') {
document.forms[1].reset();
}
}
oXMLHttpRequest.send(xmlMessage);
if (reloadopt == 'true') {
document.location.reload();
}
}
所以单选按钮调用以下php:
ajx-db_set_pgs_add_1.php
<?php include ("../php_01_db_hdr.php");
include ("../inc.php");
$sel_fldnm ="pg_id";
$sel_tbnm ="tb_set_pgs";
if (isset($_POST['fldnm'])) {
$sel_fldnm = $_POST['fldnm'];}
if (isset($_POST['tbnm'])) {
$sel_tbnm = $_POST['tbnm'];}
$sql = "SELECT * FROM `".$sel_tbnm."` ORDER BY `".$sel_fldnm."` DESC LIMIT 1";
foreach( $db->query($sql) as $dbrow ) {
$dblist_pg_url = clean(($dbrow['pg_url']));
$dblist_pg_fldr_id = clean(intval($dbrow['pg_fldr_id']));
$dblist_pg_icn_flnm = clean(($dbrow['pg_icn_flnm']));
$dblist_pg_alt_icn_flnm = clean(($dbrow['pg_alt_icn_flnm']));
$dblist_pg_icn_szwd = clean(intval($dbrow['pg_icn_szwd']));
$dblist_pg_icn_szht = clean(intval($dbrow['pg_icn_szht']));
$dblist_pg_dt_crtd = clean(($dbrow['pg_dt_crtd']));
$dblist_pg_dt_tmst = clean(($dbrow['pg_dt_tmst']));
}
header('Pragma: no-cache');
?><script type="text/javascript" src="../js/std.js"></script><script>
populatefields ("pg_dt_tmst", '<?php echo $dblist_pg_dt_tmst; ?>');
populatefields ("pg_dt_crtd", '<?php echo $dblist_pg_dt_crtd; ?>');
populatefields ("pg_icn_szht", '<?php echo $dblist_pg_icn_szht; ?>');
populatefields ("pg_icn_szwd", '<?php echo $dblist_pg_icn_szwd; ?>');
populatefields ("pg_alt_icn_flnm", '<?php echo $dblist_pg_alt_icn_flnm; ?>');
populatefields ("pg_icn_flnm", '<?php echo $dblist_pg_icn_flnm; ?>');
populatefields ("pg_fldr_id", '<?php echo $dblist_pg_fldr_id; ?>');
populatefields ("pg_url", '<?php echo $dblist_pg_url; ?>');
// etc......
</script>
函数填充字段如下:
function populatefields (fldnm, datavalue) {
var elem_id = document.getElementById(fldnm);
elem_id.value = datavalue;
}
问题是当你单击表单中的元素时不要改变。
让ajax工作的任何帮助都会很棒。 感谢