kwstephenchan Posted January 7, 2009 Share Posted January 7, 2009 Hi all. I intend to have an input field in PHP program 1. Besides this input field, I have a button so that the user can search for value to be used instead of inputing. On pressing this button, another php program, say program 2 will be called. Program 2 will go to the database and list the values for selection. The user can then select and the selected value will be passed back to program 1 (without refreshing other fields in program 1) Can anyone advise how this can be done? I tried using Ajax, but it does not seem to be doing the trick, wonder what have I done wrong. Thanks Followings are the code bit: ***** javascript definition **** <script type="text/javascript"> var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHttp"); } function getData(srcPHP, parm) { var url = srcPHP+"?parm=" + parm; if(XMLHttpRequestObject) { XMLHttpRequestObject.open("GET", url, true); XMLHttpRequestObject.onreadystatechange = function() { if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { eval(XMLHttpRequestObject.responseText); } } XMLHttpRequestObject.send(null); } } function alerter(divID,pvalue) { alert(' back toJavaScript'); var showDiv = document.getElementById(divID); showDiv.innerHTML = pvalue; } </script> **** parts of program calling the Javascript **** <tr valign="baseline"> <div width="40%" height="20" align="right" class="lbtxt">Owner : </div> <div height="20" class="intxt"> <input name="m_ownerid" type="text" id="m_ownerid" size="4"> <font color="#ffffff"><?php $divID="m_ownerid"; $srcData="data.php"; $str="<img src='images/moredot.gif'>"; echo '<a href=javascript:getData(\''.$srcData.'\',\''.$divID.'\') >'.$str.'</a>'; ?>*</font></div> </tr> ***** code content in data.php **** <?php $parm = $_GET['parm']; $pvalue="passing value"; echo 'alerter(\''.$parm.'\',\''.$pvalue.'\')'; // echo '<a href=javascript:getData(\''.$srcData.'\',\''.$divID.'\') >'.$str.'</a>'; ?> <?php /* <html> <body> <table> <tr> <td> this is the html part </td> </tr> </table> </body> </html> */ ?> As noted, I have commented out the html part because if I leave them in, I'll get page error. As I am new to Ajax, wonder if it is true that the second PHP program cannot have HTML in it. Thanks for all the help. 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.