php/javascript change inline label after DB update -
this first post.. question simple cannot find right way..!
i have php page query selection database show many records, each record i've put form fields need updated , "save" button
so each record have column in result table containing form like:
$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."' />"; ..... other editing fields $code .= "<input type='text' name='field1' value='' size='2' />" ..... other editing fields $code .= "<td><input type='submit' value='save' /></td>"
after column i've put label want change after pressing button , updating of record, like:
$code .= "<td><div id='<this_record_id>' ></div></td>";
in mypage.php have php code update record:
function updaterecord($_post){ ...connection db, prepare query etc.. $stid = ociparse($conn, $query); if (ociexecute($stid)) { $res .= "saved "; } else { $res .= "error"; } echo $res; }
obviously, kind of form action , target "_blank", see in new page result "saved" or "error" , updating of record in db ok
the thing not put "saved" in new page, update div this_record_id beside "save" button
so, i'll try add onclick event submit button
<input type='submit' value='save' onclick='jsaved(<this_record_id>)' />
and put code in head of page
<script type='text/javascript'> function jsaved(bcode){ document.getelementbyid(bcode).innerhtml = 'saved'; } </script>
and updating label correctly opening page.
what executing updating function inside js code using $_post array, don't new page result of function in label..
someone can me?
edit: solved
1) php main page form (important set 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 button (not submit) $code .= "<td><input type='button' value='save' onclick='jsaved(".$record['td001_seq']."); '/></td>"; //the label div same reference of form/record updating $code .= "<td><div id='res_".$record['td001_seq']."' ></div></td>";
2) javascript code
function jsaved(td001){ //searching exact form document page var form = false; var length = document.forms.length; for(var = 0; < length; i++) { if(document.forms[i].id == "frm_" + td001) { form = document.forms[i]; } } //create string containing key/values form (parameters) length = form.length; var sparams = ""; for(var = 0; < length; i++) { //will key1=val1&key2=val2 .... sparams = sparams + form.elements[i].id + "=" + form.elements[i].value + "&"; } //execute php update function params in post, td001 needed 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 div label upd.php result document.getelementbyid('res_' + iddiv).innerhtml = xmlhttpreq.responsetext; } } return resupd; } </script>
3) upd.php page
if (isset($_post)) { funformtaglieok($_post); } else { echo "denied"; } function funformtaglieok($params){ global $dbdw_usr, $dbdw_pwd, $dbdw_sid; // try connect oracle if ($conn = ocilogon($dbdw_usr, $dbdw_pwd, $dbdw_sid)) { //execute record update if (recordupdate ok){ echo "update" } else { echo "error" } } }
ok should use ajax, , don't use target=_blank.
if want new window, can still open javascript.
in php code called ajax call, should return right results in json format. have parse string in js, , dom update accordingly.
Comments
Post a Comment