Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2012, 07:47 PM   PM User | #1
howard-moore
Regular Coder

 
Join Date: May 2008
Posts: 114
Thanks: 13
Thanked 0 Times in 0 Posts
howard-moore is an unknown quantity at this point
Fill two fields from one drop-list

Hi All,

I have a bit of a tricky PHP/MySQL/JavaScript issue that I hope someone can help me with. I have a dynamically created drop-list (select) as follows:

Code:
<?php
	$sql = "SELECT * FROM `$filename` ORDER BY parent_title ASC";
	$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
	while ($row = mysql_fetch_assoc($sql_result)) {
		if ($row["parent_title"]<>'') {
			echo '<option value="';
			echo nl2br(stripslashes(utf8_decode($row["parent_code"]))).'">';
			echo nl2br(stripslashes(utf8_decode($row["parent_title"]))).'</option>';			
		};
	};
?>
There are two fields here: 'parent_code' and 'parent_title'. What I want to do is have it so that when 'parent_title' is selected (which is what is shown because it is a more user-friendly text entry) it fills in a form field for 'parent_code' AND 'parent_title'. I presume that this is possible using JS, but I confess that I don't know how it can be done. Can anyone give me any pointers?

Thanks,
Neil
howard-moore is offline   Reply With Quote
Old 12-29-2012, 08:37 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
so the code is the value and the title is the option text?

Code:
<body>
<form>
<select onchange="fillFields(this)">
<option value="">select</option>
<option value="3869">gone with the wind</option>
<option value="3482">fight club</option>
</select>
<input name="code"/><input name="title"/> 
</form>
<script type='text/javascript'>
function fillFields(sel){
sel.form.code.value=sel.value;
var txt=sel.value==""?"":sel.options[sel.selectedIndex].text;
sel.form.title.value=txt;
}
</script>
</body>
xelawho is offline   Reply With Quote
Old 12-30-2012, 02:07 PM   PM User | #3
howard-moore
Regular Coder

 
Join Date: May 2008
Posts: 114
Thanks: 13
Thanked 0 Times in 0 Posts
howard-moore is an unknown quantity at this point
Hi,

That's right, but I need to save both fields, and the plan is that when the user selects the text, it also populates the code field. I'll have a play with what you suggest and see how it goes - huge thanks for the help!

Neil
howard-moore is offline   Reply With Quote
Old 12-30-2012, 02:57 PM   PM User | #4
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,355
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<body>
<script type='text/javascript'>

var ary=[
 [['code',''],['title','']],
 [['code','23'],['title','123']],
 [['code','323'],['title','423']]
];

function fillFields(sel,ary){
 var i=sel.selectedIndex,ary=ary[i];
 sel.form.code.value=sel.value;
 if (ary){
  for (var z0=0;z0<ary.length;z0++){
   if (ary[z0]&&sel.form[ary[z0][0]]){
    sel.form[ary[z0][0]].value=ary[z0][1]||'';
   }
  }
 }
}

function fillFields1(sel,ary){
 var s=sel.value.split(':'),args=fillFields1.arguments;
 for (var z0=1;z0<args.length;z0++){
  if (sel.form[args[z0]]){
   sel.form[args[z0]].value=s[z0-1]||'';
  }
 }
}

</script>
<form>
<select onchange="fillFields(this,ary);">
<option value="">select</option>
<option value="3869">gone with the wind</option>
<option value="3482">fight club</option>
</select>
<input name="code"/><input name="title"/>
<br />
<select onchange="fillFields1(this,'code1','title1');">
<option value="">select</option>
<option value="38:69">gone with the wind</option>
<option value="34:82">fight club</option>
</select>
<input name="code1"/><input name="title1"/>

</form>
</body>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:44 AM.


Advertisement
Log in to turn off these ads.