hello guys im new here so please be kind with me, please be patience cause Im new in PHP.
now here's my problem I really miss-up my code. I want to create a form with 4 dropdownlist (diagnosis, cause, medicine, and treatment) this drop-downs are dynamic taking there items from my database... cause, medicine, and treatment items are dependent on diagnosis value..

and in diagnosis there is a static item which is other with a value of other. now I want a textbox to appear every time other is selected in my diagnosis dropdown....

//by: lifeworks

<SCRIPT language=JavaScript>
window.onload = initForm;
 
function initForm(){
	document.getElementById('selectedDiag').selectedIndex = 0;
	document.getElementById('selectedDiag').onchange = showHideBox;
}
 
function showHideBox(){
 
	var whichForm = document.getElementById('selectedDiag');
	var theForm = whichForm.options[whichForm.selectedIndex].value;
	if(whichForm.options[whichForm.selectedIndex].value == 'other'){
		document.getElementById('selectedOther').style.display='';
	}
	else{
		document.getElementById('selectedOther').style.display='none';
	}
 
}

</script>

this is for the textbox to appear...

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.selectedDiag.options[form.selectedDiag.options.selectedIndex].value;
self.location='updateMedicalRecords.php?selectedDiag=' + val ;
}  	

</script>

this code does not functional since I put function showHideBox()

echo "<form id=f1 method=post name=f1 action=''>";

echo "<select id='selectedDiag' name='selectedDiag' onchange=\"reload(this.form) && showHideBox()\"><option value=''>Select diagnosis</option>"."<option value='other'>other</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['diagnosisId']==@$diag){echo "<option selected value='$noticia2[diagnosisId]'>$noticia2[diagnosisName]</option>"." "."<BR>";}

 else {
	echo "<option value=$noticia2[diagnosisId]>$noticia2[diagnosisName]</option>";
}
}

echo "</select>";

//////////        Starting of second drop downlist /////////

echo "<select name='cause'>
<option value=''>Select cause</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[causeId]'>$noticia[cause]</option>";
}
echo "</select>";

//////////        Starting of third drop downlist /////////
echo "<select name='medicine'>
<option value=''>Select medicine</option>";
while($noticiamed = mysql_fetch_array($mediquery)) { 
echo  "<option value='$noticiamed[medicineId]'>$noticiamed[medicineName]</option>";
}
echo "</select>";

echo "<input type=submit value=Submit>";

echo "</form>";

and also the third dropdown has no item appearing, I dont know if its in my PHP code of in query..

<input name='txt' type='text' id='selectedOther' style='display:none' />

please help me guys.. and more power

Hi!

I've read that you are just starting to learn PHP! Good :)

It makes it easier to read your code if you have a few coding guidelines:

>> Indent and comment your code, keeping statements and function calles seperated and clarified:

/* Doing something: */
doSomething();

/* Doing something else: */
doSomethingElse();

/* Indicates whether something needs to be shown: */
$shown = true;

/* Only if it needs to be shown: */
if ($shown == true) {
     
     /* Showing something: */
     echo "Something";
     
}

/* Doing a loop: */
while ($a == 0) {

     //.... Code block ....

}

// I hope you get the picture by now,
// because typing this isn't really helping you at all with your problem...

>> Also, you might want to read up on how to properly write HTML (watch the use of quotes and ';'):

<img src='myimage.png' border='0' onclick='doSomething("StringArgument");' />

And as a final check, you can always go to the guys that design HTML: http://validator.w3.org/

>> Oh and one last thing, don't use old syntax for your HTML:

<script type='text/javascript'>
function reload(form)
{
  var val = form.selectedDiag.options[form.selectedDiag.options.selectedIndex].value;
  self.location='updateMedicalRecords.php?selectedDiag=' + val ;
}  
</script>

I think you will find your answer when you've cleaned up your code.

~G

sir Graphix thank you for your response and tips..
I dont know if this is right but I just replace the quotes with ' when Im coding HTML inside <?php tag. I really mess-up this one big time Im working for this almost one month and I cant find the answer. Is there a way that I can write those functions combined? I'm into cleaning up my code but those javascript giving me a hard time. xp

In my opinion, the three javascript function initForm and showHideBox are unnessecary. When the page reloads, the option that is chosen is already selected (so initForm is obsolete) and using a simple if condition, you can render showHideBox obsolete too!

I'il explain with some code:

//
// As you have not given all your code I assume a few things:
//    > $diag is the ID of the selected diagnostic ($_GET['selectedDiag'])
//    > The query $quer2 retrieves all the diagnostic dropdown values
//    > The query $quer retrieves all causes WHERE diagnosticId = $diag (and same with $mediquery)


/* Opening form: */
echo "<form method='post' name='f1' action=''>";

/* Opening diagnostic dropdown with default option: */
echo "
<select id='selectedDiag' name='selectedDiag' onchange='reload(this.form)'>
<option value=''>Select diagnosis</option>
";

while($noticia2 = mysql_fetch_array($quer2)) { 
	if($noticia2['diagnosisId'] == $diag) {
	  echo "<option selected='selected' value='".$noticia2['diagnosisId']."'>".$noticia2['diagnosisName']."</option>";
	} else {
	  echo "<option value='".$noticia2['diagnosisId']."'>".$noticia2['diagnosisName']."</option>";
	}
}

/* Closing diagnostic dropdown with 'other' option: */
echo "<option value='other'>Other</option>
</select>";

/* Only if the diagnostic is set: */
if (isset($diag)) {

/******************************************************************************
 * I'il let you correct this part yourself: 
*/
	echo "<select name='cause'>
	<option value=''>Select cause</option>";
	while($noticia = mysql_fetch_array($quer)) { 
	echo  "<option value='$noticia[causeId]'>$noticia[cause]</option>";
	}
	echo "</select>";

	//////////        Starting of third drop downlist /////////
	echo "<select name='medicine'>
	<option value=''>Select medicine</option>";
	while($noticiamed = mysql_fetch_array($mediquery)) { 
	echo  "<option value='$noticiamed[medicineId]'>$noticiamed[medicineName]</option>";
	}
	echo "</select>";

/*********************************************************************************/

}

/* Showing submit button: */
echo "<input type='submit' name='submit_button' value='Submit'>";

/* Closing form: */
echo "</form>";

I think this will help you make the four dropdown work.

~G

Sir Graphix;
thank you for this wonderful post.
and all your guess bout my code is correct, how did you do that?
now the four dynamic dropdownlist is solve,

here's the code I come up with

function reload(form) {
		var val=form.selectedDiag.options[form.selectedDiag.options.selectedIndex].value;
		if (val == 'other'){
		document.getElementById('otherDiag').style.display='';
		document.getElementById('otherCause').style.display='';
		document.getElementById('otherMedicine').style.display='';
		document.getElementById('otherTreat').style.display='';

		} else {
			<!--var val=form.selectedDiag.options[form.selectedDiag.options.selectedIndex].value;-->
			self.location='updateMedicalRecords.php?studentsId=<?php echo $getstudentsId; ?>&selectedDiag=' + val ;
			}
}

I just play with it, and I need to modify the script for I need to put other option in every drop down and a corresponding textbox..

and bye the way sir what is the proper way of putting the selected value from drop down list into php variable, and insert it to database?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.