-1

I'm building a shopping cart and have a simple but perplexing problem. I'm not good at javascript, so here it goes.

I'm trying to get the id of a record who's upc is equal to upc.

<input type="button" value="Add to Cart" onclick="get_product_id(<?=$row['upc']?>)" />

The reason I'm not getting the id directly is I'll be getting the record based on a list of options that builds the upc.

Uncaught ReferenceError: rtt is not defined 
onclick

However, if I pass any column of the records that is an int-type value, there is no error.

0

2 Answers 2

2

String literals in JavaScript must be quoted. The value of $row['upc'] presumably doesn't include quote characters, so it is being treated as an identifier (a variable in that context).

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, gotcha. So either define the variable as a string earlier or put some more quotes on it right?
You'll have to put quotes on it either way.
0

Try putting in apostrophes surrounding the upc-value:

<?php
$upc = $row['upc'];
?>
<input type="button" value="Add to Cart" onclick="get_product_id('<?=$upc;?>')" /> 

If you need to treat it as a number in javascript, then just use parseInt().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.