gpt4 book ai didi

javascript - 数据库更新后php/javascript更改内联标签

转载 作者:行者123 更新时间:2023-11-28 07:01:35 25 4
gpt4 key购买 nike

这是我的第一篇文章..我的问题可能很简单,但我找不到正确的方法..!

我有一个 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”,我在新页面中看到结果“已保存”或“错误”,并且数据库中的记录更新是可以的

我想要的不是将“已保存”放入新页面,而是更新“保存”按钮旁边的 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>

3) upd.php页面

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 打开它。

在由 ajax 调用调用的 PHP 代码中,您应该以 JSON 格式返回正确的结果。您必须在 JS 中解析该字符串,并相应地更新 DOM。

关于javascript - 数据库更新后php/javascript更改内联标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32098675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com