Page 1 of 2

Trouble printing description in text box field...

Posted: Fri Oct 28, 2005 8:31 am
by Curtis782
Would anyone be able to help me with this? I'm trying to print the description in the text box field (instead, I'm getting the part # in both places).

I was advised by one person to use the .selectedIndex as a pointer to an array that contains the descriptions. However, I'm not sure how to do this.

Script in action: http://70.84.139.154/~devsite/script_for_forum.php

Code: Select all

 
<html>
<head>
<title>Work Order System</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="JavaScript">
function showSelected1()
{
    var selObj = document.getElementById('partNo1');
    var txtValueObj = document.getElementById('partNo1');
    var txtTextObj = document.getElementById('desc1');
    var selIndex = selObj.selectedIndex; 
    
    txtTextObj.value = selObj.options[selIndex].text;
}
</script>
</head>
<body>
 
<?php
include ("common_vars.php");
 
mysql_connect("$DB_HOSTNAME1", "$DB_USERNAME1", "$DB_PASSWORD1") or die 
("Could not connect to Server".mysql_error());
mysql_select_db("$DB_NAME1") or die ("Unable to select database".mysql_error());
 
echo "<form name='add_form'>";
echo "<select name='partNo1' size='1' onChange='showSelected1()' class='style10'>";
echo"<option selected value='' class='style10'>&nbsp;</option>";
$query = "SELECT * from sunparts";
$result = mysql_query($query);
while ($item = mysql_fetch_array($result))
{
    $partnum=$item["partnum"];
    $description=$item["description"];
    echo"<option value='$partnum' class='style10'>$partnum</option>";
}
echo "</select>";
 
echo "&nbsp;<input name='desc1' type='text' class='style10' id='desc1' size='40'>";
 
echo "</form>";
 
?>
</body>
</html>
 

Posted: Fri Oct 28, 2005 11:17 am
by Chris Corbyn
I didn't read any further than this:

Code: Select all

function showSelected1()
{
    var selObj = document.getElementById('partNo1');     //selObj is the partNo's
    var txtValueObj = document.getElementById('partNo1');   //Why????
    var txtTextObj = document.getElementById('desc1');   //OK but you have't used it
    var selIndex = selObj.selectedIndex;   //The option number that's selected in the partNo's
    
    txtTextObj.value = selObj.options[selIndex].text;   //Write the part No text to the textObj
}
It's doing exactly as expected :)

I can't see enough code to figure out exactly what the intention is though... where's the description actually stored?

Posted: Fri Oct 28, 2005 11:22 am
by Curtis782
Please see script in action URL, if you have a moment... producing part #'s in both places.

Here is my table structure (1st 10 entries):

Code: Select all

CREATE TABLE sunparts (id int(4) NOT NULL auto_increment, partnum varchar(50), description varchar(200), PRIMARY KEY (id)) TYPE=MyISAM;

INSERT INTO sunparts VALUES (1, '827', '20GB 4 mm Tape');
INSERT INTO sunparts VALUES (2, '834', '10GB 8 mm Tape');
INSERT INTO sunparts VALUES (3, '844', '14GB 8 mm Tape');
INSERT INTO sunparts VALUES (4, '6051', '20-40GB 1/2 Tape, 50-50 Pin Cable');
INSERT INTO sunparts VALUES (5, '6052', '20-40GB 1/2 Tape, 50-68 Pin Cable');
INSERT INTO sunparts VALUES (6, '6057', '20-40GB DLT4000, 68-68 Pin Cable, Light Grey');
INSERT INTO sunparts VALUES (7, '6058', '20-40GB DLT4000, 50-68 Pin Cable, Light Grey');
INSERT INTO sunparts VALUES (8, '6059', '20-40GB DLT4000, 68-68 Pin Cable, Medium Grey');
INSERT INTO sunparts VALUES (9, '6060', '35-70GB DLT7000, 68-68 Pin Cable, Light Grey');
INSERT INTO sunparts VALUES (10, '6061', '35-70GB DLT7000, 50-68 Pin Cable, Light Grey');

Posted: Fri Oct 28, 2005 11:31 am
by Chris Corbyn
It does nothing at all in FF :?

Can I just clarify what exactly *should* happen.

If I choose "910" from the drop down should "910" appear in the text box or should some description matching the partNo 910 go in there? There's nothing in the JS to allow the Latter.

Posted: Fri Oct 28, 2005 11:33 am
by Chris Corbyn
Maybe you mean that the text in the drop down itself is just the partNum?

Code: Select all

echo"<option value='$partnum' class='style10'>$partnum</option>";
That's why...

Change that to:

