Welcome Guest ( Log In | Register )

      
Discover the best free computer help!
Learn more about Geeks to Go by taking the tour. Spyware, virus, trojan, fake security or privacy alerts? Read the malware cleaning guide.
3 Pages V   1 2 3 >  
Reply to this topicStart new topic
Concatenate two html fields into one
ScHwErV
post Mar 3 2008, 02:05 PM
Post #1


Geek U Admin
Group Icon
Posts: 18,563
From: Michigan, USA
OS: All Windows Os's



Ok, here is what I have. I have a pre-packaged script with an SQL backend. For some odd reason, the script reads and displays the names as such:

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?
Go to the top of the page
 
+Quote Post
Metallica
post Mar 3 2008, 02:58 PM
Post #2


Spyware Veteran
Group Icon
Posts: 20,724
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
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 3 2008, 02:59 PM
Post #3


Geek U Admin
Group Icon
Posts: 18,563
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.
Go to the top of the page
 
+Quote Post
Metallica
post Mar 3 2008, 03:23 PM
Post #4


Spyware Veteran
Group Icon
Posts: 20,724
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. happy.gif
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 4 2008, 02:28 PM
Post #5


Geek U Admin
Group Icon
Posts: 18,563
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.
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 6 2008, 12:30 PM
Post #6


Geek U Admin
Group Icon
Posts: 18,563
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?
Go to the top of the page
 
+Quote Post
Metallica
post Mar 6 2008, 01:06 PM
Post #7


Spyware Veteran
Group Icon
Posts: 20,724
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.


Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 6 2008, 01:15 PM
Post #8


Geek U Admin
Group Icon
Posts: 18,563
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?
Go to the top of the page
 
+Quote Post
Metallica
post Mar 6 2008, 01:40 PM
Post #9


Spyware Veteran
Group Icon
Posts: 20,724
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 smile.gif
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 6 2008, 01:45 PM
Post #10


Geek U Admin
Group Icon
Posts: 18,563
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.
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 6 2008, 01:48 PM
Post #11


Geek U Admin
Group Icon
Posts: 18,563
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)
Attached File  test.zip ( 429bytes ) Number of downloads: 10
 
Go to the top of the page
 
+Quote Post
Ax238
post Mar 6 2008, 07:55 PM
Post #12


TA Moderator
Group Icon
Posts: 1,276
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
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 6 2008, 08:26 PM
Post #13


Geek U Admin
Group Icon
Posts: 18,563
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.
Go to the top of the page
 
+Quote Post
Ax238
post Mar 6 2008, 10:11 PM
Post #14


TA Moderator
Group Icon
Posts: 1,276
From: SET HOMEPATH
OS: Windows 95/98/2000/XP/Vista



thumbsup.gif Sounds good.
Go to the top of the page
 
+Quote Post
ScHwErV
post Mar 10 2008, 09:57 AM
Post #15


Geek U Admin
Group Icon
Posts: 18,563
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.
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies / Views Topic Information
No New Posts   1 / 153 14th March 2007 - 04:22 PM
leedownen123 started - last by Neil Jones
No New Posts   3 / 320 17th August 2007 - 01:05 PM
cipher021 started - last by cipher021
No New Posts   4 / 456 29th October 2007 - 10:38 AM
Norman-B started - last by Norman-B
No New Posts   3 / 198 29th December 2007 - 07:15 PM
ultimateslacker2 started - last by Ztruker

RSS Time is now: 2nd December 2008 - 01:14 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.