JavaScript function doesn't work

The function:
	function enableBoxesSold(whichSelect,whichBox,overage,shortage){
		alert("here");
	}

Open in new window


doesn't work when called with:
echo "<SELECT size=1 id='contractor1' name='contractor1' onblur='enableBoxesSold('contractor1','BoxesSold1','contractor1o','contractor1s');updateBoxes(1);' tabindex=1>";

Open in new window


It does work if I remove all parameters from both the function and the onblur event.  If I add in one parameter it fails.

This php page has over 2,000 lines and the error I see, using Chrome dev tools, is:

Uncaught SyntaxError: Unexpected token } at line 2145

Line 2145 is no where near this function or select element.

Any thought?
Sheldon LivingstonConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Something in your PHP code is not bracketed properly.  I run into this when I'm editing a page at times.  The first thing I would try is commenting out the '}' on line 2145 and see what happens.
Dave BaldwinFixer of ProblemsCommented:
In your 'onblur' statement, the variables for the enableBoxesSold() function must be defined in the HTML page before you reach that line.  Note that those are JavaScript variables, not PHP variables.
Sheldon LivingstonConsultantAuthor Commented:
Dave... line 2145 is
<tr>

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Sheldon LivingstonConsultantAuthor Commented:
Hmmm so you mean stick something like
	var whichSelect;
	var whichBox;
	var overage;
	var shortage;

Open in new window


in the code?  I never heard of that being an issue.
Sheldon LivingstonConsultantAuthor Commented:
The issue was using ' instead of ".  I had to use \"
Dave BaldwinFixer of ProblemsCommented:
Is it fixed now?  When you pass values (variables) to a function, they do have to be defined first.  Otherwise you are passing NULL or undefined value which is usually an error.
Julian HansenCommented:
As you have correctly deduced the problem is you are outputting a string which has single quotes enclosed in single quotes - which is going to cause the browser to throw the error you are seeing.
So instead of this
echo "<SELECT size=1 id='contractor1' name='contractor1' onblur='enableBoxesSold('contractor1','BoxesSold1','contractor1o','contractor1s');updateBoxes(1);' tabindex=1>";

Open in new window

I would rather use heredoc to output lines like the above.
Mixing double and single quotes in a quoted string is always a pain. Heredoc gets around this problem by allowing both " ' and $variable in the string.

In your case it would be
echo <<< HTML
<select size=1 id="contractor1" name="contractor1" onblur="enableBoxesSold('contractor1','BoxesSold1','contractor1o','contractor1s');updateBoxes(1);" tabindex=1>
HTML;

Open in new window


Note with HEREDOC the closing tag (in this case HTML) must be flush against the left margin with no spaces after the ';'

More info here http://php.net/manual/en/language.types.string.php

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sheldon LivingstonConsultantAuthor Commented:
Going with HEREDOC...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.