bradynorman Posted September 5, 2008 Share Posted September 5, 2008 Hi, I'm a newbie to AJAX but i thought that i would attempt to incorporate a little into a web-site that i'm developing. Well after just a few hours i'm like WOW i'm hooked. Anywho - i would like to develop the following page a little further but am stuck with making multiple requests to different php pages that are retreiving sql data for me. PLEASE if anyone can offer any assistance iwould be eternally grateful - i have incidentally been browsing the forums for an answer...... In the header of my page ship_single.php <script src="showSingleAdr.js"></script> details are: var xmlHttp function showSingleAdr(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="ajax_single_address.php" url=url+"?postcode="+str url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("Address").innerHTML=xmlHttp.responseText } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } The following <input name="postcode" type="text" id="postcode" onblur="showSingleAdr(this.value)" value="<?php echo $_GET['Postcode']; ?>" /> updates <div id="Address"></div> with data retrieved from the ensuing mysql query. I'm happy with that perhaps the coding isn't perfect though..... On the same form i now want to use AJAX to retreive a stock quantity - the coding is similar: <script src="showSingleAdr.js"></script> the details of this file.... var xmlHttp function showStock(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="ajax_stock.php" url=url+"?pn="+str url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("Stock").innerHTML=xmlHttp.responseText } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } and the coding to select this is <select name="pn" id="pn" onchange="showStock(this.value)" > <option>Select Part</option> <?php mysql_select_db($database_grcdb, $grcdb); $query="SELECT pn FROM parts ORDER BY pn ASC"; $result=mysql_query($query); while(list($pn)=mysql_fetch_row($result)) { echo "<option value=\"".$pn."\">".$pn."</option>"; } ?> </select> results sent to <div id="Stock"></div> Individually these work, however together they fall over..... Where am i going wrong???? thanks in advance cheers Mike Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 5, 2008 Share Posted September 5, 2008 Uh... you can't re-defined functions in JavaScript. And why are you using the same variables? Quote Link to comment Share on other sites More sharing options...
bradynorman Posted September 5, 2008 Author Share Posted September 5, 2008 The variables are different? pn and postcode - at least that's what i'm aiming for.... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 5, 2008 Share Posted September 5, 2008 Instead of slapping together 2 scripts, why not use one script that sends off multiple requests? Quote Link to comment Share on other sites More sharing options...
bradynorman Posted September 5, 2008 Author Share Posted September 5, 2008 Thank you Ken, As i mentioned i am a complete newbie to AJAX and all suggestions are welcomed. The requests would not be made at the same time - is this a problem? cheers Mike Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 5, 2008 Share Posted September 5, 2008 Thank you Ken, As i mentioned i am a complete newbie to AJAX and all suggestions are welcomed. The requests would not be made at the same time - is this a problem? cheers Mike No. I would use 2 variables: xmlHttp and AJAX. And then you can re-use functions, so go for it. Quote Link to comment Share on other sites More sharing options...
bradynorman Posted September 5, 2008 Author Share Posted September 5, 2008 can you offer me an example as to how to achieve this 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.