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更新。