Hello guys,

I'm starting to learn Javascript and I'm having trouble combining 3 text fields on a form in order to submit it in a single textfield.
For example I have the following texfields:

<input id="country_code" name="country_code" size="6">
<input id="area_code" name="area_code" size="5">
<input name="hphone" size="9">

I want to combine them in the texfield "hphone" with javascript, not using php, I tried to add this on the form tag:

<form action="output.php method="post" id="myform" onsubmit="checkCode(this.country_code.value + this.area_code.value + this.hphone.value)">

but no luck on this..
Anyone could help?

Thanks in advance!
coleguitajuan

where is checkcode() source code? Post complete code and what exact problem your facing?

How would somebody know, that what is not working in your case?

I'm sorry,

That was a test that I did but didnt want to publish here.
My question would remain in how could I do that with Javascript, how to combine those 3 textfields on the 'hphone' field. Do you have any idea from I can start to work?

<form action="output.php method="post" id="myform">
   
      <input id="country_code" name="country_code" size="6">
      <input id="area_code" name="area_code" size="5">
      <input name="hphone" size="9"></form>

Many thanks!
Cole

How are we supposed to help you if you don't want to post the code you have?

I'm somewhat unsure on your question, however I assume you'd like to do something like below.

<form name="someForm" action="" onsubmit="checkValues()">
	<p>
		<label for="name">Name:</label>
		<input type="text" name="name" id="name" />
	</p>
	<p>
		<label for="email">Email:</label>
		<input type="email" name="email" id="email" />
	</p>
	<input type="submit" value="Send" />
</form>

<script>
	function checkValues() {
		//Combine here and/or do some checking/validation
		var toSend = document.someForm.name.value+','+document.someForm.email.value;
		//Output = name,email
		//Proceed to send it to some server side script
	}
</script>

Then for example working with a PHP script, you could do something like

$submit = explode(',', $comesFromJavascript);

-MrDJK

EDIT:
Adding the combined values to another field would simply be done by adding another field to your form and then doing something like;

document.someForm.combined.value = toSend;

Thanks MrDJK!

Now my code is like this:

<script>
function checkValues() {
//Combine here and/or do some checking/validation
var toSend = document.someForm.name.value+','+document.someForm.email.value;
//Output = name,email
//Proceed to send it to some server side script
}
</script>
<form action="output.php method="post" id="myform" name="myform">
<input id="country_code" name="country_code" size="6">
<input id="area_code" name="area_code" size="5">
<input name="hphone" size="9"></form>

It is giving me the following error:
Error: "'document.myform.country_code' is null or not an object", even I filled all the fields.

Regards,
Cole

<form action="output.php" method="post" id="myform" name="myform" onsubmit="return checkValues()">
	<p>
		<label for="country_code">Country Code</label>
		<input type="text" name="country_code" id="country_code" />
	</p>
	
	<p>
		<label for="area_code">Area Code</label>
		<input type="text" name="area_code" id="area_code" />
	</p>
	<p>
		<!-- Not sure if you want this hidden or not. -->
		<label for="hphone">Hphone</label>
		<input type="text" name="hphone" id="hphone" />
	</p>
	
	<input type="submit" value="Send" />
</form>

<script>
	function checkValues() {
	
		var combine = document.myform.country_code.value + ',' + document.myform.area_code.value;
		
		document.myform.hphone.value = combine;
		
		//Want to do an ajax request here? If so do the ajax request then return false;
		return false;
		//If not return true;
	}
</script>

The above works fine. :)
I wasn't sure what you wanted to do with the combined field so I left some comments within the code.

Hello MrDJK,

That works perfect for me!
Thank you!
Cole

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.