Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Concatenate two html fields into one


  • Please log in to reply

#1
ScHwErV

ScHwErV

    Member 5k

  • Retired Staff
  • 21,285 posts
  • MVP
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[codebox]<input type="text" name="[01]First_Name" value="" size="15"> <input type="text" name="[01]Last_Name" value="" size="15">[/codebox]
Theirs[codebox]<input type="text" name="[01]lastfirst" value="" size="30">[/codebox]

Make any sense?
  • 0

Advertisements


#2
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 33,101 posts
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

<?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
  • 0

#3
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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.
  • 0

#4
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 33,101 posts
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

<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. :)
  • 0

#5
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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.
  • 0

#6
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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?
  • 0

#7
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 33,101 posts
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.
  • 0

#8
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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?
  • 0

#9
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 33,101 posts
Javascript uses the same escape character the \
http://www.quackit.c..._characters.cfm
Not sure how that will work on [ and ] though.

Hope it works. Keep us posted :)
  • 0

#10
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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.
  • 0

Advertisements


#11
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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 Files

  • Attached File  test.zip   429bytes   491 downloads

  • 0

#12
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
Mind if I butt in?

You can reference the fields by using the following 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:
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:
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
  • 0

#13
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
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.
  • 0

#14
Ax238

Ax238

    Tech Staff

  • Technician
  • 1,323 posts
:) Sounds good.
  • 0

#15
ScHwErV

ScHwErV

    Member 5k

  • Topic Starter
  • Retired Staff
  • 21,285 posts
  • MVP
Unfortunately that did not work. As soon as I put the brackets in there, it stopped working.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP