PDA

View Full Version : form error please help



ashuraj2163@yahoo.co.in
09-28-2005, 11:23 PM
code given below is not working properly please help me to solve javascript error in selection of listbox of form1. it is giving form1 is not defined error
__________________________________________________________________

<? session_start(); ?>
<? include_once('../include/mysql_connect.php'); ?>
<? ob_start(); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Product</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.dropcontent{
width: 410px;
height: 140px;
border: 1px solid black;
background-color: #FFECC6;
display:block;
}
</style>

<script type="text/javascript">
function check(a)
{
form1.txtCost.value=a;
}
/*
Combo-Box Viewer script- Created by and Dynamicdrive.com
Visit http://www.dynamicdrive.com/ for this script and more
This notice MUST stay intact for legal use
*/

if (document.getElementById){
document.write('<style type="text/css">\n')
document.write('.dropcontent{display:none;}\n')
document.write('</style>\n')
}

function contractall(){
if (document.getElementById){
var inc=0
while (document.getElementById("dropmsg"+inc)){
document.getElementById("dropmsg"+inc).style.display="none"
inc++
}
}
}

function expandone(){
if (document.getElementById){
var selectedItem=document.dropmsgform.dropmsgoption.selectedIndex
contractall()
document.getElementById("dropmsg"+selectedItem).style.display="block"
}
}

if (window.addEventListener)
window.addEventListener("load", expandone, false)
else if (window.attachEvent)
window.attachEvent("onload", expandone)

</script>
</head>
<body>
<div align="center"></div>
<p>&nbsp;</p>
<table border="1" align="center" cellpadding="1" cellspacing="1">
<tr>
<td><strong>Special Services </strong></td>
<td><strong>Per Minute Srvice </strong></td>
</tr>
<tr>
<td><table border="1" align="center" cellpadding="1" cellspacing="1">
<?

$query="select * from sservice ";
$result=mysql_query($query)or die(mysql_error."could not retrive information about Special Services");

while($row=mysql_fetch_assoc($result))
{

?>
<tr>
<td><? echo $row['servicetype']; ?></td>
<td><? echo $row['cost'] ?>$</td>
</tr>
<?
}
?>

</table></td>
<td><table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>0.49 cents Per Minute </td>
</tr>
<tr>
<td>Minimum 10 Minute Package </td>
</tr>
</table></td>
</tr>
</table>
<div align="center">
<form name="dropmsgform">
<select name="dropmsgoption" size="1" style="width:300" onChange="expandone()">
<option selected>Special Service</option>
<option>Per Minute Service</option>
<!--<option>What is DHTML?</option> -->
</select>
<br>

<div id="dropmsg0" class="dropcontent">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<form name="form1" method="post">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Select Your Product </td>
<td>

<? $query="select * from sservice ";
$result=mysql_query($query)or die(mysql_error."could not retrive information about Special Service");
?>

<select name="listServiceType" id="listServiceType" onChange='check(listServiceType.options[selectedIndex].value)'>
<?
while($row=mysql_fetch_assoc($result))
{

?>
<option value="<? echo $row['cost'] ?>"> <? echo $row['servicetype'] ?></option>
<?
$x=$row['cost'];
}
?>
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="left">Your Cost is </td>
<td align="left"><input name="txtCost" type="text" id="txtCost" size="6" maxlength="6" value=<? echo $x ?>>
$</td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="right"><input type="submit" name="Submit" value="Buy"></td>
<td>&nbsp;</td>
</tr>
</form>
</table>
</div>

<div id="dropmsg1" class="dropcontent">
Java is completely different from JavaScript- it's more powerful, more complex, and unfortunately, a lot harder to master. It belongs in the same league as C, C++, and other more complex languages. Java programs need to be compiled before they can run, while JavaScript do not.
</div>

<div id="dropmsg2" class="dropcontent">
DHTML is the embodiment of a combination of technologies- JavaScript, CSS, and HTML. Through them a new level of interactivity is possible for the end user experience.
</div>

</form>
</div>

<p>&nbsp;</p>
</body>
</html>

Twey
09-29-2005, 03:07 PM
Try giving the form an id and using document.getElementById("form1") instead.

mwinter
09-29-2005, 05:24 PM
code given below is not working properly please help me to solve javascript error in selection of listbox of form1.Generally, it's less than helpful to post server-side code when trying to diagnose problems with a client-side script (unless perhaps the code is generating that script dynamically). A link to an example, or the output of the server-side script, is more useful.

However, in this instance, the error is obvious.


function check(a)
{
form1.txtCost.value=a;
}Here you are assuming that elements with name or id attributes are available as global variables that match those values. That assumption is fundamentally flawed. Instead, use:


function check(value) {
document.forms.form1.elements.txtCost.value = value;
}


<select name="listServiceType" id="listServiceType" onChange='check(listServiceType.options[selectedIndex].value)'>You have done it here, too. You've even assumed that the selectedIndex property of the select element is in the scope chain.


<select name="listServiceType"
onchange="check(this.options[this.selectedIndex].value);">These are relatively common errors, but you'd do well to remember to avoid them in future.


<input name="txtCost" type="text" id="txtCost" [...]You can omit the id attribute (as I did above). You don't need it.



Try giving the form an id and using document.getElementById("form1") instead.There's nothing wrong with using an id attribute, but don't use the getElementById method. There's only one circumstance when it's better, necessary in fact, but that's not an issue here.

Mike