Concatenate two html fields into one |
![]() ![]() |
Concatenate two html fields into one |
Mar 3 2008, 02:05 PM
Post
#1
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
last, first The Field Name is lastfirst. For display purposes, I can ask for Last_Name or First_Name and it will derive the Last Name or the First Name from the lastfirst field. Unfortunately on the input screens you must put the names in as "last, first" which can get annoying. I would like to find a way to concatenate the two fields on the fly for input. So I want to have it display text boxes for the following: First_Name Last_Name But if the form is submitted with changes to either field, it concatenates the incoming information into the necessary "last, first" format for entry into the database. Mine CODE <input type="text" name="[01]First_Name" value="" size="15"> <input type="text" name="[01]Last_Name" value="" size="15"> Theirs CODE <input type="text" name="[01]lastfirst" value="" size="30"> Make any sense? |
|
|
Mar 3 2008, 02:58 PM
Post
#2
|
|
|
Spyware Veteran Posts: 20,195 From: Netherlands OS: XP Pro & Vista Ultimate |
You want two fields for input and a "middle-man" program to combine the values so it fits the format that will be used later on?
Did I get that correctly? Can it be a PHP script? You could include the below CODE <?php $firstname = $_POST['[01]First_Name']; $lastname = $_POST['[01]Last_Name']; $[01]lastfirst = $lastname . ", " . $firstname; ?> I assumed that [01] will be replaced by the [id]- counter in the SQL database |
|
|
Mar 3 2008, 02:59 PM
Post
#3
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
Unfortunately I believe its gotta be java. This application runs on a custom webserver and Ive never tried PHP on it. i know it uses java tho.
|
|
|
Mar 3 2008, 03:23 PM
Post
#4
|
|
|
Spyware Veteran Posts: 20,195 From: Netherlands OS: XP Pro & Vista Ultimate |
Java or javascript?
Probably Javascript. Using javascript makes it difficult though. As you probably know javascript is run on the users computer and therefor can behave different depending on his browser, version etc. Should be something like this though CODE <script language="javascript" type="text/javascript"> function combine(idnr) { if (form.lastname[idnr].value=="") { alert("Fill out your last name."); return false; } else if (form.firstname[idnr].value=="") { alert("Fill out your first name."); return false; } fullname[idnr] = lastname[idnr] + ', ' + firstname[idnr]; return fullname[idnr]; } </script> If I was still thinking straight that should check if both fields were filled and combine the two entries. The function can be called with onSubmit or whatever you prefer. You just need to feed it the counter [idnr]. I'm sure one of the js masters can improve that for you. |
|
|
Mar 4 2008, 02:28 PM
Post
#5
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
It could be either, but Java is probably not the correct thing here. I meant Javascript, my bad.
I'm going to try to implement your script yet today and see if I can get it to work. |
|
|
Mar 6 2008, 12:30 PM
Post
#6
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
Everything looks good, until I add in the [01] in front of the field name. For this application the [01] designates which tables in the database the information is stored. I have to have [01], but this wreaks havoc with javascript. Any ideas?
|
|
|
Mar 6 2008, 01:06 PM
Post
#7
|
|
|
Spyware Veteran Posts: 20,195 From: Netherlands OS: XP Pro & Vista Ultimate |
Javascript will probably be trying to interpret the [01] as an index for an array.
Can you postpone adding the [01] untill after the javascript has run? I'm thinking about only using it in a <input type="hidden" name="[01]lastfirst" value=""> field of the form where the value gets set by the script. Or maybe even better, add the [01] in the part where the input from the form is processed. If that is possible that would be the safest option, I guess. |
|
|
Mar 6 2008, 01:15 PM
Post
#8
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
In order for the form to pull the data from the database for display, the [01] has to be in there from the get-go. I figured that Javascript was treating it like an index, but I do not see a good way around it.
In PHP you can cancel a character by proceeding it with a \, can you do that with javascript? Put something in front of or around the characters to make javascript treat them as text? |
|
|
Mar 6 2008, 01:40 PM
Post
#9
|
|
|
Spyware Veteran Posts: 20,195 From: Netherlands OS: XP Pro & Vista Ultimate |
Javascript uses the same escape character the \
http://www.quackit.com/javascript/tutorial..._characters.cfm Not sure how that will work on [ and ] though. Hope it works. Keep us posted |
|
|
Mar 6 2008, 01:45 PM
Post
#10
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
Unfortunately that did not work. I might have to identify the fields by ID rather than by name. That way it doesn't matter what the name is.
|
|
|
Mar 6 2008, 01:48 PM
Post
#11
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
This is the page I am using to test with. Its just a throw together to test if the script works or not. IF I take out the [01] it works great. Unfortunately it has to be there. Ill start working on how to pull by the id rather than the name. Shouldn't be too horrible.
Attached File(s)
|
|
|
Mar 6 2008, 07:55 PM
Post
#12
|
|
![]() TA Moderator Posts: 1,275 From: SET HOMEPATH OS: Windows 95/98/2000/XP/Vista |
Mind if I butt in?
You can reference the fields by using the following code: CODE document.forms[0].elements("[01]First_Name").value; If you're going to put them in the original field, you can do something like the following: CODE var firstname; var lastname; firstname = document.forms[0].elements("[01]First_Name").value; lastname = document.forms[0].elements("[01]Last_Name").value; if (firstname + lastname != ''){ document.forms[0].elements('[01]lastfirst').value = lastname + ', ' + firstname; } Edit: Just noticed your function, here's an edited version: CODE function comblf() { var last = document.forms[0].elements("[01]Last_Name").value; var first = document.forms[0].elements("[01]First_Name").value; if(last.length > 1 && first.length > 1) { document.forms[0].elements('[01]lastfirst').value = last + ', ' + first; } } Appears to work like a charm! Ax |
|
|
Mar 6 2008, 08:26 PM
Post
#13
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
That will make it easier to update later on if I don't have to add id's into the fray. I got it working today by adding an id for each field, but I plan on re-doing it with your revisions tomorrow to see how it works.
Thats great stuff. Ill let you know how it comes out. |
|
|
Mar 6 2008, 10:11 PM
Post
#14
|
|
![]() TA Moderator Posts: 1,275 From: SET HOMEPATH OS: Windows 95/98/2000/XP/Vista |
|
|
|
Mar 10 2008, 09:57 AM
Post
#15
|
|
![]() Geek U Admin Posts: 18,240 From: Michigan, USA OS: All Windows Os's |
Unfortunately that did not work. As soon as I put the brackets in there, it stopped working.
|
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies / Views | Topic Information | |||||
|---|---|---|---|---|---|---|---|
![]() |
1 / 165 | 7th March 2007 - 11:09 AM 31428jim started - last by Johanna |
|||||
![]() |
1 / 146 | 14th March 2007 - 04:22 PM leedownen123 started - last by Neil Jones |
|||||
![]() |
3 / 296 | 17th August 2007 - 01:05 PM cipher021 started - last by cipher021 |
|||||
![]() |
4 / 421 | 29th October 2007 - 10:38 AM Norman-B started - last by Norman-B |
|||||
![]() |
3 / 187 | 29th December 2007 - 07:15 PM ultimateslacker2 started - last by Ztruker |
|||||
|
Time is now: 7th October 2008 - 01:47 AM |
| Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk. |