php and dynamicforms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
beki
Forum Newbie
Posts: 1
Joined: Tue Jul 19, 2005 4:21 am

php and dynamicforms

Post by beki »

Hi i need help with programming a dynamic datbase driven form. If you select something in the first form click on send then the second form choices change. I already did it with only php. But the forms are not user friendly. I takes to long about 5 a six steps before you get on the end of the form.
I know you can do it easily with Javascript or ASP. I tried it with Javascript but it doesn't work.

Here is an example of my code:

<?php
require('header.html');
//*Dit is programma met zoekformulier op basis van Model merk etc allen deze moet no
//worden verbeterd er gaat iets fout bij doorgeven van info
//

?>

<h1>Horloge zoeken op Merk, Model, Kast, Band,Uurwerk,Datum</h1>
<HTML>
<HEAD>
<META NAME="save" CONTENT="history">
<STYLE>
.saveHistory {behavior:url(#default#savehistory);}
</STYLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
v=false;
//-->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.1">
<!--
if (typeof(Option)+"" != "undefined") v=true;
//-->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">

function getFormNum (formName) {
var formNum =-1;
for (i=0;i<document.forms.length;i++){
tempForm = document.forms;
if (formName == tempForm) {
formNum = i;
break;
}
}
return formNum;
}

function jmp(form, elt) {
// The first parameter is a reference to the form.
if (form != null) {
with (form.elements[elt]) {
if (0 <= selectedIndex)
location = options[selectedIndex].value;
}
}
}

var catsIndex = -1;
var itemsIndex;

if (v) { // ns 2 fix
function newCat(){
catsIndex++;
a[catsIndex] = new Array();
itemsIndex = 0;
}

// Andrew chose to name this function "O", presumably standing
// for "Option". It's not a zero, here or in the array below!
function O(txt,url) {
a[catsIndex][itemsIndex]=new myOptions(txt,url);
itemsIndex++;
}

function myOptions(text,value){
this.text = text;
this.value = value;
}

// fill array
<?php
mysql_connect('localhost', 'foo','foo');
mysql_select_db("horloges");
// Get the Merk
$i = 0;
$make_query = "SELECT DISTINCT Merk FROM horloges";
$make_result = mysql_query($make_query);
while ($make_row = mysql_fetch_array($make_result)) {

$Merk[$i] = $make_row[0];
// Now fill the array with models for each merk
echo "newCat();\n";
$model_query = "SELECT DISTINCT Prijs FROM horloges WHERE (Merk = '$Merk[$i]') ORDER BY Prijs";
$model_result = mysql_query($model_query);
$num_results = mysql_num_rows($model_result);
$Prijs = array();
while(list($Prijs) = mysql_fetch_array($model_result)) {
echo "O(\"$Prijs\", \"/$Prijs.php\")\n";
}
echo "\n";
$i++;
}



for($j=0; $j<$num_results; $j++)
{
echo $Prijs[$j];
}
?>
} // close if (v)

function relate(formName,elementNum,j) {
if(v){
var formNum = getFormNum(formName);
if (formNum>=0) {
formNum++; // reference next form, assume it follows in HTML
with (document.forms[formNum].elements[elementNum]) {
for(i=options.length-1;i>0;i--) options = null; // null out in reverse order (bug workarnd)
for(i=0;i<a[j].length;i++){
options = new Option(a[j].text,a[j].value);
}
options[0].selected = true;
}
}
} else {
jmp(formName,elementNum);
}
}

// BACK BUTTON FIX for ie4+- or
// MEMORY-CACHE-STORING-ONLY-INDEX-AND-NOT-CONTENT
// see http://www.webreference.com for full comments
function IEsetup(){
if(!document.all) return;
IE5 = navigator.appVersion.indexOf("5.")!=-1;
if(!IE5) {
for (i=0;i<document.forms.length;i++) {
document.forms.reset();
}
}
}

window.onload = IEsetup;
//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#ffffff">

<CENTER>
<TABLE BGCOLOR="#DDCCFF" BORDER="0" CELLPADDING="8" CELLSPACING="0">
<TR VALIGN="TOP">
<TD>Choose a gender:<BR>
<FORM NAME="f1" METHOD="POST" ACTION="redirect.php" onSubmit="return false;">
<SELECT NAME="m1" ID="m1" CLASS=saveHistory onChange="relate(this.form,0,this.selectedIndex)">
<?php
while (list($key, $val) = each($Merk)) {
echo "<OPTION VALUE=\"/$val.php\">$val</OPTION>\n";
}
?>
</SELECT>
<INPUT TYPE=SUBMIT VALUE="Go" onClick="jmp(this.form,0);">
</FORM>
</TD>

<TD BGCOLOR="#FFFFFF" VALIGN=MIDDLE><B>---></B></TD>

<TD>Choose a model:<BR>
<FORM NAME="f2" METHOD="POST" ACTION="redirect.php" onSubmit="return false;">
<SELECT NAME="m2" ID="m2" CLASS=saveHistory onChange="jmp(this.form,0)">
// These are placeholder values for the first time the page is
// loaded. They will not change when the form values change.
// If you delete them, the forms will still work, but the
// second select menu would come up empty until changed.
// These values could be generated dynamically, but we wanted
// to show them in place.
<OPTION VALUE="/"></OPTION>
<OPTION VALUE="/"></OPTION>
<OPTION VALUE="/"></OPTION>
<OPTION VALUE="/"></OPTION>
</SELECT>
<INPUT TYPE=SUBMIT VALUE="Go" onClick="jmp(this.form,0);">
<INPUT TYPE="hidden" NAME="baseurl" VALUE="http://localhost">
</FORM>
</TD>
</TR>
</TABLE></CENTER>
<?php
for($j=0; $j<$num_results; $j++)
{
echo $Prijs[$j];
}
?>
</BODY>
</HTML>




Cn anyone help me please
Post Reply