Code: Select all

echo"<option value='$partnum' class='style10'>$description</option>";

Posted: Fri Oct 28, 2005 12:05 pm
by Curtis782
This is what I want to happen...

The pull-down menu is for part numbers only. I want the value and text to be similar: <option value='$partnum class='style10'>$partnum</option>

When a user selects an option from the part # pull-down, I'd like the related description to be printed in the text box (and also to be the text box value).

I thought this would be easy to do (in reference to the mysql table structure) because each part # and description share the same ID. I've been struggling with this one for a week however. :?

Posted: Fri Oct 28, 2005 12:15 pm
by cspatter
Preload the values onto the page in variables (unless too many) then ref them using JS.

Make sense.. If not I can try to clarify.

Posted: Fri Oct 28, 2005 12:20 pm
by Curtis782
I'm an entry-level programmer so am unfortunately not too familiar with these concepts.

Posted: Fri Oct 28, 2005 12:42 pm
by Curtis782
If anyone may be willing to finish this script off for me I'd be willing to exchange services if you could use some web design or search engine optimization help. Please contact me beforehand.

Posted: Fri Oct 28, 2005 12:43 pm
by cspatter
$description=$item["description"];
this part of your code is in a loop but you are not doing anything with it. It is being over written each time the loop itterates. so you end up with only one description stored in a variable (the last one). What you need to do is create a (variable variable) ${'$partnum'} = $description; to store the descriptions in then you a javascript that will change the text field to what you want when the option is selected.

I dont have any exact syntax i can send you but i hope that helps. If not maybe i can point you in a better direction.

Posted: Fri Oct 28, 2005 12:54 pm
by Chris Corbyn
Curtis782 wrote:If anyone may be willing to finish this script off for me I'd be willing to exchange services if you could use some web design or search engine optimization help. curtiscarmichael@gmail.com
it's not worth paying for mate ;) It's just one extra step 9along with a correction) in your code.

What you need to do is to pull the stuff out of the database like you already are, then for each description, get PHP to write a line of JavaScript that list them as JavaScript array values.

Code: Select all

 
<html>
<head>
<title>Work Order System</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="JavaScript">
function showSelected1()
{
    var selObj = document.getElementById('partNo1');
    var txtTextObj = document.getElementById('desc1');
    var selIndex = selObj.selectedIndex;
    
    txtTextObj.value = Descriptions[selObj.options[selIndex].value];
}
</script>
</head>
<body>
 
<?php
include ("common_vars.php");
 
mysql_connect("$DB_HOSTNAME1", "$DB_USERNAME1", "$DB_PASSWORD1") or die
("Could not connect to Server".mysql_error());
mysql_select_db("$DB_NAME1") or die ("Unable to select database".mysql_error());
 
echo "<form name='add_form'>";
echo "<select name='partNo1' size='1' onChange='showSelected1()' class='style10'>";
echo"<option selected value='' class='style10'>&nbsp;</option>";
$query = "SELECT * from sunparts";
$result = mysql_query($query);
$description = array();
while ($item = mysql_fetch_array($result))
{
    $partnum=$item["partnum"];
    $description[$item['partnum']] =$item["description"];
    echo"<option value='$partnum' class='style10'>$partnum</option>";
}
echo "</select>";
 
echo "&nbsp;<input name='desc1' type='text' class='style10' id='desc1' size='40'>";
 
echo "</form>";
 
echo '<script type="text/javascript">'."\n";
var Descriptions = new Array();
foreach ($description as $key => $value)
{
    echo "Descriptions[\'$key\'] = \'$value\'\n";
}
echo '</script>';
 
?>
</body>
</html>
 

Posted: Fri Oct 28, 2005 1:14 pm
by Curtis782
Thank you :-)

The only thing is I got a parse error:
unexpected T_VAR on line 44

Code: Select all

var Descriptions = new Array();

Posted: Sat Oct 29, 2005 12:29 am
by Curtis782
anyone? :?

Posted: Sat Oct 29, 2005 2:29 am
by n00b Saibot
uh oh! d11wtq has a bug there :lol:

Code: Select all

echo '<script type="text/javascript">'."\n"; 
var Descriptions = new Array();
should be

Code: Select all

echo "<script type=\"text/javascript\">
var Descriptions = new Array();\n";
;)

Posted: Sat Oct 29, 2005 10:36 am
by Curtis782
Thank you n00b Saibot.

Unfortunately when I run the script d11wtq gave me with your bug fix the script does not work:
http://70.84.139.154/~devsite/test_script.php

When a user selects a part # (from pull-down menu), I'd like for the description to print in the text box.