i am using the code
<?php
include("config.php");
//postcode entry form for first use
if (!$postcode)
{
?>
<form action="<? printf($PHP_SELF); ?>" method="post">
Enter Postcode<br>
<input name="postcode" type="text" class="elementBlue" size="10">
<br>
<input name="Submit" type="submit" class="elementBlue" value="Submit">
</form>
<?
}
//if a postcode has been entered
if ($postcode)
{
//you can only pass 3 set variables into this function
//hence the globals.
function startElement($parser, $name, $attrs)
{
global $count;
global $postcode_array;
//checks to see if any addresses have been found
if (($name == "DATA") & ($attrs[ITEMS] == "0"))
{
printf("You have no results");
printf("<form action=\"manual_address.php\" method=\"post\">");
printf("<input name=\"Submit\" type=\"submit\" value=\"Manualy enter address\">");
printf("</form>");
}
//if an element is avalable
else if ($name == "ITEM")
{
$count++;
$postcode_array[$count][1] = $attrs[ID];
$postcode_array[$count][2] = $attrs[DESCRIPTION];
}
}
//this function is also "needed"
//but in this case not used.
function endElement($parser, $name)
{
printf("\n");
}
//this function is also "needed"
//but in this case not used.
function characterData($parser, $value)
{
printf("$value<BR>");
}
$simpleparser = xml_parser_create();
xml_set_element_handler($simpleparser, "startElement","endElement");
xml_set_character_data_handler($simpleparser,"characterData");
//cleaning up input
$postcode = trim($postcode);
$postcode = str_replace(" ","",$postcode);
$postcode = strtoupper($postcode);
//this is the query string that the XML is run from.
//to used this for other data sources just change the URL to another one or a file location.
$query = "http://services.postcodeanywhere.co.uk/xml.asp?action=lookup&account_code=$postcode_account_code&license_code=$postcode_license_code&postcode=$postcode";
if (!($fp = fopen($query, "r")))
{
die("could not open file/URL");
}
while ($data = fread($fp, 4096))
{
if (!xml_parse($simpleparser, $data, feof($fp)))
{
die(xml_error_string(xml_get_error_code($simpleparser)));
}
}
xml_parser_free($simpleparser);
//now we should have all the items in an array that we can use as normal PHP variables :o)
//the array will hold the following..
//$postcode_array[$foo][1] will return the id value for item array number $foo
//$postcode_array[$foo][2] will return the address value for item array number $foo
//I have represented this in a selection box below...
if ($count > 0)
{
?>
<form action="address_xml.php" method="post">
<select name="postcode_id" size="<? printf(count($postcode_array)); ?>" class="footer">
<?
$count2 = 0;
while ($count2 <= count($postcode_array))
{
$count2++;
?>
<option value="<? printf($postcode_array[$count2][1]); ?>" <? if ($count2 == 1) { echo " selected"; } ?>><? printf($postcode_array[$count2][2]); ?></option>
<?
}
?>
</select><br>
<input name="Submit" type="submit" class="elementBlue" value="Use selected address">
</form>
<form action="manual_address.php" method="post">
<input name="Submit" type="submit" class="elementBlue" value="Manualy enter address">
</form>
<?
}
}
?>
it works, but in my form page, having submitted the postcode i have:
and then the address at the end of the page
can anyone see why?