php/javascript在数据库更新后更改内联标签
php/javascript change inline label after DB update
这是我的第一个帖子。我的问题可能很简单,但我找不到正确的方法…!
我有一个php页面,从数据库查询选择显示许多记录,对于每条记录,我已经把表单与一些字段需要更新和一个"保存"按钮
因此,对于每个记录,我在结果表中有一个列,其中包含如下形式:
$code = "<td><form method='POST' action='mypage.php' target='_blank' />";
$code .= " <input type='hidden' name='function' value='formtaglieok' />";
$code .= " <input type='hidden' name='email' value='".$email."' />";
$code .= " <input type='hidden' name='main' value='".$main."' />";
..... some other editing fields
$code .= "<input type='text' name='field1' value='' size='2' />"
..... some other editing fields
$code .= "<td><input type='submit' value='Save' /></td>"
在这一列之后,我把标签,我想改变后按下按钮和更新记录,如:
$code .= "<td><div id='<this_record_id>' ></div></td>";
在mypage.php我有PHP代码更新记录:
function updaterecord($_POST){
...connection to db, prepare the query etc..
$stid = OCIParse($conn, $query);
if (OCIExecute($stid)) {
$res .= "Saved ";
} else {
$res .= "Error";
}
echo $res;
}
显然,有了这种形式的动作和目标"_blank",我看到在一个新的页面结果"保存"或"错误"和更新的记录在DB是ok
我不会把"保存"在一个新的页面,但更新div this_record_id旁边的"保存"按钮
那么,我将尝试在提交按钮中添加onClick事件
<input type='submit' value='Save' onclick='jSaved(<this_record_id>)' />
并把这段代码放在页首
<script type='text/javascript'>
function jSaved(bcode){
document.getElementById(bcode).innerHTML = 'Saved';
}
</script>
,它更新标签正确,但也打开另一个页面。
我要做的是使用$_POST数组在JS代码中执行我的更新函数,所以不要得到一个新的页面,而只是标签中函数的结果。
有人能帮帮我吗?
编辑:解决了
1)我的php主页与表单类似(重要设置的form_id):
$code = "<form name='frm_".$record['TD001_SEQ']."' id='frm_".$record['TD001_SEQ']."' action='' />";
$code .= " <input type='hidden' name='function' id='function' value='formtaglieok' />";
$code .= " <input type='hidden' name='email' id='email' value='".$email."' />";
$code .= " <input type='hidden' name='main' id='main' value='".$main."' />";
$code .= " <input type='hidden' name='store' id='store' value='".$store."' />";
$code .= " <input type='hidden' name='valuta' id='valuta' value='".$valuta."' />";
....other fields
//the code for the button (not submit)
$code .= "<td><input type='button' value='Save' onclick='jSaved(".$record['TD001_SEQ']."); '/></td>";
//the label DIV with the same reference of the form/record updating
$code .= "<td><div id='res_".$record['TD001_SEQ']."' ></div></td>";
2) javascript代码
function jSaved(td001){
//searching for exact form from the document page
var form = false;
var length = document.forms.length;
for(var i = 0; i < length; i++) {
if(document.forms[i].id == "frm_" + td001) {
form = document.forms[i];
}
}
//create a string containing all key/values from the form (parameters)
length = form.length;
var sParams = "";
for(var i = 0; i < length; i++) {
//will be key1=val1&key2=val2 ....
sParams = sParams + form.elements[i].id + "=" + form.elements[i].value + "&";
}
//execute the php update function with params in POST, td001 is needed to write le DIV label after update
var updResult = updateRecord("upd.php", sParams, td001);
}
//ajax code
function updateRecord(strUrl, params, idDiv) {
var xmlHttpReq = false;
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('POST', strUrl, true);
xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttpReq.send(params);
xmlHttpReq.onreadystatechange = function() {
/*state evaluation
* 0 - UNINITIALIZED
* 1 - LOADING
* 2 - LOADED
* 3 - INTERACTIVE
* 4 - COMPLETE*/
//state complete
if (xmlHttpReq.readyState == 4) {
//updating the DIV label with upd.php result
document.getElementById('res_' + idDiv).innerHTML = xmlHttpReq.responseText;
}
}
return resUpd;
}
</script>
if (isset($_POST)) {
funFormTaglieOK($_POST);
} else {
echo "Denied";
}
function funFormTaglieOK($params){
global $dbdw_usr, $dbdw_pwd, $dbdw_SID;
// Try to connect to Oracle
if ($conn = OCILogon($dbdw_usr, $dbdw_pwd, $dbdw_SID)) {
//execute record update
if (recordupdate is ok){
echo "Update"
} else {
echo "Error"
}
}
}
所以你应该使用ajax,不要使用target=_blank
如果你想要一个新窗口,你仍然可以通过Javascript打开它。
在你的PHP代码是由ajax调用,你应该返回正确的JSON格式的结果。你必须在JS中解析该字符串,并相应地进行DOM更新。
相关文章:
- 如何在更新面板内部调用/触发javascript函数<触发器>标签
- 当输入位于标签内时,更新无线电标签文本
- 使用 jQuery(移动)更新多个选择标签
- 变量中的值未在标签标记中更新
- Riot Js更新标签
- 更新条形图并删除标签
- 更新标签数组时如何对其进行排序
- 在Meteor.js中通过同一路线内的锚点标签更新视图
- 如何使用输入模糊事件更新多个具有相同类名的标签的文本
- 在不刷新的情况下更新标签内的变量
- 更新 DOM 中的标签
- 异步更新标签时出现问题
- 如何根据用户在文本框中键入的内容更新标签文本
- Javascript 在更新面板和向导中设置标签文本重置
- 如何在 d3 中动态更新文本标签
- 在道场中编码,1.得到错误所有者文档 null 或不是对象,2.按钮标签更改后未更新
- 选中了“标签更新”复选框
- 使用 javascript 更新 html
- 如何更新还包含输入元素的标签文本
- 如何更新p的标签:在primefaces中ajax调用后,在不关闭组件的情况下选择CheckboxMenu