bohoro Posted March 8, 2007 Share Posted March 8, 2007 Hi I'm wondering if there's a smart way of submitting data to a database and then reload the form with the same values if it worked. I have a field in my form that has an autocomplete and that fills in the data for me. However I'm wondering how to get the data to be updated (if the user wants to) without reloading the entire page. Any ideas? main.php <?php ?> <html> <head> <script language="JavaScript" src="source.js" type="text/javascript"></script> <script language="JavaScript" src="xmlhttp.js" type="text/javascript"></script> </script> <script type="text/javascript"> <!-- var xmlhttp = false; try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(E){ xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){ xmlhttp = new XMLHttpRequest(); } function makerequest(serverPage, objID){ var obj = document.getElementById(objID); xmlhttp.open("GET",serverPage); xmlhttp.onreadystatechange = function(){ if( xmlhttp.readyState == 4 && xmlhttp.status ==200){ obj.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); } //--> </script> </head> <body> <div style="padding: 10px"> <div id="message"> <?php if(isset($_GET['message'])){ echo $_GET['message']; } ?> <form action="process_task.php" method="post" id="newtask" name="newtask"> Name<br /> <input name="yourname" style="width: 150px; heigth: 16px;" type="text" /><br /> <input type="button" value="submit" /> <div align ="right"><a href="javascript:closetask();">close</a></div> </form> <fieldset><label>Main</label> <div style="padding: 10px;"> <div id="messagebox"><div> <form id="userdata" method="post" action="" onsubmit="validateall()" > Name<br /> <input id="lastname" style="width: 150px, height:16px" type="text" value="" onkeypress="autocomplete(this.value,event)" /> <input id="firstname" style="width: 150px, height:16px" type="text" class="req"> <input id="hourly" style="width: 150px, height:16px" type="text" class="req"> <input id="monthly" style="width: 150px, height:16px" type="text" class="req"> <input id="availability" style="width: 150px, height:16px" type="text" class="req"> <input id="email" style="width: 150px, height:16px" type="text"> <input id="faxNumber" style="width: 150px, height:16px" type="text"> <input id="privatePhone" style="width: 150px, height:16px" type="text"> <input id="cellularPhone" style="width: 150px, height:16px" type="text"> <input id="businessPhone" style="width: 150px, height:16px" type="text"> <input id="webpage" style="width: 150px, height:16px" type="text"> <input id="city" style="width: 150px, height:16px" type="text"> <input id="postCode" style="width: 150px, height:16px" type="text"> <input id="adress" style="width: 150px, height:16px" type="text"> <input id="yearOfBirth" style="width: 150px, height:16px" type="text"> <br /> PP-history <br /> <textarea id="history" style="height: 80px;" ></textarea><br /> <input id="hiddenid" style="width: 150px, height:16px" type="hidden"> <div id="autocompletediv" style="visibility: hidden; left: 0px; top: 0px; position: absolute; width: 0px;"></div> <!-- updates the info about the consultants --> <input type="submit" name="update" onclick="validateall();return false;" value="update"/> </form> </fieldset> </body> </html> autocomplete.php <?php require_once("_res/db/dbconnect.php"); $db = opendatabase(); $myquery2 = "SELECT * FROM consultants JOIN employmentinfo JOIN contactinfo WHERE lastName LIKE LOWER('%". mysql_real_escape_string($_GET['sstring']) . "%') AND employmentinfo.consultantId=consultants.id AND contactinfo.consultId=consultants.id ORDER BY lastName ASC"; if($userquery = mysql_query($myquery2)){ while($userdata = mysql_fetch_array($userquery)){ if(!get_magic_quotes_gpc()) { $foundarr[] .= stripslashes($userdata['lastName']); $foundarrid[] .= stripslashes($userdata['id']); $foundarrs1[] .= stripslashes($userdata['hourly']); $foundarrs2[] .= stripslashes($userdata['monthly']); $foundarrs3[] .= stripslashes($userdata['availability']); $foundarrs6[] .= stripslashes($userdata['email']); $foundarrs7[] .= stripslashes($userdata['faxNumber']); $foundarrs8[] .= stripslashes($userdata['privatePhone']); $foundarrs9[] .= stripslashes($userdata['cellularPhone']); $foundarrs10[] .= stripslashes($userdata['businessPhone']); $foundarrs11[] .= stripslashes($userdata['webpage']); $foundarrs12[] .= stripslashes($userdata['city']); $foundarrs13[] .= stripslashes($userdata['postCode']); $foundarrs14[] .= stripslashes($userdata['adress']); $foundarrs15[] .= stripslashes($userdata['yearOfBirth']); /* 3 fält fattas ev. */ } else{ $foundarr[] = $userdata['lastName']; $foundarrid[] = $userdata['id']; $foundarrs[] = $userdata['hourly']; $foundarrs1[] = $userdata['hourly']; $foundarrs2[] = $userdata['monthly']; $foundarrs3[] = $userdata['availability']; $foundarrs6[] = $userdata['email']; $foundarrs7[] = $userdata['faxNumber']; $foundarrs8[] = $userdata['privatePhone']; $foundarrs9[] = $userdata['cellularPhone']; $foundarrs10[] = $userdata['businessPhone']; $foundarrs11[] = $userdata['webpage']; $foundarrs12[] = $userdata['city']; $foundarrs13[] = $userdata['postCode']; $foundarrs14[] = $userdata['adress']; $foundarrs15[] = $userdata['yearOfBirth']; } } } else { echo mysql_error(); } if(count($foundarr)>0 ){ ?> <div style="background: #CCCCCC"; border-style:solid; border-width: 1px; border-color= #000000;"> <?php for($i=0; $i< count($foundarr); $i++){ ?> <div style="padding: 4px; height: 14px;" onmouseover="this.style.background='#EEEEEE'" onmouseout="this.style.background = '#CCCCCC'" onclick="setvalue('<?php echo $foundarr[$i]; ?>','<?php echo $foundarrid[$i]; ?>','<?php echo $foundarrs[$i]?>','<?php echo $foundarrs1[$i];?>','<?php echo $foundarrs2[$i];?>','<?php echo $foundarrs3[$i];?>','<?php echo $foundarrs6[$i];?>','<?php echo $foundarrs7[$i];?>','<?php echo $foundarrs8[$i];?>','<?php echo $foundarrs9[$i];?>','<?php echo $foundarrs10[$i];?>','<?php echo $foundarrs11[$i];?>','<?php echo $foundarrs12[$i];?>','<?php echo $foundarrs13[$i];?>','<?php echo $foundarrs14[$i];?>','<?php echo $foundarrs15[$i];?>');"> <?php } ?> </div> <div style="padding: 4px; height: 14px;"> <?php //} ?> </div> <?php } ?> source.js function findPosX(obj) { var curtop = 0; if(obj.offsetParent){ while(obj.offsetParent){ curtop += obj.offsetTop; obj = obj.offsetParent; } }else if(obj.x){ curtop += obj.x; } return curtop; } function findPosY(obj) { var curtop = 0; if(obj.offsetParent){ while(obj.offsetParent){ curtop += obj.offsetTop; obj = obj.offsetParent; } }else if(obj.y){ curtop += obj.y; } return curtop; } function autocomplete(thevalue,e){ theObject = document.getElementById("autocompletediv"); theObject.style.visibility = "visible"; theObject.style.width = "152px"; var posx = 0; var posy = 0; posx = (findPosX(document.getElementById("lastname"))+1); posy = (findPosY(document.getElementById("lastname"))+23); theObject.style.left = posx + "px"; theObject.style.top = posx + "px"; var theextrachar = e.which; if(theextrachar == undefined) { theextrachar = e.keyCode } var objID = "autocompletediv"; if (theextrachar == { if(thevalue.length == 1) { var serverPage = "autocomp.php"; }else { var serverPage = "autocomp.php" + "?sstring=" + thevalue.substr(0,(thevalue.length -1)); } }else{ var serverPage = "autocomp.php" + "?sstring=" + thevalue + String.fromCharCode(theextrachar); } var obj = document.getElementById(objID); xmlhttp.open("GET",serverPage); xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ obj.innerHTML = xmlhttp.responsetext; } } xmlhttp.send(null); } function setvalue(thevalue, theid, hourly, monthly, availability, email, faxNumber, privatePhone, cellularPhone, businessPhone, webpage, city, postCode, adress, yearOfBirth ){ acObject = document.getElementById("autocompletediv"); acObject.style.visibility = "hidden"; acObject.style.height = "0px"; acObject.style.width = "0px"; /* fill out form */ document.getElementById("lastname").value = thevalue; document.getElementById("hiddenid").value = theid; document.getElementById("hourly").value = hourly; document.getElementById("monthly").value = monthly; document.getElementById("availability").value = availability; document.getElementById("email").value = email; document.getElementById("faxNumber").value = faxNumber; document.getElementById("privatePhone").value = privatePhone; document.getElementById("cellularPhone").value = cellularPhone; document.getElementById("businessPhone").value = businessPhone; document.getElementById("webpage").value = webpage; document.getElementById("city").value = city; document.getElementById("postCode").value = postCode; document.getElementById("adress").value = adress; document.getElementById("yearOfBirth").value = yearOfBirth; } function validateall() { try{ var isok; var tables; isok = false; tables = document.getElementsByTagName('input'); for (i=0; i<tables.length; i++)//loop through all the <td> elements { if (tables[i].className == "req") { if( tables[i].id == "hourly" ) { tables[i].value="0.0" //test } if( tables[i].id == "monthly" ) { } } } } catch(e) { alert( e.message); } return isok; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.