Trouble printing description in text box field...

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

Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Trouble printing description in text box field...

Post 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>
 
Last edited by Curtis782 on Fri Jan 25, 2008 5:21 pm, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post 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');
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>";
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post 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. :?
cspatter
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2005 1:28 pm

Post 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.
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post by Curtis782 »

I'm an entry-level programmer so am unfortunately not too familiar with these concepts.
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post 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.
Last edited by Curtis782 on Fri Oct 28, 2005 1:15 pm, edited 1 time in total.
cspatter
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2005 1:28 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
 
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post 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();
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post by Curtis782 »

anyone? :?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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";
;)
Curtis782
Forum Commoner
Posts: 31
Joined: Tue Oct 25, 2005 3:34 pm

Post 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.
Post Reply