Jump to content

Multiple requests


bradynorman

Recommended Posts

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

 

 

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.