Hi experts,
I have a table of postcodes and I am wanting to get the user to select the city, state postcode from a combo box (displayed like city, state, postcode) and have it populate the next two text fields of "State" and "Postcode". I am looking for something like an onchange function to fill the boxes.
The postcode data comes from tblPostcode
Thanks for your help.
the code is ...
enquiry_dml.php
<?php
function insert()
{
global $HTTP_SERVER_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_POST_FILES, $Translation;
if(get_magic_quotes_gpc())
{
$enqDateStamp = $HTTP_POST_VARS["enqDateSt
amp"];
$enqUsername = $HTTP_POST_VARS["enqUserna
me"];
$city = $HTTP_POST_VARS["city"];
$state = $HTTP_POST_VARS["state"];
$postcode = $HTTP_POST_VARS["postcode"
];
}
else
{
$enqDateStamp = addslashes($HTTP_POST_VARS
["enqDateS
tamp"]);
$enqUsername = addslashes($HTTP_POST_VARS
["enqUsern
ame"]);
$city = addslashes($HTTP_POST_VARS
["city"]);
$state = addslashes($HTTP_POST_VARS
["state"])
;
$postcode = addslashes($HTTP_POST_VARS
["postcode
"]);
}
sql("insert into tblEnquiries (city, state, postcode) values (" . (($city != "") ? "'$city'" : "NULL") . ", " . (($state != "") ? "'$state'" : "NULL") . ", " . (($postcode != "") ? "'$postcode'" : "NULL") . ")");
return mysql_insert_id();
}
function update($selected_id)
{
global $HTTP_SERVER_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS, $Translation;
if(get_magic_quotes_gpc())
{
$enqDateStamp = $HTTP_POST_VARS["enqDateSt
amp"];
$enqUsername = $HTTP_POST_VARS["enqUserna
me"];
$city = $HTTP_POST_VARS["city"];
$state = $HTTP_POST_VARS["state"];
$postcode = $HTTP_POST_VARS["postcode"
];
}
else
{
$enqDateStamp = addslashes($HTTP_POST_VARS
["enqDateS
tamp"]);
$enqUsername = addslashes($HTTP_POST_VARS
["enqUsern
ame"]);
$city = addslashes($HTTP_POST_VARS
["city"]);
$state = addslashes($HTTP_POST_VARS
["state"])
;
$postcode = addslashes($HTTP_POST_VARS
["postcode
"]);
}
sql("update tblEnquiries set " . "city=" . (($city != "") ? "'$city'" : "NULL") . ", " . "state=" . (($state != "") ? "'$state'" : "NULL") . ", " . "postcode=" . (($postcode != "") ? "'$postcode'" : "NULL") . " where enquiries_id='$selected_id
'");
}
function form($selected_id = "", $AllowUpdate = 1, $AllowInsert = 1, $AllowDelete = 1)
{
// function to return an editable form for a table records
// and fill it with data of record whose ID is $selected_id. If $selected_id
// is empty, an empty form is shown, with only an 'Add New'
// button displayed.
global $Translation;
if(!$AllowInsert && $selected_id==""){ return ""; }
// combobox: city
$combo_city = new DataCombo;
$combo_city->Query = "select postcodes_id, city, state, postcode from tblPostcodes order by city";
$combo_city->SelectName = "city";
if($selected_id)
{
$res = sql("select * from tblEnquiries where enquiries_id='$selected_id
'");
$row = mysql_fetch_array($res);
$combo_state->SelectedData
= $row["state"];
$combo_postcode->SelectedD
ata = $row["postcode"];
}
$combo_city->Render();
$combo_state->Render();
$combo_postcode->Render();
// code for template based detail view forms
// open the detail view template
$templateCode=@implode('',
@file('./tblEnquiries_temp
lateDV.htm
l'));
// process form title
$templateCode=str_replace(
'<%%DETAIL
_VIEW_TITL
E%%>', 'Enquiries detailed view', $templateCode);
// process buttons
if($AllowInsert){
$templateCode=str_replace(
'<%%INSERT
_BUTTON%%>
', "<input type=image src=insert.gif name=insert alt='" . $Translation['add new record'] . "'>", $templateCode);
}else{
$templateCode=str_replace(
'<%%INSERT
_BUTTON%%>
', '', $templateCode);
}
if($selected_id){
if($AllowUpdate){
$templateCode=str_replace(
'<%%UPDATE
_BUTTON%%>
', "<input type=image src=update.gif vspace=1 name=update alt='" . $Translation["update record"] . "'>", $templateCode);
}else{
$templateCode=str_replace(
'<%%UPDATE
_BUTTON%%>
', '', $templateCode);
}
if($AllowDelete){
$templateCode=str_replace(
'<%%DELETE
_BUTTON%%>
', "<input type=image src=delete.gif vspace=1 name=delete alt='" . $Translation['delete record'] . "' onClick=\"return confirm('" . $Translation['are you sure?'] . "');\">", $templateCode);
}else{
$templateCode=str_replace(
'<%%DELETE
_BUTTON%%>
', '', $templateCode);
}
$templateCode=str_replace(
'<%%DESELE
CT_BUTTON%
%>', "<input type=image src=deselect.gif vspace=1 name=deselect alt='" . $Translation['deselect record'] . "'>", $templateCode);
}else{
$templateCode=str_replace(
'<%%UPDATE
_BUTTON%%>
', '', $templateCode);
$templateCode=str_replace(
'<%%DELETE
_BUTTON%%>
', '', $templateCode);
$templateCode=str_replace(
'<%%DESELE
CT_BUTTON%
%>', '', $templateCode);
}
// process combos
$templateCode=str_replace(
'<%%COMBO(
city)%%>',
$combo_city->HTML, $templateCode);
$templateCode=str_replace(
'<%%COMBOT
EXT(city)%
%>', $combo_city->MatchText, $templateCode);
$templateCode=str_replace(
'<%%COMBO(
state)%%>'
, $combo_state->HTML, $templateCode);
$templateCode=str_replace(
'<%%COMBOT
EXT(state)
%%>', $combo_state->MatchText, $templateCode);
$templateCode=str_replace(
'<%%COMBO(
postcode)%
%>', $combo_postcode->HTML, $templateCode);
$templateCode=str_replace(
'<%%COMBOT
EXT(postco
de)%%>', $combo_postcode->MatchText
, $templateCode);
// process images
$templateCode=str_replace(
'<%%UPLOAD
FILE(enqui
ries_id)%%
>', '', $templateCode);
$templateCode=str_replace(
'<%%UPLOAD
FILE(enqDa
teStamp)%%
>', '', $templateCode);
$templateCode=str_replace(
'<%%UPLOAD
FILE(enqUs
ername)%%>
', '', $templateCode);
$templateCode=str_replace(
'<%%UPLOAD
FILE(city)
%%>', '', $templateCode);
$templateCode=str_replace(
'<%%UPLOAD
FILE(state
)%%>', '', $templateCode);
$templateCode=str_replace(
'<%%UPLOAD
FILE(postc
ode)%%>', '', $templateCode);
// process values
if($selected_id){
$templateCode=str_replace(
'<%%VALUE(
enquiries_
id)%%>', htmlspecialchars($row['enq
uiries_id'
], ENT_QUOTES), $templateCode);
$templateCode=str_replace(
'<%%VALUE(
enqDateSta
mp)%%>', htmlspecialchars($row['enq
DateStamp'
], ENT_QUOTES), $templateCode);
$templateCode=str_replace(
'<%%VALUE(
city)%%>',
htmlspecialchars($row['cit
y'], ENT_QUOTES), $templateCode);
$templateCode=str_replace(
'<%%VALUE(
state)%%>'
, htmlspecialchars($row['sta
te'], ENT_QUOTES), $templateCode);
$templateCode=str_replace(
'<%%VALUE(
postcode)%
%>', htmlspecialchars($row['pos
tcode'], ENT_QUOTES), $templateCode);
}else{
$templateCode=str_replace(
'<%%VALUE(
enquiries_
id)%%>', '', $templateCode);
$templateCode=str_replace(
'<%%VALUE(
enqDateSta
mp)%%>', '', $templateCode);
$templateCode=str_replace(
'<%%VALUE(
city)%%>',
'', $templateCode);
$templateCode=str_replace(
'<%%VALUE(
state)%%>'
, '', $templateCode);
$templateCode=str_replace(
'<%%VALUE(
postcode)%
%>', '', $templateCode);
}
// process translations
foreach($Translation as $symbol=>$trans){
$templateCode=str_replace(
"<%%TRANSL
ATION($sym
bol)%%>", $trans, $templateCode);
}
// finally, clear scrap
$templateCode=str_replace(
'<%%', '<!--', $templateCode);
$templateCode=str_replace(
'%%>', '-->', $templateCode);
return $templateCode;
}
?>
enquiry_form.html
<br>
<table border=1 bordercolor=navy cellpadding=0 cellspacing=0>
<tr>
<td>
<div class=TableTitle><%%DETAIL
_VIEW_TITL
E%%></div>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td colspan=2></td>
<td rowspan=6 valign=top>
<div><%%UPDATE_BUTTON%%></
div>
<div><%%DELETE_BUTTON%%></
div>
<div><%%DESELECT_BUTTON%%>
</div>
<br>
<div><%%INSERT_BUTTON%%></
div>
</td>
</tr>
<tr>
<td class=TableHeader valign=top>
<div class=TableHeader style="text-align:right;">
City / suburb</div>
</td>
<td class=TableBody width=300><%%COMBO(city)%%
> &nb
sp;</td>
</tr>
<tr>
<td class=TableHeader valign=top>
<div class=TableHeader style="text-align:right;">
State / territory</div>
</td>
<td class=TableBody width=300><input size=50 type=text class=TextBox name=city value="<%%VALUE(state)%%>"
> &nb
sp;</td>
</tr>
<tr>
<td class=TableHeader valign=top>
<div class=TableHeader style="text-align:right;">
Postcode</
div>
</td>
<td class=TableBody width=300><input size=50 type=text class=TextBox name=postcode value="<%%VALUE(postcode)%
%>">
</td
>
</tr>
</table>
</td>
</tr>
</table>