Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

Syntax to concatenate form fields in JavaScript

I have the following to take a creditcard month MM and year YYYY and concatenate them to MM/YY. It is creating an error messageParse error: syntax error, unexpected T_VARIABLE in /home/site/public_html/form.php on line 1

Can anyone tell me what the problem is?

<script language="Javascript">

   function cloneText()
 {
       document.myform.CCExpiration.value =
 document.myform.CreditCard0ExpirationMonth.value + "/" +
 document.myform.CreditCard0ExpirationYear.value.substring(2);
}
       
 </script>
<script language="Javascript">

   function cloneText()
 {
 	document.myform.CCExpiration.value =
 document.myform.CCExpirationMonth.value + "/" + 
 document.myform.CCExpirationYear.value.substring(2);
}
 	
 </script>

Open in new window

0
3rdLifeWebDev
Asked:
3rdLifeWebDev
  • 8
  • 5
  • 4
  • +2
2 Solutions
 
DrDamnitCommented:
That's a php error, and you've posted the javascript.

Can you please post the form.php file contents?
0
 
3rdLifeWebDevAuthor Commented:
Let me be more specific...

The JavaScript works fine when:  

<script language="Javascript">

   function cloneText()
 {
        document.myform.CCExpiration.value =
 document.myform.CCExpirationMonth.value;
}
       
 </script>

It is when I put in the concatenation code that the page breaks.
0
 
MorcalavinCommented:
The error you posted above is coming from the php script, not any javascript.  The server is reporting the error.  Show us the contents of the form.php file.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HonorGodCommented:
Might you have a typographical error?

Does this:

document.myform.CCExpirationYear.value

exist?
0
 
3rdLifeWebDevAuthor Commented:
HonorGod, yes the value does exist.
0
 
DrDamnitCommented:
Please post form.php. Otherwise, we're all just shooting in the dark.
0
 
DrDamnitCommented:
Alternatively, you consider putting var_dump($_POST) so you can see what is getting processed.

I am betting you'll see the problem immediately after doing that.

Please post the results of this too.
0
 
HonorGodCommented:
What is the value of

document.myform.CCExpirationYear.value

If it is less than 2 characters long,

document.myform.CCExpirationYear.value.substring(2)

is going to have a problem.
0
 
3rdLifeWebDevAuthor Commented:
The Value is 4 characters long ... YYYY
0
 
3rdLifeWebDevAuthor Commented:
For all who have been kind enough to answer:

I looked at line 1 which contacted:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

I took this out and the form works fine now. The issue seems to be the rules in the DOCTYPE do not allow a JavaScript concatenation the way that I have it programmed.

Any input on this? Even though I found a work - around, it would be really good to know what the real issue is.

Thanks!
0
 
HonorGodCommented:
Well it has to be something else then.

If we look only at the code, as seen below, the output is an alert box showing 01/10, which is what we expect.
<html>
<body>
  <script type='text/javascript'>
    var m = '01'
    var y = '2010'
    alert( m + '/' + y.substring( 2 ) )
  </script>
</body>
</html>

Open in new window

0
 
3rdLifeWebDevAuthor Commented:
Could it be the use of double quotes instead of single quotes for "/" ?
0
 
MorcalavinCommented:
How can we find the error?  The original error message you posted(Parse error: syntax error, unexpected T_VARIABLE in /home/site/public_html/form.php on line 1) is a PHP error, and has nothing to do with javascript.  You have not provided the php code that was causing the error, so we are still chasing phantoms.
0
 
3rdLifeWebDevAuthor Commented:
Morcalavin, The PHP code is not causing the error. It is the JavaScript that is causing the error.

This appear not to be a php error. Initially, I thought it may be, but have determined otherwise.

Thank you for your attention.
0
 
3rdLifeWebDevAuthor Commented:
Morclalavin,

FYI, the following code is Line 1, where the syntax error is occuring:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
0
 
MorcalavinCommented:
The doctype shouldn't throw a javascript error.  When you add that line back in, do you still get the following error:
Parse error: syntax error, unexpected T_VARIABLE in /home/site/public_html/form.php on line 1

If so, it's still a php error.  If not, it's possible you have the doctype declaration in the wrong place in yoru html code.  It would be helpful to see the php code and the html output as well.
0
 
HonorGodCommented:
re: http://#a26193556

No, it isn't the double quotes.

In JavaScript, either double, or single quotes may be used to surround a string.
So, these are equivalent:
var m = '01';


var m = "01";

Open in new window

0
 
3rdLifeWebDevAuthor Commented:
Morcacalvin,

The page works with the DOCTYPE in place and the javascript NOT containing the concatenation code.

It is only where I add the concatenation code that the page breaks and gives me the systax error message.

Then when I remove the <!DOCTYPE ...> then the page works.

I do not think the error is in the php, since there is NO edit of the php when I add the javascript concatenation.

The issue seems to be between the DOCTYPE rules and the concatenation in the javascript.
0
 
MorcalavinCommented:
Just humor us.  Post the php code as it is when it is causing the error.  Having all the code available really makes the difference.
0
 
MangagmCommented:
try this:
<script language="Javascript">

   function cloneText()
 {
       document.myform.CCExpiration.value = eval(
 document.myform.CCExpirationMonth.value.toString() + "/" +
 document.myform.CCExpirationYear.value.toString().substring(2);
)

}
       
 </script>

if that doesnt work the problem will be most likely that ur component is expecting a number value and your throwing a string value, that y it works when you send the number:

document.myform.CCExpiration.value =
 document.myform.CCExpirationMonth.value;  

but not when you send the string concatenated:

document.myform.CreditCard0ExpirationMonth.value + "/" +
 document.myform.CreditCard0ExpirationYear.value.substring(2);


try with firefox, it has an option called errores console in tools, clic the errors button, then the clean button, then reload the page where your having the problem and see what errors occur

if its a javascript error, it will appear in there, if not, its most likely what I told you, either a component is expecting a number or a especial formated string if its a datetype or something like that
0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now